katana_public_api_client.katana_client¶
katana_public_api_client.katana_client
¶
KatanaClient - The pythonic Katana API client with automatic resilience.
This client uses httpx's native transport layer to provide automatic retries, rate limiting, error handling, and pagination for all API calls without any decorators or wrapper methods needed.
Attributes¶
Classes¶
ErrorLoggingTransport(wrapped_transport=None, logger=None, **kwargs)
¶
Bases: AsyncHTTPTransport
Transport layer that adds detailed error logging for 4xx client errors.
This transport wraps another AsyncHTTPTransport and intercepts responses to log detailed error information using the generated error models.
Parameters:
-
wrapped_transport(AsyncHTTPTransport | None, default:None) –The transport to wrap. If None, creates a new AsyncHTTPTransport.
-
logger(Logger | None, default:None) –Logger instance for capturing error details. If None, creates a default logger.
-
**kwargs(Any, default:{}) –Additional arguments passed to AsyncHTTPTransport if wrapped_transport is None.
Source code in katana_public_api_client/katana_client.py
Functions¶
handle_async_request(request)
async
¶
Handle request and log detailed error information for 4xx responses.
Source code in katana_public_api_client/katana_client.py
KatanaClient(api_key=None, base_url=None, timeout=30.0, max_retries=5, max_pages=100, logger=None, **httpx_kwargs)
¶
Bases: AuthenticatedClient
The pythonic Katana API client with automatic resilience and pagination.
This client inherits from AuthenticatedClient and can be passed directly to generated API methods without needing the .client property.
Features: - Automatic retries on network errors and server errors (5xx) - Automatic rate limit handling with Retry-After header support - Auto-pagination ON by default for GET requests (collects all pages automatically) - Uses 250 items per page (Katana's max) for efficient pagination - Rich logging and observability - Minimal configuration - just works out of the box
Auto-pagination behavior:
- ON by default for GET requests with NO page parameter
- Uses 250 items per page when no limit specified by caller
- If caller specifies a limit, that limit is used per page
- ANY explicit page parameter disables auto-pagination (e.g., page=1)
- Disabled per-request via extensions: extensions={"auto_pagination": False}
- Control max pages via max_pages constructor parameter
- Limit total items via extensions: extensions={"max_items": 200}
Usage
async with KatanaClient() as client: from katana_public_api_client.api.product import get_all_products
# Auto-pagination is ON - all pages collected automatically
# Uses 250 items per page for efficiency
response = await get_all_products.asyncio_detailed(
client=client, # Pass client directly - no .client needed!
)
# Use a custom limit per page (100 instead of 250)
response = await get_all_products.asyncio_detailed(
client=client,
limit=100, # Use 100 per page
)
# Get a specific page only (ANY page param disables auto-pagination)
response = await get_all_products.asyncio_detailed(
client=client,
page=2, # Get page 2 only
limit=50
)
# Limit total items collected (via httpx client)
httpx_client = client.get_async_httpx_client()
response = await httpx_client.get(
"/products",
extensions={"max_items": 200} # Stop after 200 items
)
# Control max pages globally
client_limited = KatanaClient(max_pages=5) # Limit to 5 pages max
Parameters:
-
api_key(str | None, default:None) –Katana API key. If None, will try to load from KATANA_API_KEY env var, .env file, or ~/.netrc file (in that order).
-
base_url(str | None, default:None) –Base URL for the Katana API. Defaults to https://api.katanamrp.com/v1
-
timeout(float, default:30.0) –Request timeout in seconds. Defaults to 30.0.
-
max_retries(int, default:5) –Maximum number of retry attempts for failed requests. Defaults to 5.
-
max_pages(int, default:100) –Maximum number of pages to collect during auto-pagination. Defaults to 100.
-
logger(Logger | None, default:None) –Any object whose debug/info/warning/error methods accept (msg, *args, **kwargs) — the standard logging.Logger call convention (e.g. logging.Logger, structlog.BoundLogger). If None, creates a default stdlib logger.
-
**httpx_kwargs(Any, default:{}) –Additional arguments passed to the base AsyncHTTPTransport. Common parameters include: - http2 (bool): Enable HTTP/2 support - limits (httpx.Limits): Connection pool limits - verify (bool | str | ssl.SSLContext): SSL certificate verification - cert (str | tuple): Client-side certificates - trust_env (bool): Trust environment variables for proxy configuration - event_hooks (dict): Custom event hooks (will be merged with built-in hooks)
Raises:
-
ValueError–If no API key is provided via api_key param, KATANA_API_KEY env var, .env file, or ~/.netrc file.
Note
Transport-related parameters (http2, limits, verify, etc.) are correctly passed to the innermost AsyncHTTPTransport layer, ensuring they take effect even with the layered transport architecture.
Example
async with KatanaClient() as client: ... # All API calls through client get automatic resilience ... response = await some_api_method.asyncio_detailed(client=client)
Source code in katana_public_api_client/katana_client.py
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 | |
Attributes¶
api
property
¶
Thin CRUD wrappers for all API resources. Returns raw attrs models.
Example
async with KatanaClient() as client: ... products = await client.api.products.list(is_sellable=True) ... product = await client.api.products.get(123) ... await client.api.products.delete(123)
inventory
property
¶
Access inventory and stock operations.
Returns:
-
Inventory–Inventory instance for stock levels, movements, and adjustments.
Example
async with KatanaClient() as client: ... # Check stock levels ... stock = await client.inventory.check_stock("WIDGET-001") ... low_stock = await client.inventory.list_low_stock(threshold=10)
materials
property
¶
Access material catalog operations.
Returns:
-
Materials–Materials instance for material CRUD operations.
Example
async with KatanaClient() as client: ... materials = await client.materials.list() ... material = await client.materials.get(123)
products
property
¶
Access product catalog operations.
Returns:
-
Products–Products instance for product CRUD and search operations.
Example
async with KatanaClient() as client: ... # Product CRUD ... products = await client.products.list(is_sellable=True) ... product = await client.products.get(123) ... results = await client.products.search("widget")
services
property
¶
Access service catalog operations.
Returns:
-
Services–Services instance for service CRUD operations.
Example
async with KatanaClient() as client: ... services = await client.services.list() ... service = await client.services.get(123)
variants
property
¶
Access variant catalog operations.
Returns:
-
Variants–Variants instance for variant CRUD operations.
Example
async with KatanaClient() as client: ... variants = await client.variants.list() ... variant = await client.variants.get(123)
Functions¶
PaginationTransport(wrapped_transport=None, max_pages=100, logger=None, **kwargs)
¶
Bases: AsyncHTTPTransport
Transport layer that adds automatic pagination for GET requests.
This transport wraps another transport and automatically collects all pages for GET requests by default.
Auto-pagination behavior:
- ON by default for GET requests with NO page parameter in URL
- Uses 250 items per page (Katana's max) when no limit specified by caller
- If caller specifies a limit, that limit is used (caller's choice)
- ANY explicit page parameter in URL disables auto-pagination (e.g., ?page=1)
- Disabled when request has extensions={"auto_pagination": False}
- Only applies to GET requests (POST, PUT, etc. are never paginated)
Controlling pagination limits:
- max_pages (constructor): Maximum number of pages to fetch
- max_items (extension): Maximum total items to collect, e.g.,
extensions={"max_items": 200} stops after 200 items
Parameters:
-
wrapped_transport(AsyncHTTPTransport | None, default:None) –The transport to wrap. If None, creates a new AsyncHTTPTransport.
-
max_pages(int, default:100) –Maximum number of pages to collect during auto-pagination. Defaults to 100.
-
logger(Logger | None, default:None) –Logger instance for capturing pagination operations. If None, creates a default logger.
-
**kwargs(Any, default:{}) –Additional arguments passed to AsyncHTTPTransport if wrapped_transport is None.
Source code in katana_public_api_client/katana_client.py
Functions¶
handle_async_request(request)
async
¶
Handle request with automatic pagination for GET requests.
Auto-pagination is ON by default for GET requests. It is disabled when:
- extensions={"auto_pagination": False} is set, OR
- ANY explicit page parameter is in the URL (e.g., ?page=1 or ?page=2)
To get auto-pagination, simply don't pass a page parameter. The transport will automatically use 250 items per page (Katana's max) unless you specify a limit, in which case your limit will be respected.
Source code in katana_public_api_client/katana_client.py
RateLimitAwareRetry(*args, **kwargs)
¶
Bases: Retry
Custom Retry class that allows non-idempotent methods (POST, PATCH) to be retried ONLY when receiving a 429 (Too Many Requests) status code.
For all other retryable status codes (502, 503, 504), only idempotent methods (HEAD, GET, PUT, DELETE, OPTIONS, TRACE) will be retried.
This ensures we don't accidentally retry non-idempotent operations after server errors, but we DO retry them when we're being rate-limited.
Source code in katana_public_api_client/katana_client.py
Functions¶
increment()
¶
Return a new retry instance with the attempt count incremented.
Source code in katana_public_api_client/katana_client.py
is_retryable_method(method)
¶
Allow all methods to pass through the initial check.
Store the method for later use in is_retryable_status_code.
Source code in katana_public_api_client/katana_client.py
is_retryable_status_code(status_code)
¶
Check if a status code is retryable for the current method.
For 429 (rate limiting), allow all methods. For other errors (502, 503, 504), only allow idempotent methods.
Source code in katana_public_api_client/katana_client.py
Functions¶
ResilientAsyncTransport(max_retries=5, max_pages=100, logger=None, **kwargs)
¶
Factory function that creates a chained transport with error logging, pagination, and retry capabilities.
This function chains multiple transport layers: 1. AsyncHTTPTransport (base HTTP transport) 2. ErrorLoggingTransport (logs detailed 4xx errors) 3. PaginationTransport (auto-collects paginated responses) 4. RetryTransport (handles retries with Retry-After header support)
Parameters:
-
max_retries(int, default:5) –Maximum number of retry attempts for failed requests. Defaults to 5.
-
max_pages(int, default:100) –Maximum number of pages to collect during auto-pagination. Defaults to 100.
-
logger(Logger | None, default:None) –Logger instance for capturing operations. If None, creates a default logger.
-
**kwargs(Any, default:{}) –Additional arguments passed to the base AsyncHTTPTransport. Common parameters include: - http2 (bool): Enable HTTP/2 support - limits (httpx.Limits): Connection pool limits - verify (bool | str | ssl.SSLContext): SSL certificate verification - cert (str | tuple): Client-side certificates - trust_env (bool): Trust environment variables for proxy configuration
Returns:
-
RetryTransport–A RetryTransport instance wrapping all the layered transports.
Note
When using a custom transport, parameters like http2, limits, and verify must be passed to this factory function (which passes them to the base AsyncHTTPTransport), not to the httpx.Client/AsyncClient constructor.
Example
Source code in katana_public_api_client/katana_client.py
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 | |