79 lines
1.9 KiB
Plaintext
79 lines
1.9 KiB
Plaintext
The Capabilities component
|
|
==========================
|
|
|
|
The Capabilities component basically gives us two objects: a Capability
|
|
and a SeedCapability.
|
|
|
|
We can instaniate a SeedCapability like this:
|
|
>>> from pyogp.lib.base.caps import SeedCapability, Capability
|
|
>>> seed = SeedCapability('seed', 'http://127.0.0.1:12345/seed_cap')
|
|
|
|
We assume that we got the seed capability URL itself from login or some other service.
|
|
|
|
We can now ask this SeedCapability object for new capabilities:
|
|
>>> caps = seed.get(['some_capability', 'some_other'])
|
|
|
|
The result is a dictionary object:
|
|
>>> len(caps.keys())
|
|
2
|
|
|
|
Let's store the some_capability cap in a variable:
|
|
>>> some_cap = caps['some_capability']
|
|
|
|
The capability now can be simply called with a payload and returns some data itself.
|
|
|
|
First we call it:
|
|
>>> data = some_cap.POST({'a':'b'})
|
|
|
|
And now we can check the data:
|
|
>>> data['something']
|
|
'else'
|
|
>>> data['some']
|
|
12345
|
|
|
|
This data here is provided by the mockup server for testing.
|
|
|
|
Internals
|
|
=========
|
|
|
|
Each capability stores it's name and public URL which it is instantiated with.
|
|
We can access these like this:
|
|
|
|
>>> some_cap.name
|
|
'some_capability'
|
|
|
|
>>> some_cap.public_url
|
|
'http://localhost:12345/cap/some_capability'
|
|
|
|
As we can see, it's not a secret URL in this mockup case but in production it will be.
|
|
|
|
|
|
|
|
Testing errors
|
|
==============
|
|
|
|
Now we can test what happens to our code when the server returns a wrong content type.
|
|
In this case it should not find a deserializer and say so::
|
|
|
|
>>> seed = SeedCapability('seed', 'http://127.0.0.1:12345/seed_cap_wrong_content_type')
|
|
>>> cap = seed.get(['some_capability'])
|
|
Traceback (most recent call last):
|
|
...
|
|
deserialization for text/foobar not supported
|
|
|
|
|
|
Now we test if network errors are handled correctly::
|
|
|
|
>>> cap = Capability('test','http://127.0.0.1:12345/cap/error')
|
|
>>> cap.POST({'test':'testing'})
|
|
Traceback (most recent call last):
|
|
...
|
|
HTTPError: 500 Internal Server Error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|