ReSTful API using Flask Demo
By Dr. Louay Chebib
ReSTful API using Flask Demo
Very flexible and uses an existing protocol like HTLM
REST not constrained to XML, can return XML, JSON, YAML (YAML Ain't Markup Language) or anything else.
What is ReST?
Representational State Transfer (ReST)
ReSTful API using Flask Demo
- Client-Server Independence
-
Stateless and self contained calls independent of each other.
- State is stored on the client—not on the server
- Cache reused data on the client with an expiry time
- Uniform Interface: client & server talk using loose coupling
- Communicate using HTTP with URI resources, CRUD (Create, Read, Update, Delete), & JSON.
- Layered System provides loose coupling, flexibility, independence, portability of processes & functions & increases security through independence.
- Code on Demand is an optional constraint that is not fully accepted since it allows for the creation & execution of ad-hoc applications.
Constraints
ReSTful API using Flask Demo
Use urllib to get web page data
Simple BOTs
Demo: urllib_HTML.py
- Get a page of Text
- Dump 2 web pages (HTML)
- Download 2 Images (JPeG)
ReSTful API using Flask Demo
Use paramiko to send commands & get data over SSH (UNIX)
Automating SSH
Demo: SSH_paramiko_Demo.py
- Send text to a file
- Get Time/Date and update local Time/Date
- Can Execute command on a remote system!
ReSTful API using Flask Demo
Simple Flask Server
Demo: Hello World
Server: hello_flask.py
Client: in browser http://127.0.0.1:5000/
or run: urllib_hello.py
ReSTful API using Flask Demo
ReST API Flask Server & Client
Demo: "CRUD" like
Run on two Sessions/Devices
Server: rest_server_demo.py
(Flask loves Decorators)
Client: REST_client_demo.py
- Add 2 tasks
- Change a task by key (id)
- Delete a task by key (id)
- List a task by key (id)
- List All tasks
Can also be run with: curl
ReST is not CRUD!
ReSTful API using Flask Demo
Other Items
Document API interface: preferably at server. e.g. swagger
HTTP methods: (POST GET PATCH DELETE) in ReST relate to CRUD
Compare ReST to SOLID
Store values/data to: database, files...
User validation should be hashed/encrypted
Expose Flask Server: nginx.org
RESTful API using Flask
By Dr. L. Chebib
RESTful API using Flask
RESTful API using Flask
- 1,459