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
HTTP API & Python
Search
Milan Cermak
February 20, 2013
Programming
3
260
HTTP API & Python
Slides from my talk at Brno and Prague Python user group meetups on HTTP/REST APIs.
Milan Cermak
February 20, 2013
Tweet
Share
More Decks by Milan Cermak
See All by Milan Cermak
Building a CI/CD pipeline on AWS
milancermak
0
5.4k
Designing mobile friendly APIs
milancermak
0
110
Programování pro iOS z pohledu pythonistu
milancermak
0
260
Prečo povedať nie SQL
milancermak
1
120
Other Decks in Programming
See All in Programming
テスト分析入門/Test Analysis Tutorial
goyoki
11
2.7k
衛星の軌道をWeb地図上に表示する
sankichi92
0
250
OpenTelemetryで始めるベンダーフリーなobservability / Vendor-free observability starting with OpenTelemetry
seike460
PRO
0
160
CRUD から CQRS へ ~ 分離が可能にする柔軟性
tkawae
0
220
Zennの運営完全に理解した #完全に理解したTalk
wadayusuke
1
140
AIにコードを生成するコードを作らせて、再現性を担保しよう! / Let AI generate code to ensure reproducibility
yamachu
7
6k
OpenNext + Hono on Cloudflare でイマドキWeb開発スタックを実現する
rokuosan
0
110
コンポーネントライブラリで実現する、アクセシビリティの正しい実装パターン
schktjm
1
660
Doma で目指す ORM 最適解
nakamura_to
1
160
primeNumberでのRBS導入の現在 && RBS::Traceでinline RBSを拡充してみた
mnmandahalf
0
250
「兵法」から見る質とスピード
ickx
0
180
バランスを見極めよう!実装の意味を明示するための型定義 TSKaigi 2025 Day2 (5/24)
whatasoda
2
770
Featured
See All Featured
For a Future-Friendly Web
brad_frost
178
9.7k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
52
2.8k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
357
30k
Optimizing for Happiness
mojombo
378
70k
Being A Developer After 40
akosma
91
590k
Intergalactic Javascript Robots from Outer Space
tanoku
271
27k
VelocityConf: Rendering Performance Case Studies
addyosmani
329
24k
Mobile First: as difficult as doing things right
swwweet
223
9.6k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.8k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.5k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.6k
Transcript
HTTP API & Py Milan Čermák Pyvo Praha, 20. 2.
2013 Wednesday, February 20, 13
What makes a good API? Wednesday, February 20, 13
Wednesday, February 20, 13
Consistency Wednesday, February 20, 13
Consistency Predictability Wednesday, February 20, 13
Consistency Predictability Adherence to standards Wednesday, February 20, 13
Consistency Predictability Adherence to standards Great documentation Wednesday, February 20,
13
Why HTTP? Wednesday, February 20, 13
"While HTTP isn’t always the best answer, it’s a damn
fine first guess." Coda Hale Wednesday, February 20, 13
Benefits of HTTP Wednesday, February 20, 13
Benefits of HTTP The most widespread application protocol Wednesday, February
20, 13
Benefits of HTTP The most widespread application protocol Statelessness Wednesday,
February 20, 13
Benefits of HTTP The most widespread application protocol Statelessness Optional
caching Wednesday, February 20, 13
Benefits of HTTP The most widespread application protocol Statelessness Optional
caching Promotes layered infrastructure Wednesday, February 20, 13
Benefits of HTTP The most widespread application protocol Statelessness Optional
caching Promotes layered infrastructure etc. Wednesday, February 20, 13
Limitations of HTTP Wednesday, February 20, 13
Limitations of HTTP Authorization Wednesday, February 20, 13
Limitations of HTTP Authorization Statelessness Wednesday, February 20, 13
Limitations of HTTP Authorization Statelessness Verbosity Wednesday, February 20, 13
Limitations of HTTP Authorization Statelessness Verbosity Crippled parallelism Wednesday, February
20, 13
"REST" if protocol.startswith("http") else "you're doing it wrong" Wednesday, February
20, 13
Set of architectural constraints Wednesday, February 20, 13
Set of architectural constraints Client-server Wednesday, February 20, 13
Set of architectural constraints Client-server Stateless Wednesday, February 20, 13
Set of architectural constraints Client-server Stateless Cacheable Wednesday, February 20,
13
Set of architectural constraints Client-server Stateless Cacheable Layered Wednesday, February
20, 13
Set of architectural constraints Client-server Stateless Cacheable Layered Uniform interface
* Wednesday, February 20, 13
How can Python help? Wednesday, February 20, 13
OOP Wednesday, February 20, 13
OOP URI ~ Class handler mapping Wednesday, February 20, 13
import handlers urls = [(r"/user", handlers.users.NewUser), (r"/user/(\d+)", handlers.users.User)] class User(handler.base.BaseHandler):
def delete(self, user_id): pass def get(self, user_id): pass def post(self, user_id): pass Wednesday, February 20, 13
class UserValidatorMixin(object): def check_user_data(self, user_dict): pass class User(handler.base.BaseHandler, UserValidatorMixin): def
post(self, user_id): new_user = self.get_argument(“user”) if not self.check_user_data(new_user): return self.http_error(400, “Invalid data”) Wednesday, February 20, 13
Middleware Wednesday, February 20, 13
Wednesday, February 20, 13
Extending JSONEncoder Wednesday, February 20, 13
import json class AppJSONEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, User):
return {"name": obj.name, "height": obj.height, "cash": obj.get_bank_account_balance()} return json.JSONEncoder.default(self, obj) user = User("1337") json.dumps(user, cls=AppJSONEncoder) Wednesday, February 20, 13
What about mobile? Wednesday, February 20, 13
Compression of HTTP bodies GET /user/1337 HTTP/1.1 Host: api.napyvo.io Accept-Encoding:
gzip, identity Wednesday, February 20, 13
Compression of HTTP bodies HTTP/1.1 200 OK Content-Encoding: gzip Content-Type:
application/json; charset=utf-8 Wednesday, February 20, 13
Compression of HTTP bodies POST /user HTTP/1.1 Host: api.napyvo.io Content-Encoding:
gzip Content-Type: application/json [gzipped representation of a user] Wednesday, February 20, 13
Caching Cache-Control: max-age=3600 Expires: Thu, 31 Jan 2013 22:00:00 GMT
<- Last-Modified: Thu, 31 Jan 2013 18:30:00 GMT -> If-Modified-Since: Wed, 30 Jan 2013 13:37:00 GMT ETag: foo If-None-Match: foo Wednesday, February 20, 13
Partial resources Wednesday, February 20, 13
GET /car/9 {"car": { "color": "red", "passengers": [ {"href": "/user/1337",
"rel": "self"}] } } Wednesday, February 20, 13
GET /car/9?zoom=passengers {"car": { "color": "red", "passengers": [ {"href": "/user/1337",
"rel": "self", "name": "Milan", "drink": "beer", "skills": ["python", "http"], }] } } Wednesday, February 20, 13
The promise of Hypermedia Wednesday, February 20, 13
Hypermedia as the engine of application state Wednesday, February 20,
13
Q & A Wednesday, February 20, 13