Provides python api to SN and messengers
Project description
Build json API fast and simple
Key Features
Provides simple API.
Supports serialize/deserialize class objects to/from dictionary.
Installation
pip install botapi
Getting started
Let’s take some data:
order = {
'user': {
'name': 'Jack',
'surname': 'Doe',
'phone': '123456789',
},
'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 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')
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
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:
{'paid': True, 'items': [{'name': 'product 1', 'quantity': 2, 'id': 1, 'subtotal': 12.5}, {'name': 'product 2', 'quantity': 1, 'id': 2, 'subtotal': 5}], 'user': {'name': 'John', 'surname': 'Doe', 'phone': '123456789'}, '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.1.0.tar.gz
(8.2 kB
view hashes)
Built Distribution
botapi-1.1.0-py3-none-any.whl
(11.8 kB
view hashes)