Package: attribute_wrapper
AttributeWrapperThis wrapper maps attribute calls to HTTP API. The package provides similar
functionality asTortilla, but it is
implemented in smaller and simpler code.This simplicity gives you ability to subclass and modify functionality as you
need.ExamplesBasic accessfromattribute_wrapperimportHTTPWrapperr=HTTPWrapper("http://kitakitsune.org")data=r.get()This will download content ofhttp://kitakitsune.organd return it as string. The.get()call determines the HTTP method, which is used to perform the request.Attribute path compositionThe dot attribute access notation is used to compose the path of required HTTP resource. For example:r.raw.get()is translated to GET request forhttp://kitakitsune.org/raw.Special charactersThe dot notation in python is restricted only to A-Z, a-z, 0-9 and _, which may be sometimes too much restricting. Thats why theGenericWrapperhas the attribute.specials, which contains mappings of special sequences to URL characters.The.specialstable is at this moment implemented by following structure:{"__dot__":".","__slash__":"/","__dash__":"-",}Which means that URL containing this substrings will be translated as expected:r.raw.doctene_knihy__dot__txt.get()is translated tohttp://kitakitsune.org/raw/doctene_knihy.txtThis functionality can be changed by replacing.specialsdictionary table with something else, or with blank dictionary to disable it.Underscore methodMore complicated paths should be handled using_method:r._("compl?icated/$path/").get()which is translated tohttp://kitakitsune.org/compl?icated/$path/.Automatic suffixIf you work with API, which expects that each file ends with suffix like.jsonor.html, you can modify the.suffixattribute or addsuffixparameter when you are instancing the class:r=HTTPWrapper("http://kitakitsune.org",suffix=".txt")# this will return content of the http://kitakitsune.org, because the suffix# is ignored in root of the pathr.get()# this will return http://kitakitsune.org/raw/doctene_knihy.txtr.raw.doctene_knihy.get()ParametersThe method handling call at last attribute accepts keyword parameters, which are sent to server as form data:r.post(something="else")This will send{"something": "else"}dictionary as POST data to server.JSONWrapperAs example subclass, there is alsoJSONWrapper, which translates all parameters to JSON and send it as HTTPbodyto server. This may be specially useful with some of the REST API.SubclassingThe code is actually really simple (138 lines!) and it should be (at least I think) easy to understand. If you need some new functionality, you can just simply subclass theGenericWrapperclass and rewrite the.download_handler()method to reflect your needs.For example - theJSONWrapperis implemented in few lines of code:importjsonclassJSONWrapper(GenericWrapper):defdownload_handler(self,method,url,data):ifdata:data=json.dumps(data)headers={'content-type':'application/json'}resp=requests.request(method,url,headers=headers,data=data)# handle http errorsresp.raise_for_status()returnjson.loads(resp.text)Your codeFeel free to send pull request with you own classes (don’t forget to document it). I would like to make this package useful and I will gladly incorporate your code, so you don’t need to create your own package.InstallationThe code is hosted atPYPI,
and you can easily install it using the following command:sudopipinstallattribute_wrapperTestingThis project usespy.testfor testing. Just runpy.testfrom the root of the project:bystrousak:/h/D/c0d3z/python/libs/attribute_wrapper,0$ py.test
============================= test session starts ==============================
platform linux2 -- Python 2.7.6 -- py-1.4.23 -- pytest-2.6.0
collected 8 items

tests/test_wrapper.py ........

=========================== 8 passed in 0.12 seconds ===========================Changelog0.2.0Added full test coverage.Added ValueError exception when calling the root object without method.Fixed some typos in comments.Added testing section to README.rst.Added special underscore methodGenericWrapper._(path)for complicated paths.0.1.2Added handling of HTTP errors.0.1.1Fixed package metadata problems.0.1.0Project created.