Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
REST w Pyramidzie
Search
Łukasz Oleś
January 16, 2013
0
60
REST w Pyramidzie
Łukasz Oleś
January 16, 2013
Tweet
Share
More Decks by Łukasz Oleś
See All by Łukasz Oleś
Jak napisać dobre CV? Historia zatrudniania w Mirantis PL
lukaszo
0
97
Fuel - czyli jak stworzyć własną chmurę w ciągu godziny
lukaszo
0
170
Featured
See All Featured
Building AI with AI
inesmontani
PRO
1
700
The agentic SEO stack - context over prompts
schlessera
0
640
YesSQL, Process and Tooling at Scale
rocio
174
15k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
220
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
62
50k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Principles of Awesome APIs and How to Build Them.
keavy
128
17k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
220
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
250
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.1k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
330
Transcript
REST w Pyramidzie Łukasz Oleś
None
Prostota Minimalizm Dokumentacja Szybkość Stabilność
Działa na: • Python 2.6 – 3.3 • PyPy •
Jython • GAE
REST http://autouzupelnij.pl GET http://api.autouzupelnij.pl/v1/public/city?term=gli
REST (…)/public/city?term=gli Nazwa konta (…)/public/city?term=gli
REST (…)/public/city?term=gli Nazwa konta Kontener
REST (…)/public/city?term=gli Nazwa konta Zapytanie Kontener
Nowy projekt pcreate -t starter api \api development.ini setup.py \api
__init__.py views.py
Plik api/__init__.py from pyramid.config import Configurator def 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
Plik api/views.py from 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
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)
Nowy element Plik api/__init__.py config.add_route('items', '/v1/{account}/{container}/{item}')
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)
Walidacja - dodatkowe dane { ”population” : 20000, ”province” :
”slaskie” }
Colander http://docs.pylonsproject.org/projects/colander from colander import MappingSchema, SchemaNode, Int, String class
City(MappingSchema): province = SchemaNode(String()) population = SchemaNode(Int())
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)
@view_config(context=colander.Invalid) def failed_valdation(exc, request): err = json.dumps(exc.asdict()) return HTTPBadRequest(err)
Autoryzacja http://docs.pylonsproject.org/projects/pyramid/en/ latest/tutorials/wiki2/authorization.html https://ziggurat-foundations.readthedocs.org
Plik api/__init__.py from api.auth import RootFactory, auth_check def main(global_config, **settings):
config = Configurator(settings=settings, root_factory=RootFactory, authentication_policy= AuthenticationPolicy(auth_check))
COSR – poziom zaawansowany JavaScript Ajax http://autouzupelnij.pl -> http://api.autouzupelnij.pl http://en.wikipedia.org/wiki/Cross-origin_resource_sharing
Access-Control-Allow-Origin: http://autouzupelnij.pl Access-Control-Allow-Origin: * Access-Control-Allow-Methods: POST, GET Access-Control-Allow-Headers: Content-Type COSR
– poziom zaawansowany
Plik api/__init__.py from pyramid.events import subscriber, NewResponse @subscriber(NewResponse) def add_access_list(event):
event.response.headerlist.append( ('Access-Control-Allow-Origin', '*')) COSR – poziom zaawansowany
Co dalej? Cornice http://cornice.readthedocs.org/
Pytania Łukasz Oleś
[email protected]
Dziękuję Łukasz Oleś
[email protected]