add/remove to/from cart
This commit is contained in:
parent
fe6246332f
commit
d2a37d2f59
3 changed files with 110 additions and 2 deletions
|
|
@ -1,15 +1,17 @@
|
|||
from apiclient import APIClient, endpoint, JsonResponseHandler, HeaderAuthentication
|
||||
import uuid
|
||||
from apiclient import APIClient, endpoint, JsonRequestFormatter, JsonResponseHandler, HeaderAuthentication
|
||||
from apiclient_pydantic import serialize
|
||||
|
||||
from .models.order import Order, Orders
|
||||
from .models.product import Product
|
||||
from .models.recipe import Recipe
|
||||
from .models.cart import Cart, AddItem, Add
|
||||
from .endpoints import Endpoints
|
||||
|
||||
|
||||
class Rohlik(APIClient):
|
||||
def __init__(self, api_key: str):
|
||||
super().__init__(response_handler=JsonResponseHandler, authentication_method=HeaderAuthentication(token=api_key,parameter="x-api-authorization",scheme="token"))
|
||||
super().__init__(request_formatter=JsonRequestFormatter, response_handler=JsonResponseHandler, authentication_method=HeaderAuthentication(token=api_key,parameter="x-api-authorization",scheme="token"))
|
||||
|
||||
@serialize()
|
||||
def get_product(self, product_id: int) -> Product:
|
||||
|
|
@ -32,3 +34,35 @@ class Rohlik(APIClient):
|
|||
def get_recipe(self, recipe_id: int) -> Recipe:
|
||||
recipe_response = self.get(Endpoints.recipe.format(recipe_id=recipe_id))
|
||||
return recipe_response['data']
|
||||
|
||||
@serialize()
|
||||
def get_cart(self) -> Cart:
|
||||
return self.get(Endpoints.cart)
|
||||
|
||||
@serialize()
|
||||
def add_to_cart(self, product_id) -> Cart:
|
||||
add = Add()
|
||||
add_item=AddItem()
|
||||
add_item.uuid=str(uuid.uuid4())
|
||||
add_item.product_id = product_id
|
||||
add_item.action_type = "modify"
|
||||
add_item.amount = 1
|
||||
add_item.source = "hp-v2"
|
||||
add_item.sale_id = 0
|
||||
add.items = []
|
||||
add.items.append(add_item)
|
||||
return self.patch(Endpoints.cart, add.dict(by_alias=True))
|
||||
|
||||
@serialize()
|
||||
def remove_from_cart(self, product_id) -> Cart:
|
||||
add = Add()
|
||||
add_item=AddItem()
|
||||
add_item.uuid=str(uuid.uuid4())
|
||||
add_item.product_id = product_id
|
||||
add_item.action_type = "modify"
|
||||
add_item.amount = -1
|
||||
add_item.source = "hp-v2"
|
||||
add_item.sale_id = 0
|
||||
add.items = []
|
||||
add.items.append(add_item)
|
||||
return self.patch(Endpoints.cart, add.dict(by_alias=True))
|
||||
|
|
|
|||
|
|
@ -8,3 +8,4 @@ class Endpoints:
|
|||
recipe: str = 'services/frontend-service/recipe/{recipe_id}'
|
||||
prices: str = 'api/v1/products/{product_id}/prices'
|
||||
stock: str = 'api/v1/products/{product_id}/stock'
|
||||
cart: str = 'api/v2/cart'
|
||||
73
pyrohlik/models/cart.py
Normal file
73
pyrohlik/models/cart.py
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
from typing import Any, List, Optional
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class Store(BaseModel):
|
||||
store_id: int = Field(..., alias='storeId')
|
||||
zone_id: int = Field(..., alias='zoneId')
|
||||
type: str
|
||||
|
||||
|
||||
class FlatDetails(BaseModel):
|
||||
entrance: Optional[str]
|
||||
floor: Optional[str]
|
||||
door: Optional[str]
|
||||
block: Optional[str]
|
||||
apartment: Optional[str]
|
||||
staircase: Optional[str]
|
||||
intercom: Optional[str]
|
||||
additional_info: Optional[str] = Field(..., alias='additionalInfo')
|
||||
|
||||
|
||||
class Address(BaseModel):
|
||||
id: int
|
||||
city: str
|
||||
street: str
|
||||
house_number: str = Field(..., alias='houseNumber')
|
||||
orientation_number: str = Field(..., alias='orientationNumber')
|
||||
latitude: float
|
||||
longitude: float
|
||||
postal_code: str = Field(..., alias='postalCode')
|
||||
display: str
|
||||
stores: List[Store]
|
||||
flat_details: FlatDetails = Field(..., alias='flatDetails')
|
||||
additional_address_details: List[str] = Field(..., alias='additionalAddressDetails')
|
||||
|
||||
|
||||
class Destination(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
type: str
|
||||
access_type: str = Field(..., alias='accessType')
|
||||
location_type: str = Field(..., alias='locationType')
|
||||
delivery_point_provider: Optional[str] = Field(..., alias='deliveryPointProvider')
|
||||
photos: Optional[str]
|
||||
address: Address
|
||||
|
||||
|
||||
class Item(BaseModel):
|
||||
product_id: int = Field(..., alias='productId')
|
||||
amount: int
|
||||
sale_id: int = Field(..., alias='saleId')
|
||||
result: str
|
||||
|
||||
|
||||
class Cart(BaseModel):
|
||||
status: str
|
||||
cart_id: int = Field(..., alias='cartId')
|
||||
store: Store
|
||||
destination: Destination
|
||||
items: List[Item]
|
||||
|
||||
|
||||
class AddItem(BaseModel):
|
||||
source: Optional[str]
|
||||
product_id: Optional[int] = Field(alias='productId')
|
||||
sale_id: Optional[int] = Field(alias='saleId')
|
||||
action_type: Optional[str] = Field(alias='actionType')
|
||||
amount: Optional[int]
|
||||
uuid: Optional[str]
|
||||
|
||||
|
||||
class Add(BaseModel):
|
||||
items: Optional[List[AddItem]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue