Skip to main content

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

pyhalboy-1.0.5.tar.gz (6.3 kB view details)

Uploaded Source

Built Distributions

pyhalboy-1.0.5-py3.11.egg (11.7 kB view details)

Uploaded Source

pyhalboy-1.0.5-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file pyhalboy-1.0.5.tar.gz.

File metadata

  • Download URL: pyhalboy-1.0.5.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.28.2 requests-toolbelt/0.9.1 tqdm/4.64.1 CPython/3.9.15

File hashes

Hashes for pyhalboy-1.0.5.tar.gz
Algorithm Hash digest
SHA256 46c26ba5abb5a584f95e80b26e645af2ef6b1431097a8cc6dd1277e9de586d36
MD5 fb4b1c3dc0cdc36cd51f5b718571c4df
BLAKE2b-256 cb81a33b7f9c87d6e59f2e6a711937c30b9621e01c623f5a0c6a2a95e34a3f47

See more details on using hashes here.

File details

Details for the file pyhalboy-1.0.5-py3.11.egg.

File metadata

  • Download URL: pyhalboy-1.0.5-py3.11.egg
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.28.2 requests-toolbelt/0.9.1 tqdm/4.64.1 CPython/3.9.15

File hashes

Hashes for pyhalboy-1.0.5-py3.11.egg
Algorithm Hash digest
SHA256 45ff85061e208b5a340412deedf123110a0033e8b566cc92d13ed6fb319c3c03
MD5 64b998eda4368dbbcb25a910a3e49f25
BLAKE2b-256 6d57fc56221bb6b2595d194c4b1876cb71e9e65d9a5a4b04891f3867d7fbd637

See more details on using hashes here.

File details

Details for the file pyhalboy-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: pyhalboy-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.28.2 requests-toolbelt/0.9.1 tqdm/4.64.1 CPython/3.9.15

File hashes

Hashes for pyhalboy-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7c6dca624d1116f0a7420e9e203ca1eb627e4c633954d7d5cae5a967fdf0fca4
MD5 9925d4e87dbd72b1c73a6ce2458ae72e
BLAKE2b-256 a8f968e0f600376b98cee2ce9befd2c4983f4743acd0e7c1d225ae4fd8e5e0ea

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page