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 Distribution
File details
Details for the file pyhalboy-1.0.6.tar.gz
.
File metadata
- Download URL: pyhalboy-1.0.6.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee845619e87db0f07e71e8b60b2f19162bc637b0c74fce9c176195b28ab0330b |
|
MD5 | 86a431d69b5f0841f5fb14a59308365e |
|
BLAKE2b-256 | 2dc981a25f5c6cd9ca33562e26df9b959d9096608cbda80ca82a8733e580a5ac |
File details
Details for the file pyhalboy-1.0.6-py3-none-any.whl
.
File metadata
- Download URL: pyhalboy-1.0.6-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb8c297074c918285723129326239ff27dab43e39f681792ee0f99c3029ce5fa |
|
MD5 | 57947244e6b79e81571177ec234f5e6b |
|
BLAKE2b-256 | d36eb32c86e59cd04589aa5b74edf3d35ee3559a4135132b7904817df25fa3e4 |