|
| 1 | +import copy |
| 2 | +from abc import ABC |
| 3 | +from collections.abc import Iterator |
| 4 | +from typing import Any, Self |
| 5 | + |
| 6 | +import httpx |
| 7 | + |
| 8 | +from mpt_api_client.http.client import MPTClient |
| 9 | +from mpt_api_client.models import Collection, Resource |
| 10 | +from mpt_api_client.rql.query_builder import RQLQuery |
| 11 | + |
| 12 | + |
| 13 | +class CollectionBaseClient[ResourceType: Resource](ABC): # noqa: WPS214 |
| 14 | + """Immutable Base client for RESTful resource collections. |
| 15 | +
|
| 16 | + Examples: |
| 17 | + active_orders_cc = order_collection.filter(RQLQuery(status="active")) |
| 18 | + active_orders = active_orders_cc.order_by("created").iterate() |
| 19 | + product_active_orders = active_orders_cc.filter(RQLQuery(product__id="PRD-1")).iterate() |
| 20 | +
|
| 21 | + new_order = order_collection.create(order_data) |
| 22 | +
|
| 23 | + """ |
| 24 | + |
| 25 | + _endpoint: str |
| 26 | + _resource_class: type[Resource] |
| 27 | + _collection_class: type[Collection[Resource]] |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + query_rql: RQLQuery | None = None, |
| 32 | + client: MPTClient | None = None, |
| 33 | + ) -> None: |
| 34 | + self.mpt_client = client or MPTClient() |
| 35 | + self.query_rql: RQLQuery | None = query_rql |
| 36 | + self.query_order_by: list[str] | None = None |
| 37 | + self.query_select: list[str] | None = None |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def clone(cls, collection_client: "CollectionBaseClient[ResourceType]") -> Self: |
| 41 | + """Create a copy of collection client for immutable operations. |
| 42 | +
|
| 43 | + Returns: New collection client with same settings. |
| 44 | + """ |
| 45 | + new_collection = cls( |
| 46 | + client=collection_client.mpt_client, |
| 47 | + query_rql=collection_client.query_rql, |
| 48 | + ) |
| 49 | + new_collection.query_order_by = ( |
| 50 | + copy.copy(collection_client.query_order_by) |
| 51 | + if collection_client.query_order_by |
| 52 | + else None |
| 53 | + ) |
| 54 | + new_collection.query_select = ( |
| 55 | + copy.copy(collection_client.query_select) if collection_client.query_select else None |
| 56 | + ) |
| 57 | + return new_collection |
| 58 | + |
| 59 | + def build_url(self, query_params: dict[str, Any] | None = None) -> str: |
| 60 | + """Return the endpoint URL.""" |
| 61 | + query_params = query_params or {} |
| 62 | + query_parts = [ |
| 63 | + f"{param_key}={param_value}" for param_key, param_value in query_params.items() |
| 64 | + ] # noqa: WPS237 |
| 65 | + if self.query_order_by: |
| 66 | + query_parts.append(f"order={','.join(self.query_order_by)}") # noqa: WPS237 |
| 67 | + if self.query_select: |
| 68 | + query_parts.append(f"select={','.join(self.query_select)}") # noqa: WPS237 |
| 69 | + if self.query_rql: |
| 70 | + query_parts.append(str(self.query_rql)) |
| 71 | + if query_parts: |
| 72 | + return f"{self._endpoint}?{'&'.join(query_parts)}" # noqa: WPS237 |
| 73 | + return self._endpoint |
| 74 | + |
| 75 | + def order_by(self, *fields: str) -> Self: |
| 76 | + """Returns new collection with ordering setup. |
| 77 | +
|
| 78 | + t. Returns: |
| 79 | + New collection with ordering setup. |
| 80 | +
|
| 81 | + Raises: |
| 82 | + ValueError: If ordering has already been set. |
| 83 | +
|
| 84 | + """ |
| 85 | + if self.query_order_by is not None: |
| 86 | + raise ValueError("Ordering is already set. Cannot set ordering multiple times.") |
| 87 | + new_collection = self.clone(self) |
| 88 | + new_collection.query_order_by = list(fields) |
| 89 | + return new_collection |
| 90 | + |
| 91 | + def filter(self, rql: RQLQuery) -> Self: |
| 92 | + """Creates a new collection with the filter added to the filter collection. |
| 93 | +
|
| 94 | + Returns: New copy of the collection with the filter added. |
| 95 | + """ |
| 96 | + if self.query_rql: |
| 97 | + rql = self.query_rql & rql |
| 98 | + new_collection = self.clone(self) |
| 99 | + new_collection.query_rql = rql |
| 100 | + return new_collection |
| 101 | + |
| 102 | + def select(self, *fields: str) -> Self: |
| 103 | + """Set select fields. Raises ValueError if select fields are already set. |
| 104 | +
|
| 105 | + Returns: New copy of the collection with the select fields set. |
| 106 | +
|
| 107 | + Raises: |
| 108 | + ValueError: If select fields are already set. |
| 109 | + """ |
| 110 | + if self.query_select is not None: |
| 111 | + raise ValueError( |
| 112 | + "Select fields are already set. Cannot set select fields multiple times." |
| 113 | + ) |
| 114 | + |
| 115 | + new_client = self.clone(self) |
| 116 | + new_client.query_select = list(fields) |
| 117 | + return new_client |
| 118 | + |
| 119 | + def fetch_page(self, limit: int = 100, offset: int = 0) -> Collection[ResourceType]: |
| 120 | + """Fetch one page of resources.""" |
| 121 | + response = self._fetch_page_as_response(limit=limit, offset=offset) |
| 122 | + return Collection.from_response(response) |
| 123 | + |
| 124 | + def fetch_one(self) -> ResourceType: |
| 125 | + """Fetch one page, expect exactly one result.""" |
| 126 | + response = self._fetch_page_as_response(limit=1, offset=0) |
| 127 | + resource_list: Collection[ResourceType] = Collection.from_response(response) |
| 128 | + total_records = len(resource_list) |
| 129 | + if resource_list.meta: |
| 130 | + total_records = resource_list.meta.pagination.total |
| 131 | + if total_records == 0: |
| 132 | + raise ValueError("Expected one result, but got zero results") |
| 133 | + if total_records > 1: |
| 134 | + raise ValueError(f"Expected one result, but got {total_records} results") |
| 135 | + |
| 136 | + return resource_list[0] |
| 137 | + |
| 138 | + def iterate(self) -> Iterator[ResourceType]: |
| 139 | + """Iterate over all resources, yielding GenericResource objects.""" |
| 140 | + offset = 0 |
| 141 | + limit = 100 # Default page size |
| 142 | + |
| 143 | + while True: |
| 144 | + response = self._fetch_page_as_response(limit=limit, offset=offset) |
| 145 | + items_collection: Collection[ResourceType] = Collection.from_response(response) |
| 146 | + yield from items_collection |
| 147 | + |
| 148 | + if not items_collection.meta: |
| 149 | + break |
| 150 | + if not items_collection.meta.pagination.has_next(): |
| 151 | + break |
| 152 | + offset = items_collection.meta.pagination.next_offset() |
| 153 | + |
| 154 | + def create(self, resource_data: dict[str, Any]) -> ResourceType: |
| 155 | + """Create a new resource using `POST /endpoint`. |
| 156 | +
|
| 157 | + Returns: New resource created. |
| 158 | + """ |
| 159 | + response = self.mpt_client.post(self._endpoint, json=resource_data) |
| 160 | + response.raise_for_status() |
| 161 | + |
| 162 | + return self._resource_class.from_response(response) # type: ignore[return-value] |
| 163 | + |
| 164 | + def _fetch_page_as_response(self, limit: int = 100, offset: int = 0) -> httpx.Response: |
| 165 | + """Fetch one page of resources. |
| 166 | +
|
| 167 | + Returns: httpx.Response object. |
| 168 | +
|
| 169 | + Raises: |
| 170 | + HTTPStatusError: if the response status code is not 200. |
| 171 | + """ |
| 172 | + pagination_params: dict[str, int] = {"limit": limit, "offset": offset} |
| 173 | + response = self.mpt_client.get(self.build_url(pagination_params)) |
| 174 | + response.raise_for_status() |
| 175 | + |
| 176 | + return response |
0 commit comments