Share
Embed
RESTful Microservices with Python
Is it a good idea to write RESTful microservices in Python? We will see that is the case.
Antonio Ognio
July 24, 2014
Transcript
30 slides
-
RESTful Microservices with Python Antonio Ognio <[email protected]> Systems Architect at
Red Científica Peruana L I G H T N I N G :. T A L K S:. -
REST A way of taking full advantage of the existing
web-related protocols and software in order to query and manipulate objects remotely. -
REST A way of taking full advantage of the existing
web-related protocols and software in order to query and manipulate objects remotely. -
REST ✓ Was reverse-engineered by Roy Fielding for this PhD
thesis from the existing web technologies after they were created (2000). Requires you to use provide links to your resources ✓ so your APIs are browseable. Requires you to respect to uniform interface of ✓ HTTP and use each of the verbs without violating their own semantics. -
REST HTTP Verb CRUD Action GET Read POST Update PUT
Create DELETE Delete Isn't REST just a mapping of verbs to.. ? -
REST HTTP Verb CRUD Action GET Read POST Update PUT
Create DELETE Delete Isn't REST just a mapping of verbs to.. ? WRONG!!! -
Some things you should be doing... ✓ Returning many different
status codes not just 200 or 404: Accepted (201) Created (202) No Content (204) Moved Permanently (301) See Other (303) Bad Request (400) Unauthorized (401) Forbidden (403) Method Not Allowed (405) Not Acceptable (406) Conflict (409) Gone (410) Internal Server Error (500) etc. -
Some things you should be doing... ✓ Making sure one
“thing” or “entity” in your domain problem have one and JUST ONE URI associated with it. Providing a “home document” at the root of your service linking to ✓ the main resources available. Using URI templates with named parameters so client code does not ✓ have to hard code URIs. Providing paginated results including hyperlinks to the next, prev, ✓ first and last pages. Returning 200 and not 404 when a search yields no results. ✓ Returning 400 if an input validation fails but a 409 if the data is OK ✓ but there is a business model conflict in the requested action. -
Master your HTTP codes httpstatus.es Point your browser to for
a friendly refresher on available status codes and their actual meanings... Point your browser to -
Learn more about REST (1) Roy Fielding's PhD dissertation Grab
your own free copy of http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm ...it is surprisingly easy to read and very informative -
Learn more about REST (2) REST for the REST of
us Grab your own free copy of http://vimeo.com/82725804 ...a talk at gave at a conference in Chile last year https://speakerdeck.com/gnrfan/rest-for-the-rest-of-us -
Microservice Architecture Building a larger system or service by piecing
together smaller services that are usually highly reusable, self-contained, conceptually simple and easy to maintain. -
Advantages of Microservices ✓ Once you define a public API
and others start using it you can change the implementation as you which without breaking things. Each service is so small that one engineer can fully understand it and ✓ make a much better job of maintaining it. You are no longer limited to using an specific language, version or ✓ database for your whole system: use what is best for the job! Upgrading the system one micro-service at a time is much easier ✓ than upgrading a monolithic system. Prototype a microservice in a language you're comfortable with and ✓ re-implement it in any other as needed if that helps for speed or reliability reasons. -
Learn more about Microservices (1) Martin Fowler's article on Microservices
Grab your own free copy of http://martinfowler.com/articles/microservices.html ..it is short but comprehensive and useful in general -
Learn more about Microservices (2) µCon: The Microservices Conference Attend
https://skillsmatter.com/conferences/6312-mucon ..27th and 28th of November 2014, London, UK. -
Why Python? (for Microservices) ✓ The most important thing is
to get the API right so prototyping quickly and easily is key and Python is great for that. There are good microframeworks available for Python, most notably ✓ Flask, you might not need a full-fledged Django installation. This is a great opportunity to build production code in Python 3. No ✓ more excuses!!! Chance to build asynchronous services using Tornado or Twisted. ✓ Re-implement using PyPy, Cython, Golang, Java, C++, Clojure, etc as ✓ needed if you require more speed or efficiency. You can build a Python web service frontend to many microservices ✓ in legacy languages like PHP, ASP, etc. -
Useful Python Packages ✓ requests uritemplate ✓ rfc3339 ✓ Tornado
✓ hal-json ✓ halogen ✓ collection-json ✓ negotiator ✓ Flask-Restless ✓ tastypie ✓ djangorestframework ✓ https://github.com/gnrfan/django-restful ✓ -
RESTful Microservices with Python Antonio Ognio <[email protected]> Systems Architect at
Red Científica Peruana L I G H T N I N G :. T A L K S:.