katana_public_api_client.utils¶
katana_public_api_client.utils
¶
Utility functions for working with Katana API responses.
This module provides convenient helpers for unwrapping API responses, handling errors, extracting data, and formatting display values.
Classes¶
APIError(message, status_code, error_response=None)
¶
Bases: Exception
Base exception for API errors.
Parameters:
-
message(str) –Human-readable error message
-
status_code(int) –HTTP status code
-
error_response(ErrorResponse | DetailedErrorResponse | None, default:None) –The error response object from the API
Source code in katana_public_api_client/utils.py
Functions¶
AuthenticationError(message, status_code, error_response=None)
¶
Bases: APIError
Raised when authentication fails (401).
Source code in katana_public_api_client/utils.py
RateLimitError(message, status_code, error_response=None)
¶
Bases: APIError
Raised when rate limit is exceeded (429).
Source code in katana_public_api_client/utils.py
ServerError(message, status_code, error_response=None)
¶
Bases: APIError
Raised when server error occurs (5xx).
Source code in katana_public_api_client/utils.py
ValidationError(message, status_code, error_response=None)
¶
Bases: APIError
Raised when request validation fails (422).
Parameters:
-
message(str) –Human-readable error message
-
status_code(int) –HTTP status code (should be 422)
-
error_response(DetailedErrorResponse | None, default:None) –The detailed error response with validation details
Source code in katana_public_api_client/utils.py
Functions¶
__str__()
¶
Format validation error with code-specific details.
Source code in katana_public_api_client/utils.py
Functions¶
get_error_message(response)
¶
Extract error message from an error response.
Parameters:
-
response(Response[T]) –The Response object (typically an error response)
Returns:
-
str | None–Error message string, or None if no error message found
Example
Source code in katana_public_api_client/utils.py
get_variant_display_name(variant)
¶
Build the full variant display name matching Katana UI format.
Format: "{Product/Material Name} / {Config Value 1} / {Config Value 2} / ..."
Example: "Mayhem 140 / Liquid Black / Large / 5 Star"
When the variant has been fetched with extend=product_or_material, the API returns variants with a nested product_or_material object (Product or Material). This function extracts the base product/material name and appends config attribute values separated by " / ".
Parameters:
-
variant(Variant) –Variant object (ideally with product_or_material populated)
Returns:
-
str–Formatted variant name with config values, or empty string if no name available
Example
from katana_public_api_client import KatanaClient
from katana_public_api_client.api.variant import get_variant
from katana_public_api_client.utils import get_variant_display_name
async with KatanaClient() as client:
response = await get_variant.asyncio_detailed(client=client, id=123)
variant = unwrap(response)
display_name = get_variant_display_name(variant)
print(display_name) # "Mayhem 140 / Liquid Black / Large / 5 Star"
Source code in katana_public_api_client/utils.py
handle_response(response, *, on_success=None, on_error=None, raise_on_error=False)
¶
Handle a response with custom success and error handlers.
This function provides a convenient way to handle both success and error cases with custom callbacks.
Parameters:
-
response(Response[T]) –The Response object from an API call
-
on_success(Callable[[T], Any] | None, default:None) –Callback function to call with parsed data on success
-
on_error(Callable[[APIError], Any] | None, default:None) –Callback function to call with APIError on error
-
raise_on_error(bool, default:False) –If True, raise the error even if on_error is provided
Returns:
-
Any–Result of on_success callback, result of on_error callback, or None
Example
def handle_products(product_list):
print(f"Got {len(product_list.data)} products")
return product_list.data
def handle_error(error):
print(f"Error: {error}")
return []
response = await get_all_products.asyncio_detailed(client=client)
products = handle_response(
response, on_success=handle_products, on_error=handle_error
)
Source code in katana_public_api_client/utils.py
is_error(response)
¶
Check if a response was an error (4xx or 5xx status code).
Parameters:
Returns:
-
bool–True if status code is 4xx or 5xx, False otherwise
Source code in katana_public_api_client/utils.py
is_success(response)
¶
Check if a response was successful (2xx status code).
Parameters:
Returns:
-
bool–True if status code is 2xx, False otherwise
Example
Source code in katana_public_api_client/utils.py
unwrap(response, *, raise_on_error=True)
¶
Unwrap a Response object and return the parsed data or raise an error.
This is the main utility function for handling API responses. It automatically raises appropriate exceptions for error responses and returns the parsed data for successful responses.
Parameters:
-
response(Response[T]) –The Response object from an API call
-
raise_on_error(bool, default:True) –If True, raise exceptions on error status codes. If False, return None on errors.
Returns:
-
T | None–The parsed response data
Raises:
-
AuthenticationError–When status is 401
-
ValidationError–When status is 422
-
RateLimitError–When status is 429
-
ServerError–When status is 5xx
-
APIError–For other error status codes
Example
from katana_public_api_client import KatanaClient
from katana_public_api_client.api.product import get_all_products
from katana_public_api_client.utils import unwrap
async with KatanaClient() as client:
response = await get_all_products.asyncio_detailed(client=client)
product_list = unwrap(
response
) # Raises on error, returns parsed data
products = product_list.data # List of Product objects
Source code in katana_public_api_client/utils.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
unwrap_data(response, *, raise_on_error=True, default=None)
¶
Unwrap a Response and extract the data list from list responses.
This is a convenience function that unwraps the response and extracts
the .data field from list response objects (like ProductListResponse,
WebhookListResponse, etc.).
Parameters:
-
response(Response[T]) –The Response object from an API call
-
raise_on_error(bool, default:True) –If True, raise exceptions on error status codes. If False, return default on errors.
-
default(list[DataT] | None, default:None) –Default value to return if data is not available
Returns:
-
Any | None–List of data objects, or default if not available
Example
from katana_public_api_client import KatanaClient
from katana_public_api_client.api.product import get_all_products
from katana_public_api_client.utils import unwrap_data
async with KatanaClient() as client:
response = await get_all_products.asyncio_detailed(client=client)
products = unwrap_data(response) # Directly get list of Products
for product in products:
print(product.name)