一个基本的HTTP服务客户端:Demands
一个基本的HTTP服务客户端。
By default it "demands" successful responses from API endpoints, otherwise it raises an exception.
Demands accepts all the same parameters asrequests.requestand extends therequests.Sessionclass, documentation for both: Requests Developer Interface.
Written and used by the folks at Yola to support our free website builder.
HTTPServiceClient Overview
- base class for creating service clients
- provides flexible way of http error handling for descendants
-
HTTPServiceErrorraised on unexpected service response
-
Supports pre-send and post-send hooks
Usage from demands import HTTPServiceClient class MyService(HTTPServiceClient): def get_user(self, user_id): return self.get('/users/%s/' % user_id).json() def safe_get_user(self, user_id, default_user): response = self.get( '/users/%s/' % user_id, expected_response_codes=[404]) return response.json() if response.is_ok else default_user service = MyService(url='http://localhost/') user = service.get_user(1234)
Any parameters passed to the constructor will also be used for each and every request:
service = MyService( url='http://localhost/', headers={'h1':'value'}, auth=('username','pass'), ) # sent with auth and both headers user = service.get('/some-path', headers={'h2': 'kittens'})
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!