HALBoy port to python for everything HAL related
Project description
PyHalboy
A library for all things hypermedia. insipred by Halboy and Halboy.js
- Create hypermedia resources
- Marshal to and from plain python dicts
- Navigate JSON+HAL APIs
API
Resources
With PyHalboy you can create resources, and pull information from them.
from pyhalboy import Resource
discountResource = Resource()
.add_link('self', '/discounts/1256')
.add_property('discountPercentage', 10)
itemResources = [
Resource()
.add_link('self', '/items/534')
.add_property('price', 25.48)
]
resource = Resource()
.add_link('self', '/orders/123')
.add_link('creator', '/users/rob')
.add_resource('discount', discountResource)
.add_resource('items', itemResources)
.add_property('state', 'dispatching')
resource.get_link('self')
// { href: '/orders/123' }
resource.get_href('self')
// '/orders/123'
resource.get_property('state')
// 'dispatching'
resource
.get_resource('creator')
.get_property('discountPercentage')
// 10
resource
.get_resource('items')[0]
.get_property('price')
// 25.48
Marshalling
You can create HAL resources from plain JS objects, and vice versa.
from pyhalboy import Resource
itemResources = [
Resource()
.add_link('self', '/items/534')
.add_property('price', 25.48)
]
resource =
new Resource()
.add_link('self', '/orders/123')
.add_link('creator', '/users/rob')
.add_resource('items', itemResources)
.add_property('state', 'dispatching')
resource.to_object()
// {
// _links: {
// self: { href: '/orders/123' },
// creator: { href: '/users/rob' }
// },
// _embedded: {
// items: [{
// _links: {
// self: { href: '/items/534' }
// },
// price: 25.48
// }]
// },
// state: 'dispatching'
// }
Resource.from_object(resource.to_object())
.get_href('self')
// '/orders/123'
Navigation
Provided you're calling a HAL+JSON API, you can discover the API and navigate
through its links. When you've found what you want, you call
navigator.resource()
and you get a plain old HAL resource, which you can inspect
using any of the methods above.
from pyhalboy import Navigator
// GET / - 200 OK
// {
// "_links": {
// "self": {
// "href": "/"
// },
// "users": {
// "href": "/users"
// },
// "user": {
// "href": "/users/{id}",
// "templated": true
// }
// }
// }
discovery_result = Navigator.discover('https://api.example.com/')
users_result = discovery_result.get('users')
users_result.status()
// 200
users_result.location()
// 'https://api.example.com/users'
robResult = discovery_result.get('user', {id :'rob'})
robResult.location()
// 'https://api.example.com/users/rob'
sue_result = discovery_result.post('user', {
'id': 'sue',
'name': 'Sue',
'title': 'Dev'
})
sue_result.location()
// 'https://api.example.com/users/sue'
sue_result
.resource()
.get_property('title')
// 'Dev'
Contribution
Feel free to submit PRs and raise bugs.
publishing
python setup.py install sdist bdist_wheel
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file pyhalboy-1.0.2.tar.gz
.
File metadata
- Download URL: pyhalboy-1.0.2.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60a8a0f71e00c90f869e6b9ae7508d13116199150eebf64daeb30f2688c05b42 |
|
MD5 | a0d02c59a98e1cfd749bee275edf01b5 |
|
BLAKE2b-256 | 966abc644ed215af97c279a2b0f3df0f89922f4ad41545e16e64078e42f5bf08 |
File details
Details for the file pyhalboy-1.0.2-py3.9.egg
.
File metadata
- Download URL: pyhalboy-1.0.2-py3.9.egg
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb34325a9cf053f71da61e020911122f391f73421dd5034feb644e7621c5552f |
|
MD5 | dc321fe6e9b9a252479fbe868fae1e6b |
|
BLAKE2b-256 | 30ecaff759611bcc0b854827dea988f6265df2ee0b73f50265216a8d082e7e73 |
File details
Details for the file pyhalboy-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: pyhalboy-1.0.2-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2cd89b3e9810c0cb63092ed39ad6b2191ead76b901c3088c15c8e7ebb09dd54 |
|
MD5 | 987e1369d110d8a2bd930fae0f962354 |
|
BLAKE2b-256 | 63f173345485150c18be19ce71d8ffd4e56d983b7a5fe0b134c5a3ca6934fa23 |