a python a web service - files.meetup.comfiles.meetup.com/8579852/pytim #4 vlad temian - a python...

85
A Python a web service @vtemian

Upload: others

Post on 18-Jul-2020

17 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

A Python a web service@vtemian

Page 2: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Let’s imaginea perfect world...

Page 3: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

WEB APPLICATION

Page 4: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web serviceWEB APPLICATION

Tools & Utilities

Page 5: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web serviceWEB APPLICATION

Tools & Utilities Web Process

Page 6: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Tools & Utilities Web Process Worker Process

WEB APPLICATION

Page 7: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Tools & Utilities

management tools

supporting services

Web Process Worker Process

WEB APPLICATION

Page 8: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Tools & Utilities

management tools

supporting services

Web Process

user interface

api service

data persistence

auth

crud admin

Worker Process

WEB APPLICATION

Page 9: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Tools & Utilities

management tools

supporting services

Web Process

user interface

api service

data persistence

auth

crud admin

Worker Process

deferred tasks

scheduled tasks

WEB APPLICATION

Page 10: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Tools & Utilities

management tools

supporting services

Web Process

user interface

api service

data persistence

auth

crud admin

Worker Process

deferred tasks

scheduled tasks

WEB APPLICATION

Page 11: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Tools & Utilities

management tools

supporting services

Web Process

user interface

api service

data persistence

auth

crud admin

Worker Process

deferred tasks

scheduled tasks

WEB APPLICATION

Page 12: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Tools & Utilities

management tools

supporting services

Web Process

user interface

api service

data persistence

auth

crud admin

Worker Process

deferred tasks

scheduled tasks

WEB APPLICATION

Page 13: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Tools & Utilities

management tools

supporting services

Web Process

user interface

api service

data persistence

auth

crud admin

Worker Process

deferred tasks

scheduled tasks

WEB APPLICATION

Page 14: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Single codebase areEVIL!

Page 15: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web serviceWEB APPLICATION

Tools & Utilities

management tools

supporting services

Web Process

user interface

api service

data persistence

auth

crud admin

Worker Process

deferred tasks

scheduled tasksTOTAL MESS

Page 16: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Decouple!

Page 17: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Developers

API service

Data persistence

Frontend

End Users

Page 18: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Developers End Users Internal

API service API service API service

Data persistence

Page 19: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Good API

Page 20: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Good API

Page 21: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Good APIIntuitive

similar things should be similar, ugly things should look ugly

Documented

Opinionated

Page 22: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Featurespagination

propper HTTPresponse handling throttling

permissions serializationauthentification

posting of data with validation

Page 23: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Django

regex routing system

Page 24: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Django

regex routing system built-in orm

Page 25: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

users = Users.objects.filter(last_login__lt=timestamp)

Django

Page 26: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

regex routing system

authmigrations

management tools makes decision for you

templating system

built-in orm crud admin

testing tools

Django

Page 27: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service NOBODY CARES

Page 28: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Developers End Users Internal

API service API service API service

Data persistence

Django

Page 29: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

regex routing system

makes decision for you

templating system

testing tools

Django

Page 30: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Page 31: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Django REST Framework

Page 32: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Just install it, write 20 lines of code> CRUD API

Page 33: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

API Console

Page 34: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Great community

Page 35: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Easy to document your API

Page 36: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Integration test usingAPIRequestFactory

Page 37: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

OAuth 1 & 2 out of the box

Page 38: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Serialization

Page 39: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Django paginatoror PaginationSerializer

Page 40: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Object or field level validation

Page 41: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Using django caching policy

Page 42: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

For throttling, just add: DEFAULT_THROTTLE_CLASSES +

DEFAULT_THROTTLE_RATESin settings.py

Page 43: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Powerful and extensible object level permissions

Page 44: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Page 45: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Depends on Django >get all the Django mess

Page 46: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Pretty huge

Page 47: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Can do a lot of black magic

Page 48: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Rigid

Page 49: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Django Tastypie

Page 50: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Plug and play

Page 51: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Decent auth support

Page 52: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Documented

Page 53: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Still supported

Page 54: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Paginator for pagination

Page 55: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Validation --- you can extend it

Page 56: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

serializer = Serializer(formats=[‘json’, ‘jsonp’, ‘xml’, ‘yaml’, ‘html’, ‘plist’])

Page 57: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

throttle = BaseThrottle(throttle_at=100)

Page 58: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

NO NO NO

Page 59: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Not intuitive

Page 60: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Ugly

Page 61: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

class ParentResource(ModelResource): children = fields.ToManyField(ChildResource, ‘children’)

def prepend_urls(self): return [ url(r”^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/children%s$” % (self._meta.resource_name, trailing_slash()), self.wrap_view(‘get_children’), name=”api_get_children”), ] def get_children(self, request, **kwargs): try: bundle = self.build_bundle(data={‘pk’: kwargs[‘pk’]}, request=request) obj = self.cached_obj_get(bundle=bundle, **self.remove_api_resource_names(kwargs)) except ObjectDoesNotExist: return HttpGone() except MultipleObjectsReturned: return HttpMultipleChoices(“More than one resource is found at this URI.”)

child_resource = ChildResource() return child_resource.get_detail(request, parent_id=obj.pk)

Page 62: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Depends on Django >get all the Django mess

Page 63: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Can do a lot of black magic

A Python a web service

Can do a lot of black magic

Page 64: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Rigid

Page 65: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Flask

Page 66: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Page 67: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

from flask import Flaskapp = Flask(__name__)

@app.route(“/”)def hello(): return “Hello World!”

if __name__ == “__main__”: app.run()

Page 68: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Werkzeug, Jinja 2 and good intentions

Page 69: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Very small and easy to learn(super intuitive)

Page 70: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Solid and well documented

Page 71: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Awesome and strong community

Page 72: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Great debugging tools

Page 73: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Application dispatcher (on wsgi level)

Page 74: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Integrating unittest and integration tests (also check flask-testing)

Page 75: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Signals

Page 76: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Configuration == dict

Page 77: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Blueprints

Page 78: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Response object == wsgi app

Page 79: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

SHOW ME THE SOURCEYOU MUST

Page 80: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

BYOB

Page 81: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Some batteries

Flask-SQLAlchemy Flask-Testing

Flask-Oauthlib

Flask-Script

Flask-PrincipalFlask-Cache

Flask-Classy

Page 82: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Page 83: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Q/A

Page 84: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Resources

Kenneth ReitzFlask GoodnessAPI Driven DevelopmentHow I develop Things and Why?

Kevin LackerHow to Design Great APIs?

Page 85: A Python a web service - files.meetup.comfiles.meetup.com/8579852/PyTim #4 Vlad Temian - A Python Web Se… · A Python a web service Tools & Utilities management tools supporting

A Python a web service

Thank you!

DRINK ALL THE BEERS!