Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
REST w Pyramidzie              Łukasz Oleś
Prostota MinimalizmDokumentacja  Szybkość Stabilność
Działa na:●Python 2.6 – 3.3●PyPy●Jython●GAE
REST              http://autouzupelnij.plGEThttp://api.autouzupelnij.pl/v1/public/city?term=gli
REST         (…)/public/city?term=gliNazwa konta
REST         (…)/public/city?term=gliNazwa konta      Kontener
REST         (…)/public/city?term=gliNazwa konta      Kontener           Zapytanie
Nowy projekt      pcreate -t starter apiapi  development.ini  setup.py  api    __init__.py    views.py
Plik api/__init__.pyfrom pyramid.config import Configuratordef main(global_config, **settings):  config = Configurator(set...
Plik api/views.pyfrom pyramid.view import view_config@view_config(route_name=completion,               renderer=json,     ...
Plik api/views.py@view_config(route_name=completion,               renderer=json,               request_method=DELETE,    ...
Nowy elementPlik api/__init__.pyconfig.add_route(items,                /v1/{account}/{container}/{item})
Plik api/views.py@view_config(route_name=items,               renderer=json,               request_method=PUT,            ...
Walidacja - dodatkowe dane    {    ”population” : 20000,    ”province” : ”slaskie”    }
Colander       http://docs.pylonsproject.org/projects/colanderfrom colander import MappingSchema,                     Sche...
Plik api/views.py@view_config(route_name=items,               renderer=json,               request_method=PUT,            ...
@view_config(context=colander.Invalid)def failed_valdation(exc, request):  err = json.dumps(exc.asdict())  return HTTPBadR...
Autoryzacjahttp://docs.pylonsproject.org/projects/pyramid/en/       latest/tutorials/wiki2/authorization.html  https://zig...
Plik api/__init__.pyfrom api.auth import RootFactory, auth_checkdef main(global_config, **settings):  config = Configurato...
COSR – poziom zaawansowany     http://en.wikipedia.org/wiki/Cross-origin_resource_sharing                 JavaScript Ajaxh...
COSR – poziom zaawansowanyAccess-Control-Allow-Origin: http://autouzupelnij.plAccess-Control-Allow-Origin: *Access-Control...
COSR – poziom zaawansowanyPlik api/__init__.pyfrom pyramid.events import subscriber,                          NewResponse@...
Co dalej?          Cornicehttp://cornice.readthedocs.org/
Pytania          Łukasz Oleś          lukaszoles@gmail.com
Dziękuję       Łukasz Oleś       lukaszoles@gmail.com
Pyramid REST
Upcoming SlideShare
Loading in …5
×

Pyramid REST

4,016 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Pyramid REST

  1. 1. REST w Pyramidzie Łukasz Oleś
  2. 2. Prostota MinimalizmDokumentacja Szybkość Stabilność
  3. 3. Działa na:●Python 2.6 – 3.3●PyPy●Jython●GAE
  4. 4. REST http://autouzupelnij.plGEThttp://api.autouzupelnij.pl/v1/public/city?term=gli
  5. 5. REST (…)/public/city?term=gliNazwa konta
  6. 6. REST (…)/public/city?term=gliNazwa konta Kontener
  7. 7. REST (…)/public/city?term=gliNazwa konta Kontener Zapytanie
  8. 8. Nowy projekt pcreate -t starter apiapi development.ini setup.py api __init__.py views.py
  9. 9. Plik api/__init__.pyfrom pyramid.config import Configuratordef main(global_config, **settings): config = Configurator(settings=settings) config.add_route(completion, /v1/{account}/{container}) config.scan() return config.make_wsgi_app()pserve development.ini
  10. 10. Plik api/views.pyfrom pyramid.view import view_config@view_config(route_name=completion, renderer=json, request_method=GET)def completion(request): account = request.matchdict[account] container = request.matchdict[container] query = request.params.get(term, u) storage = get_storage(account) offers = storage.suggest(container, query)) return offers
  11. 11. Plik api/views.py@view_config(route_name=completion, renderer=json, request_method=DELETE, permission=edit)def delete_container(request): container = request.matchdict[container] account = request.matchdict[account] storage = get_storage( account) storage.delete_container(container)
  12. 12. Nowy elementPlik api/__init__.pyconfig.add_route(items, /v1/{account}/{container}/{item})
  13. 13. Plik api/views.py@view_config(route_name=items, renderer=json, request_method=PUT, permission=edit)def add_item(request): container = request.matchdict[container] account = request.matchdict[account] item = request.matchdict[item] storage = get_storage(account) storage.store_item(container, item)
  14. 14. Walidacja - dodatkowe dane { ”population” : 20000, ”province” : ”slaskie” }
  15. 15. Colander http://docs.pylonsproject.org/projects/colanderfrom colander import MappingSchema, SchemaNode, Int, Stringclass City(MappingSchema): province = SchemaNode(String()) population = SchemaNode(Int())
  16. 16. Plik api/views.py@view_config(route_name=items, renderer=json, request_method=PUT, permission=edit)def add_item(request): container = request.matchdict[container] account = request.matchdict[account] item = request.matchdict[item] city = City() data = city.deserialize(request.json_body) storage = get_storage(account) storage.store_item(container, item, data)
  17. 17. @view_config(context=colander.Invalid)def failed_valdation(exc, request): err = json.dumps(exc.asdict()) return HTTPBadRequest(err)
  18. 18. Autoryzacjahttp://docs.pylonsproject.org/projects/pyramid/en/ latest/tutorials/wiki2/authorization.html https://ziggurat-foundations.readthedocs.org
  19. 19. Plik api/__init__.pyfrom api.auth import RootFactory, auth_checkdef main(global_config, **settings): config = Configurator(settings=settings, root_factory=RootFactory, authentication_policy= AuthenticationPolicy(auth_check))
  20. 20. COSR – poziom zaawansowany http://en.wikipedia.org/wiki/Cross-origin_resource_sharing JavaScript Ajaxhttp://autouzupelnij.pl -> http://api.autouzupelnij.pl
  21. 21. COSR – poziom zaawansowanyAccess-Control-Allow-Origin: http://autouzupelnij.plAccess-Control-Allow-Origin: *Access-Control-Allow-Methods: POST, GETAccess-Control-Allow-Headers: Content-Type
  22. 22. COSR – poziom zaawansowanyPlik api/__init__.pyfrom pyramid.events import subscriber, NewResponse@subscriber(NewResponse)def add_access_list(event): event.response.headerlist.append( (Access-Control-Allow-Origin, *))
  23. 23. Co dalej? Cornicehttp://cornice.readthedocs.org/
  24. 24. Pytania Łukasz Oleś lukaszoles@gmail.com
  25. 25. Dziękuję Łukasz Oleś lukaszoles@gmail.com

×