一个流畅的python对象,用于与restfuljsonapi交互。
RestResponse的Python项目详细描述
resresponse
resresponse旨在成为一个流畅的python对象,用于与restful json api接口
安装
将这一行添加到应用程序的需求中。txt:
RestResponse
然后执行:
$ pip install -r requirements.txt
或自行安装为:
$ pip install RestResponse
用法
导入resresponse并对任何列表、dict或原语调用parse。您还可以调用序列化json上的加载
>>>importRestResponse>>>importrequests>>>r=requests.get('http://jsonplaceholder.typicode.com/users')>>>users=RestResponse.parse(r.json())>>># or...users=RestResponse.loads(r.text)>>>foruserinusers:...printuser.name...LeanneGrahamErvinHowellClementineBauchPatriciaLebsackChelseyDietrichMrs.DennisSchulistKurtisWeissnatNicholasRunolfsdottirVGlennaReichertClementinaDuBuque>>>user=users[0]>>>printuser.pretty_print(indent=4){"username":"Bret","website":"hildegard.org","name":"Leanne Graham","company":{"bs":"harness real-time e-markets","name":"Romaguera-Crona","catchPhrase":"Multi-layered client-server neural-net"},"id":1,"phone":"1-770-736-8031 x56442","address":{"suite":"Apt. 556","street":"Kulas Light","geo":{"lat":"-37.3159","lng":"81.1496"},"zipcode":"92998-3874","city":"Gwenborough"},"email":"Sincere@april.biz"}>>>user.name='Rest Response'>>>r=requests.put('http://jsonplaceholder.typicode.com/users/{0}'.format(user.id),json=user)>>># or if you choose to NOT allow by overriding>>># json._default_encoder = RestEncoder()>>># simplejson._default_encoder = RestEncoder()>>># use __call__ on RestResponseObj to explicitly dump and load via RestEncoder>>>r=requests.put('http://jsonplaceholder.typicode.com/users/{0}'.format(user.id),json=user())# Note __call__ on user>>>response=RestResponse.parse(r.json())>>>printresponse.nameRestResponse>>>new=RestResponse.parse({})>>>new.id=5>>>ifnotnew.username:...new.username='New User'...>>>new.address.geo.lat="-42.3433">>>new.address.geo.lng="74.3433">>>new.email='someone@somewhere.biz'>>>printnew.pretty_print(){"username":"New User","email":"someone@somewhere.biz","id":5,"address":{"geo":{"lat":"-42.3433","lng":"74.3433"}}}
可调用函数
可调用属性将通过cloudpickle和base64编码,前缀为\u callable\uuu:
>>>importRestResponse>>>data=RestResponse.parse({'callable':lambdax:x+1})>>>data.callable<function<lambda>at0x7f92593de9b0>>>>data.callable(1)2>>>pretty_str=data.pretty_print()>>>printpretty_str{"callable":"__callable__: gAJjY2xvdWRwaWNrbGUuY2xvdWRwaWNrbGUKX2ZpbGxfZnVuY3Rpb24KcQAoY2Nsb3VkcGlja2xlLmNsb3VkcGlja2xlCl9tYWtlX3NrZWxfZnVuYwpxAWNjbG91ZHBpY2tsZS5jbG91ZHBpY2tsZQpfYnVpbHRpbl90eXBlCnECVQhDb2RlVHlwZXEDhXEEUnEFKEsBSwFLAktDVQh8AABkAQAXU3EGTksBhnEHKVUBeHEIhXEJVQc8c3RkaW4+cQpVCDxsYW1iZGE+cQtLAVUAcQwpKXRxDVJxDkr/////VQhfX21haW5fX3EPh3EQUnERfXESKFUEbmFtZXETaAtVA2RvY3EUTlUGbW9kdWxlcRVoD1UOY2xvc3VyZV92YWx1ZXNxFk5VB2dsb2JhbHNxF31xGFUEZGljdHEZfXEaVQhkZWZhdWx0c3EbTnV0Ui4="}>>>data=RestResponse.loads(pretty_str)>>>data.callable(1)2
二进制
检测为二进制数据的属性将通过前缀为\u binary\:
的base64进行编码
>>>importRestResponse>>>importrequests>>>data=RestResponse.parse({...'binary':requests.get('https://cataas.com/cat').content...})>>>data.binary'\xff\xd8\xff\xdb\x00C\x00\x06\x04\x05\x06\x05\x04\x06\x06\x05\x06\x07\x07\x06\x08\n\x10\n\n\t\t\n\x14\x0e\x0f\x0c\x10\x17\x14\x18\x18\x17\x14\x16\x16\x1a\x1d%\x1f\x1a\x1b#\x1c\x16\x16 , #&\')*)\x19\x1f-0-(0%()(\xff\xdb\x00C\x01\x07\x07\x07\n\x08\n\x13\n\n\x13(\x1a\x16\x1a((((((((((((((((((((((((((((((((((((((((((((((((((\xff\xc2\x00\x11\x08\x00\x01\x00\x01\x03\x01"\x00\x02\x11\x01\x03\x11\x01\xff\xc4\x00\x15\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\xff\xc4\x00\x14\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xff\xda\x00\x0c\x03\x01\x00\x02\x10\x03\x10\x00\x00\x01\x90\x07\xff\xc4\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x08\x01\x01\x00\x01\x05\x02\x7f\xff\xc4\x00\x14\x11\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x08\x01\x03\x01\x01?\x01\x7f\xff\xc4\x00\x14\x11\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x08\x01\x02\x01\x01?\x01\x7f\xff\xc4\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x08\x01\x01\x00\x06?\x02\x7f\xff\xc4\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x08\x01\x01\x00\x01?!\x7f\xff\xda\x00\x0c\x03\x01\x00\x02\x00\x03\x00\x00\x00\x10\xfb\xff\xc4\x00\x14\x11\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x08\x01\x03\x01\x01?\x10\x7f\xff\xc4\x00\x14\x11\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x08\x01\x02\x01\x01?\x10\x7f\xff\xc4\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x08\x01\x01\x00\x01?\x10\x7f\xff\xd9'>>>pretty_str=data.pretty_print()>>>printpretty_str{"binary":"__binary__: /9j/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wgARCAABAAEDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAT/xAAUAQEAAAAAAAAAAAAAAAAAAAAB/9oADAMBAAIQAxAAAAGQB//EABQQAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQEAAQUCf//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQMBAT8Bf//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQIBAT8Bf//EABQQAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQEABj8Cf//EABQQAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQEAAT8hf//aAAwDAQACAAMAAAAQ+//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQMBAT8Qf//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQIBAT8Qf//EABQQAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQEAAT8Qf//Z"}>>>data=RestResponse.loads(pretty_str)>>>data.binary'\xff\xd8\xff\xdb\x00C\x00\x06\x04\x05\x06\x05\x04\x06\x06\x05\x06\x07\x07\x06\x08\n\x10\n\n\t\t\n\x14\x0e\x0f\x0c\x10\x17\x14\x18\x18\x17\x14\x16\x16\x1a\x1d%\x1f\x1a\x1b#\x1c\x16\x16 , #&\')*)\x19\x1f-0-(0%()(\xff\xdb\x00C\x01\x07\x07\x07\n\x08\n\x13\n\n\x13(\x1a\x16\x1a((((((((((((((((((((((((((((((((((((((((((((((((((\xff\xc2\x00\x11\x08\x00\x01\x00\x01\x03\x01"\x00\x02\x11\x01\x03\x11\x01\xff\xc4\x00\x15\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\xff\xc4\x00\x14\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xff\xda\x00\x0c\x03\x01\x00\x02\x10\x03\x10\x00\x00\x01\x90\x07\xff\xc4\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x08\x01\x01\x00\x01\x05\x02\x7f\xff\xc4\x00\x14\x11\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x08\x01\x03\x01\x01?\x01\x7f\xff\xc4\x00\x14\x11\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x08\x01\x02\x01\x01?\x01\x7f\xff\xc4\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x08\x01\x01\x00\x06?\x02\x7f\xff\xc4\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x08\x01\x01\x00\x01?!\x7f\xff\xda\x00\x0c\x03\x01\x00\x02\x00\x03\x00\x00\x00\x10\xfb\xff\xc4\x00\x14\x11\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x08\x01\x03\x01\x01?\x10\x7f\xff\xc4\x00\x14\x11\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x08\x01\x02\x01\x01?\x10\x7f\xff\xc4\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x08\x01\x01\x00\x01?\x10\x7f\xff\xd9'
不支持
需要注意的是,缺少引用属性(包括嵌套属性)是错误的。
>>>importRestResponse>>>data=RestResponse.parse({})>>>data.property.is_noneNone>>>bool(data.property.is_none)False>>>isinstance(data.property.is_none,RestResponse.NoneProp)True>>>'some data'indata.property.is_noneFalse>>>[xforxindata.property.is_none][]>>>data.property.is_none=None>>>isinstance(data.property.is_none,RestResponse.NoneProp)False>>>printdata.pretty_print(){"property":{"is_none":null}}
sqlalchemy orm
resresponse使用sqlalchemy提供的可变mixin与数据库交互。以下(烧瓶)片段将帮助您开始:
fromflaskimportFlaskfromflask_sqlalchemyimportSQLAlchemyfromRestResponse.orm.sqlalchemyimportRESTResponseapp=Flask(__name__)db=SQLAlchemy(app)classSomeModel(db.Model):id=db.Column(db.Integer(),primary_key=True)data=db.Column(RESTResponse(),nullable=False)def__init__(self,data):self.data=data# data should be json serializable
数据将作为序列化的json blob保存到数据库中。当数据被加载时,它将被强制到底层responseobj
APIModel
继承resresponse.apimodel以便于键入api模型。请注意,您的属性不应该以/code>开头。
apimodel.\u data
setter确保传入的数据符合您的属性,有效地过滤掉通过dir()
标识的带下划线的属性。此行为可以通过向\u opts\u[''u overrides']
添加属性来覆盖(注意:\u opts\uu
必须在设置\u数据之前更新)。
为了方便起见,apimodel包含用于属性完整性的助手方法。其中包括:
设置int
,获取int
设置字符串
,获取字符串
设置日期
,获取日期
设置日期时间
,获取日期时间
设置浮点
,获取浮点
设置bool
,获取bool
< > >
您可以使用其中一个帮助程序(或定义自己的帮助程序)或另一个对象(如下面的reference
)初始化apicollection。
请考虑以下片段:
fromRestResponseimportApiModel,ApiCollectionclassModel(ApiModel):def__init__(self,data):self.__opts__['_overrides']=['_bar']self._data=data# this _foo property will be ignored on init@propertydef_foo(self):returnself._get_string(self._data._foo)@_foo.setterdef_foo(self,_foo):self._data._foo=self._set_string(_foo)@propertydef_bar(self):returnself._get_string(self._data._bar)@_bar.setterdef_bar(self,_bar):self._data._bar=self._set_string(_bar)@propertydefid(self):returnself._get_int(self._data.id)@id.setterdefid(self,id):self._data.id=self._set_int(id)@propertydefreference(self):ifnotself._data.reference:self._data.reference=Reference()returnReference(self._data.reference)@reference.setterdefreference(self,reference):self._data.reference=Reference(reference)@propertydefint_collection(self):ifnotself._data.int_collection:self._data.int_collection=ApiCollection(self._set_int)returnself._data.int_collection@int_collection.setterdefint_collection(self,int_collection):self._data.int_collection=ApiCollection(self._set_int)self._data.int_collection.extend(int_collection)@propertydefref_collection(self):ifnotself._data.ref_collection:self._data.ref_collection=ApiCollection(Reference)returnself._data.ref_collection@ref_collection.setterdefref_collection(self,ref_collection):self._data.ref_collection=ApiCollection(Reference)self._data.ref_collection.extend(ref_collection)classReference(ApiModel):def__init__(self,data):self._data=data@propertydefid(self):returnself._get_int(self._data.id)@id.setterdefid(self,id):self._data.id=self._set_int(id)
然后使用restobject、dict、apimodel或序列化json进行初始化
RestResponse
0
注意:默认情况下,apimodel不会解码/编码二进制/可调用类型。您需要通过重写apimodel来启用此行为。
RestResponse
1
测试
RestResponse
2
贡献
欢迎在github上通过https://github.com/tysonholub/restresponse.git" rel="nofollow">https://github.com/tysonholub/restresponse.git提交错误报告和请求。这个项目是 旨在成为一个安全、受欢迎的协作空间,并且希望贡献者遵守 贡献者契约行为准则。
许可证
根据麻省理工学院许可证的条款,此软件包作为开源软件提供