CS October 2017

Similar documents
Distributed Systems. 03r. Python Web Services Programming Tutorial. Paul Krzyzanowski TA: Long Zhao Rutgers University Fall 2017

CIS192 Python Programming

CIS192 Python Programming

Quick housekeeping Last Two Homeworks Extra Credit for demoing project prototypes Reminder about Project Deadlines/specifics Class on April 12th Resul

Flask-Cors Documentation

REST API OVERVIEW. Design and of Web APIs using the REST paradigm.

Microservices using Python and Oracle. Arup Nanda Longtime Oracle DBA And Explorer of New Things

HOW TO FLASK. And a very short intro to web development and databases

RESTful Services. Distributed Enabling Platform

Other architectures are externally built or expanded

Building Web Applications

RESTful Web Services. 20-Jan Gordon Dickens Chariot Solutions

flask-jwt-simple Documentation

Teach your (micro)services speak Protocol Buffers with grpc.

HTTP Console Documentation

Flask Slither Documentation

Introduction to RESTful Web Services. Presented by Steve Ives

REST in a Nutshell: A Mini Guide for Python Developers

GitHub-Flask Documentation

29-27 May 2013 CERN WEB FRAMEWORKS. Adrian Mönnich

REST Easy with Infrared360

Creating a REST API which exposes an existing SOAP Service with IBM API Management

Creating a REST API which exposes an existing SOAP Service with IBM API Management

Composer Help. Web Request Common Block

Microservices with Node.js

RESTFUL WEB SERVICES - INTERVIEW QUESTIONS

grpc - A solution for RPCs by Google Distributed Systems Seminar at Charles University in Prague, Nov 2016 Jan Tattermusch - grpc Software Engineer

Integrating with ClearPass HTTP APIs

Develop Mobile Front Ends Using Mobile Application Framework A - 2

Session 12. RESTful Services. Lecture Objectives

Apache Wink Developer Guide. Draft Version. (This document is still under construction)

IEMS 5722 Mobile Network Programming and Distributed Server Architecture Semester 2

RedBarrel Documentation

REST A brief introduction

Introduction to Web Services & SOA

APIs and API Design with Python

Chapter 1 - Consuming REST Web Services in Angular

nacelle Documentation

web.py Tutorial Tom Kelliher, CS 317 This tutorial is the tutorial from the web.py web site, with a few revisions for our local environment.

describe the functions of Windows Communication Foundation describe the features of the Windows Workflow Foundation solution

Requests Mock Documentation

NETCONF Design and Implementation of a Prototype

Developing RESTful Services Using JAX-RS

AIM Enterprise Platform Software IBM z/transaction Processing Facility Enterprise Edition 1.1.0

SURVEY ON JIRA INTEGRATION USING REST APIs

LOG8430: Architecture logicielle et conception avancée

Introduction to Web Services & SOA

Coding Intro to APIs and REST

Archer Documentation. Release 0.1. Praekelt Dev

I was given the following web application: and the instruction could be found on the first page.

Connexion Documentation

Lesson 7: Defining an Application

flask-jwt Documentation

Restful Interfaces to Third-Party Websites with Python

Assignment 2. Start: 15 October 2010 End: 29 October 2010 VSWOT. Server. Spot1 Spot2 Spot3 Spot4. WS-* Spots

REST in Peace Mastering the JSDO with a Dynamic ABL backend. Mike Fechner, Consultingwerk Ltd.

DatabaseRESTAPI

Controller/server communication

Managing Remote Medical Devices Through The Cloud. Joel K Young SVP of Research and Development & CTO Digi International Friday, September 9 11:30AM

API Documentation TOA4 Monitor Watch Remote Procedure Call API

Webspeed. I am back. Enhanced WebSpeed

Oracle Transportation Management. REST API Getting Started Guide Release Part No. E

CS 498RK FALL RESTFUL APIs

XML Processing & Web Services. Husni Husni.trunojoyo.ac.id

Lesson 14 SOA with REST (Part I)

INFO/CS 4302 Web Informa6on Systems

websnort Documentation

Flask-Genshi Documentation

A RESTful Approach to the Management of Cloud Infrastructure. Swit Phuvipadawat Murata Laboratory

Operating Systems. 18. Remote Procedure Calls. Paul Krzyzanowski. Rutgers University. Spring /20/ Paul Krzyzanowski

Flask-Twilio Documentation

ReST 2000 Roy Fielding W3C

Services Web Nabil Abdennadher

Writing APIs in Lumen

Copyright 2014 Blue Net Corporation. All rights reserved

DESIGN AND IMPLEMENTATION OF SAGE DISPLAY CONTROLLER PROJECT

Power to the People! Web Service Scoring for the Masses

Versioned APIs with Phoenix. Elvio Vicosa

flask-jwt-extended Documentation

Full Stack Web Developer Nanodegree Syllabus

