ajtowns / beanbag Goto Github PK
View Code? Open in Web Editor NEWHelper module for accessing REST interfaces from Python
License: Other
Helper module for accessing REST interfaces from Python
License: Other
When get the resource by BeanBag's attr and it includes a "-" char, should BeanBag support this kind of resource or raise a clearly error info?
(master)mwhudson@aeglos:/opt/opensource/deb/beanbag$ PYTHONPATH=. python3.6 `which py.test-3`
======================================================== test session starts =========================================================
platform linux -- Python 3.6.1, pytest-2.8.7, py-1.4.31, pluggy-0.3.1
rootdir: /opt/opensource/deb/beanbag, inifile:
collected 4 items
tests/test_attrdict.py .
tests/test_bbv1.py .
tests/test_bbv2.py .F
============================================================== FAILURES ==============================================================
_______________________________________________________ test_sane_inheritance ________________________________________________________
def test_sane_inheritance():
> class MyBeanBag(BeanBag):
E TypeError: __class__ set to <class 'test_bbv2.test_sane_inheritance.<locals>.MyBeanBagBase'> defining 'MyBeanBag' as <class 'test_bbv2.test_sane_inheritance.<locals>.MyBeanBag'>
tests/test_bbv2.py:44: TypeError
================================================= 1 failed, 3 passed in 0.20 seconds =================================================
I haven't looked into this at all!
While trying to access gitlab api and dealing with pagination, i need access to the resonse headers.
As it looks from the code the response header is thrown away in the decode() function.
Is there a workaround i can use?
I was trying to simply subclass BeanBag but that complained about missing:
AttributeError: 'MyBeanBagBase' object has no attribute 'base_url'
Kind regards
Hauke
Python 2.7.3 (default, Jan 2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import beanbag, pdb; g = beanbag.BeanBag("https://api.github.com/rubbish")
>>> g.rubbish1()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/beanbag/__init__.py", line 159, in __call__
return self.__bbr.make_request(verb, self.__path, kwargs, body)
File "/usr/local/lib/python2.7/dist-packages/beanbag/__init__.py", line 285, in make_request
% (r.status_code, r.reason),
AttributeError: 'Response' object has no attribute 'reason'
>>> pdb.pm()
> /usr/local/lib/python2.7/dist-packages/beanbag/__init__.py(285)make_request()
-> % (r.status_code, r.reason),
(Pdb) p dir(r)
['__bool__', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_content', '_content_consumed', 'config', 'content', 'cookies', 'encoding', 'error', 'headers', 'history', 'iter_content', 'iter_lines', 'json', 'ok', 'raise_for_status', 'raw', 'request', 'status_code', 'text', 'url']
(Pdb) p r.status_code
404
Apparently a request doesn't have a r.reason on a 404. (I've not used request, so I don't know why not.)
The reason is that py
is no longer bundled in pytest
which now contains only a subset of the py package.
In setup.py we have: install_requires = ['requests >= 0.12.1']
.
I think you meant install_requires = ['requests >= 1.0.1']
.
If one sets session.verify=False
subsequent request()
calls of request module - session.request doesn't take this into account. Apparently one needs to set verify as a request()
attribute.
So url_v1.py
could be fixed with
r = self.session.request(verb, path, params=params, data=ebody, verify=self.session.verify)
Some REST frame works require changing the mime type for every request (yes, this does seem dodgy). However there doesn't seem to be a good way to do this with beanbag. For now I have used the ugly solution:
(~gh)[0].mime_json = "application/vnd.com.nsn.cumulocity.managedObjectCollection+json"
Python 2.7.3 (default, Jan 2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import beanbag, pdb; g = beanbag.BeanBag("https://api.github.com")
>>> g.repos.ajtowns.beanbag()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/beanbag/__init__.py", line 159, in __call__
return self.__bbr.make_request(verb, self.__path, kwargs, body)
File "/usr/local/lib/python2.7/dist-packages/beanbag/__init__.py", line 296, in make_request
r, (verb, path, params, ebody))
beanbag.BeanBagException: Could not decode response
When you report the error (ie, in the raise BeanBagException()), include the reason from the inner exception in the text (ie, str(sys.exc_info()[1])).
For some complicated rest APIs , it is necessarily to be able to access the original JSON data. E.g. so the entire response can be stored in a JSONField in the database. It looks like this data is stored in the AttrDict as base
, but this value is not accessible.
Set BeanBag as a base class, can't use the super() function, the base class came to MyClassBase.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.