|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +from playwright.sync_api import APIRequestContext |
| 4 | + |
| 5 | +from services.api_endpoints import APIEndpoints |
| 6 | +from services.http_client import HTTPClient |
| 7 | + |
| 8 | +CART_ENDPOINT = APIEndpoints.CART_ENDPOINT |
| 9 | + |
| 10 | + |
| 11 | +class CartAPIClient(HTTPClient): |
| 12 | + """Cart API client.""" |
| 13 | + |
| 14 | + def __init__(self, api_context: APIRequestContext) -> None: |
| 15 | + """ |
| 16 | + Initializing the UserAPIClient with the passed APIRequestContext. |
| 17 | + |
| 18 | + :param api_context: Context for executing API requests |
| 19 | + """ |
| 20 | + super().__init__(api_context) |
| 21 | + |
| 22 | + def get_a_cart(self, task_id: str, user_uuid: str) -> dict[str, Any]: |
| 23 | + """ |
| 24 | + Getting a list of users with a Task-Id. |
| 25 | + |
| 26 | + :param task_id: Task ID |
| 27 | + :return: Response from the API |
| 28 | + """ |
| 29 | + headers = {"X-Task-Id": task_id} |
| 30 | + endpoint = f"{APIEndpoints.USERS_ENDPOINT}/{user_uuid}/{CART_ENDPOINT}" |
| 31 | + response = self.get(endpoint, headers=headers) |
| 32 | + |
| 33 | + if not response.ok: |
| 34 | + msg = f"Request failed with status {response.status}: {response.text}" |
| 35 | + raise ValueError(msg) |
| 36 | + |
| 37 | + return response.json() |
| 38 | + |
| 39 | + def add_an_item_to_cart( |
| 40 | + self, task_id: str, user_uuid: str, item_uuid: str |
| 41 | + ) -> dict[str, Any]: |
| 42 | + """ |
| 43 | + Adding an item to the user's cart. |
| 44 | + |
| 45 | + :param task_id: Task ID |
| 46 | + :param user_uuid: UUID of the user |
| 47 | + :param item_uuid: UUID of the item to add to the cart |
| 48 | + """ |
| 49 | + headers = {"X-Task-Id": task_id} |
| 50 | + endpoint = f"{APIEndpoints.USERS_ENDPOINT}/{user_uuid}/{CART_ENDPOINT}/add" |
| 51 | + data = {"item_uuid": item_uuid} |
| 52 | + response = self.post(endpoint, headers=headers, json=data) |
| 53 | + |
| 54 | + if not response.ok: |
| 55 | + msg = f"Request failed with status {response.status}: {response.text}" |
| 56 | + raise ValueError(msg) |
| 57 | + |
| 58 | + return response.json() |
| 59 | + |
| 60 | + def change_item_quantity( |
| 61 | + self, task_id: str, user_uuid: str, item_uuid: str, quantity: int |
| 62 | + ) -> dict[str, Any]: |
| 63 | + """ |
| 64 | + Changing the quantity of an item in the user's cart. |
| 65 | + |
| 66 | + :param task_id: Task ID |
| 67 | + :param user_uuid: UUID of the user |
| 68 | + :param item_uuid: UUID of the item to change the quantity of |
| 69 | + :param quantity: New quantity of the item |
| 70 | + """ |
| 71 | + headers = {"X-Task-Id": task_id} |
| 72 | + endpoint = f"{APIEndpoints.USERS_ENDPOINT}/{user_uuid}/{CART_ENDPOINT}/change" |
| 73 | + data = {"item_uuid": item_uuid, "quantity": quantity} |
| 74 | + response = self.post(endpoint, headers=headers, json=data) |
| 75 | + |
| 76 | + if not response.ok: |
| 77 | + msg = f"Request failed with status {response.status}: {response.text}" |
| 78 | + raise ValueError(msg) |
| 79 | + |
| 80 | + return response.json() |
| 81 | + |
| 82 | + def clear_cart(self, task_id: str, user_uuid: str) -> dict[str, Any]: |
| 83 | + """ |
| 84 | + Clearing the user's cart. |
| 85 | + |
| 86 | + :param task_id: Task ID |
| 87 | + :param user_uuid: UUID of the user |
| 88 | + """ |
| 89 | + headers = {"X-Task-Id": task_id} |
| 90 | + endpoint = f"{APIEndpoints.USERS_ENDPOINT}/{user_uuid}/{CART_ENDPOINT}/clear" |
| 91 | + response = self.post(endpoint, headers=headers) |
| 92 | + |
| 93 | + if not response.ok: |
| 94 | + msg = f"Request failed with status {response.status}: {response.text}" |
| 95 | + raise ValueError(msg) |
| 96 | + |
| 97 | + return response.json() |
| 98 | + |
| 99 | + def remove_an_item_from_cart( |
| 100 | + self, task_id: str, user_uuid: str, item_uuid: str |
| 101 | + ) -> dict[str, Any]: |
| 102 | + """ |
| 103 | + Removing an item from the user's cart. |
| 104 | + |
| 105 | + :param task_id: Task ID |
| 106 | + :param user_uuid: UUID of the user |
| 107 | + :param item_uuid: UUID of the item to remove from the cart |
| 108 | + """ |
| 109 | + headers = {"X-Task-Id": task_id} |
| 110 | + endpoint = f"{APIEndpoints.USERS_ENDPOINT}/{user_uuid}/{CART_ENDPOINT}/remove" |
| 111 | + data = {"item_uuid": item_uuid} |
| 112 | + response = self.post(endpoint, headers=headers, json=data) |
| 113 | + |
| 114 | + if not response.ok: |
| 115 | + msg = f"Request failed with status {response.status}: {response.text}" |
| 116 | + raise ValueError(msg) |
| 117 | + |
| 118 | + return response.json() |
0 commit comments