ExtraHop 7.3 ExtraHop Trace REST API Guide

04 Webservices. Web APIs REST Coulouris. Roy Fielding, Aphrodite, chp.9. Chp 5/6

TOA4 Remote Procedure Call (RPC) API

Django-CSP Documentation

LUCITY REST API INTRODUCTION AND CORE CONCEPTS

CS 417 9/18/17. Paul Krzyzanowski 1. Socket-based communication. Distributed Systems 03. Remote Procedure Calls. Sample SMTP Interaction

Blueprint REST API Developer Guide

Restful Application Development

BaasBox. Open Source Backend as a Service. Otto Hylli

A bit more on Testing

Introduction to REST Web Services

How A Website Works. - Shobha

Protocols. Application Layer FTP, HTTP, SSH, IMAP. Transport Layer TCP, UDP. Internet Layer IP. Link Layer Ethernet, WiFi

White paper irmc Redfish API

Reviewing the API Documentation

NIELSEN API PORTAL USER REGISTRATION GUIDE

Backends and Databases. Dr. Sarah Abraham

CS50 Quiz Review. November 13, 2017

REST Web Services Objektumorientált szoftvertervezés Object-oriented software design

File Resource Operations Summary

Transcription:

From Web Browsing to Web Services Web browser: Dominant model for user interaction on the Internet Distributed Systems 03r. Python Web Services Programming Tutorial Not good for programmatic access to data or manipulating data UI is a major component of the content Site scraping is a pain! Paul Krzyzanowski TA: Long Zhao Rutgers University Fall 2017 1 2 Web Services Web Services We wanted: Remotely hosted services that programs can use Machine-to-machine communication Set of protocols by which services can be published, discovered, and used in a technology neutral form Language & architecture independent Problems Web pages are content-focused Traditional RPC solutions usually used a range of ports And we need more than just RPC sometimes Many RPC systems didn t work well across languages Firewalls restrict ports & may inspect the protocol No support for load balancing Applications will typically invoke multiple remote services Service Oriented Architecture (SOA) SOA = Programming model General principles Payloads are text (XML or JSON) Technology-neutral HTTP used for transport Use existing infrastructure: web servers, firewalls, load-balancers 3 4 REST REST REST stands for REpresentational State Transfer REST Server simply provides access to resources REST was first introduced by Roy Fielding in year 2000 REST is a web standards based architecture Uses HTTP Protocol for data communication Resource-oriented every component is a resource a resource is accessed by a common interface using HTTP standard methods REST client accesses and presents the resources REST resources each resource is identified by URIs/ Global IDs representations of a resource Text, JSON and XML JSON is now the most popular format 5 6 Paul Krzyzanowski 1

RESTful Web Services A web service is: A collection of open protocols Standards used for exchanging data between applications or systems Interoperability between different languages (Java and Python) or platforms (Windows and Linux) Everything Is a Resource Any interaction of a RESTful API is an interaction with a resource. Resources are sources of information, typically documents or services. Web services based on REST Architecture are known as RESTful Web Services Use HTTP methods to implement the concept of REST architecture URI (Uniform Resource Identifier) to define a RESTful service Resources representation: JSON A user can be thought of as resource and thus has an URL such as in the case of GitHub: https://api.github.com/users/lrei 7 8 Everything Is a Resource Everything Is a Resource Resources can have different representations. The above mentioned user has the following JSON representation (partial document): Resources are Nouns If I want to delete a post whose ID is 233: { } "login": "lrei", "created_at": "2008-11-21T14:48:42Z", "name": "Luis Rei", "email": "me@luisrei.com", "id": 35857, "blog": "http://luisrei.com" http://api.example.com/posts/delete/233/ The correct way: Send a DELETE HTTP request to the URL: http://api.example.com/posts/233/ 9 10 HTTP Methods The following HTTP methods are most commonly used in a REST based architecture. GET Provides a read only access to a resource. PUT Used to create a new resource. DELETE Used to remove a resource. POST Used to update an existing resource or create a new resource. OPTIONS Used to get the supported operations on a resource. Implementing RESTful Web APIs with Python & Flask 11 12 Paul Krzyzanowski 2

Flask Flask is a microframework for Python based on Werkzeug, a WSGI utility library. To install: >> (sudo) pip install flask Flask is a good choice for a REST API because it is: Written in Python; Simple to use; Flexible; Multiple good deployment options; RESTful request dispatching. We use the curl command to make test requests. curl is a command that lets you transfer data to or from a server using several protocols, most commonly HTTP See https://curl.haxx.se Note: the ilab systems already have flask and python installed 13 14 Let's begin by making a complete app that responds to requests at the root, /articles and /articles/:id. Let's begin by making a complete app that responds to requests at the root, /articles and /articles/:id. from flask import Flask, url_for app = Flask( name ) @app.route('/') def api_root(): @app.route('/articles/<articleid> ) def api_article(articleid): return 'You are reading ' + articleid + '\n' return 'Welcome\n' @app.route('/articles') def api_articles(): if name == ' main ': app.run() return 'List of ' + url_for('api_articles ) + '\n' 15 16 GET Parameters You can use curl to make the requests using: >> curl http://127.0.0.1:5000/ The responses will be, respectively, >> curl http://127.0.0.1:5000/ GET / Welcome >> curl http://127.0.0.1:5000/articles GET /articles List of /articles >> curl http://127.0.0.1:5000/articles/123 GET /articles/123 You are reading 123 Let s begin by making a complete app that responds to requests at /hello and handles an optional GET parameter from flask import request @app.route('/hello') def api_hello(): if 'name' in request.args: else: return 'Hello ' + request.args['name'] + '\n' return 'Hello John\n' if name == ' main ': app.run() 17 18 Paul Krzyzanowski 3

GET Parameters The server will reply in the following manner: >> curl http://127.0.0.1:5000/hello GET /hello Hello John >> curl http://127.0.0.1:5000/hello?name=peter GET /hello?name=peter Hello Peter Request Methods (HTTP Verbs) Let s modify the to handle different HTTP verbs: @app.route('/echo', methods = ['GET', 'POST', 'PUT', 'DELETE']) def api_echo(): if request.method == 'GET': return "ECHO: GET\n" elif request.method == 'POST': return "ECHO: POST\n" elif request.method == 'PUT': return "ECHO: PUT\n" elif request.method == 'DELETE': return "ECHO: DELETE\n" 19 20 Request Methods (HTTP Verbs) Request Data & Headers To curl the -X option can be used to specify the request type: >> curl -X POST http://127.0.0.1:5000/echo The replies to the different request methods will be: GET /echo ECHO: GET Usually POST is accompanied by data. And sometimes that data can be in one of multiple formats: plain text, JSON, XML, your own data format, a binary file. Accessing the HTTP headers is done using the request.headers dictionary ("dictionary-like object") and the request data using the request.data string. If the mimetype is application/json, request.json will contain the parsed JSON. POST /echo ECHO: POST 21 22 Request Data & Headers Request Data & Headers Usually POST is accompanied by data. And sometimes that data can be in one of multiple formats: plain text, JSON, XML, your own data format, a binary file. To specify the content type with curl: >> curl -H "Content-type: application/json" -X POST \ http://127.0.0.1:5000/messages -d '{"message": "Hello Data"}' from flask import json @app.route('/messages', methods = ['POST ]) def api_message(): if request.headers['content-type'] == 'text/plain': return "Text Message: " + request.data + '\n' elif request.headers['content-type'] == 'application/json': return json.dumps(request.json) else: return "415 Unsupported Media Type ;)" The replies to the different content types will be: POST /messages "Hello Data" Content-type: text/plain Text Message: Hello Data POST /messages {"message": "Hello Data"} Content-type: application/json {"message": "Hello Data"} 23 24 Paul Krzyzanowski 4

Responses Responses Responses are handled by Flask's Response class: from flask import Response @app.route('/hello', methods = ['GET ]) def api_hello(): data = { 'hello': 'world', 'number': 3 } js = json.dumps(data) resp = Response(js, status=200, mimetype='application/json ) return resp To view the response HTTP headers using curl, specify the -i option: >> curl -i http://127.0.0.1:5000/hello The response returned by the server, with headers included, will be: GET /hello HTTP/1.0 200 OK Content-Type: application/json Content-Length: < > Server: < > Date: < > { "hello": "world", "number": 3 } 25 26 Status Codes & Errors Status Codes & Errors 200 is the default status code reply for GET requests, in both of these examples. There are certain cases where overriding the defaults is necessary: error handling. @app.errorhandler(404) def not_found(error=none): message = { 'status': 404, 'message': 'Not Found:' + request.url } resp = jsonify(message) resp.status_code = 404 return resp @app.route('/users/<userid>', methods = ['GET ]) def api_users(userid): users = { '1 : 'john', '2 : 'steve', '3 : 'bill } if userid in users: return jsonify({ userid: users[userid] }) else: return not_found() 27 28 Status Codes & Errors Other Useful Links This produces: GET /users/2 HTTP/1.0 200 OK { "2": "steve" } ilab: https://www.cs.rutgers.edu/resources/instructionallab JSON: http://www.json.org/ GET /users/4 HTTP/1.0 404 NOT FOUND { "status": 404, "message": "Not Found: http://127.0.0.1:5000/users/4" } Flask Framework: http://flask.pocoo.org/ Flask Quick Start: http://flask.pocoo.org/docs/0.12/quickstart/ Implementing a RESTful Web API with Python & Flask: http://blog.luisrei.com/articles/flaskrest.html 29 30 Paul Krzyzanowski 5

The end 31 Paul Krzyzanowski 6