Skip to main content

Library for build json API's

Project description

PyPI PyPI - Python Version Codecov Build GitHub Requirements Status

Build json API fast and simple

Key Features

  • Provides simple API.

  • Supports serialize/deserialize class objects to/from dictionary.

  • Field type may be the same with model type (self_base Field attribute)

Installation

pip install botapi

Getting started

Let’s take some data:

order = {
    'user': {
        'name': 'Jack',
        'surname': 'Doe',
        'phone': '123456789',
    },
    'date': '2020-12-10 10:12:13',
    'paid': True,
    'items': [
        {
            'name': 'product 1',
            'id': 1,
            'quantity': 2,
            'subtotal': 10.5
        },
        {
            'name': 'product 2',
            'id': 2,
            'quantity': 1,
            'subtotal': 5
        }
    ]
}

Write models:

from datetime import datetime

from botapi import Model, Field, ListField

class Item(Model):
    name = Field()
    item_id = Field(alias='id')


# inherit model
class CartItem(Item):
    quantity = Field(base=int)
    subtotal = Field()


class UserModel(Model):
    name = Field()
    surname = Field()
    phone = Field()


class OrderModel(Model):
    user = Field(base=UserModel)
    paid = Field(base=bool, default=False)
    cart = ListField(item_base=CartItem, default=[], alias='items')
    order_date = DateTimeField()

Deserialize and work with data:

# deserialize data
obj = OrderModel(**order)

# work with data
obj.user.name = 'John'
obj.paid = True
obj.cart[0].subtotal = 12.5
obj.order_date = datetime.now()

Serialize model:

# may be you want to add some data
comment = 'call before delivery'

# serialize data
print(obj.serialize(data_to_update={'comment': comment}))

Output:

{'user': {'surname': 'Doe', 'phone': '123456789', 'name': 'John'}, 'paid': True, 'items': [{'quantity': 2, 'subtotal': 12.5, 'id': 1, 'name': 'product 1'}, {'quantity': 1, 'subtotal': 5, 'id': 2, 'name': 'product 2'}], 'order_date': '2020-12-22 12:04:39', 'comment': 'call before delivery'}

Requirements

License

BotAPI is distributed under the Apache License 2.0 license.

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

botapi-1.2.1.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

botapi-1.2.1-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file botapi-1.2.1.tar.gz.

File metadata

  • Download URL: botapi-1.2.1.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for botapi-1.2.1.tar.gz
Algorithm Hash digest
SHA256 db61c1967d77e9c0a6c5a727c1a28c9663b1ef5192af0d118225337f86d2f0af
MD5 9538d75ab1c9acc9722ab955e2cc7cdf
BLAKE2b-256 ef85cabe47951978150c616d73e3070cb8c68dad158d7a0bb9e289b98b8e0a73

See more details on using hashes here.

File details

Details for the file botapi-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: botapi-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for botapi-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ca99f66cd9627858c505fe2ed8180e04cdef530d7ecd4a185026d2a7ab708a7c
MD5 3b63730053c4cbe53369c2e67132a07f
BLAKE2b-256 e4bd7132e3a6900b06e5e57a104c738ae84f900220ed81115dbfd92a64b9490e

See more details on using hashes here.

Supported by

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