Client API Reference¶
StockTrimClient¶
StockTrimClient ¶
StockTrimClient(
api_auth_id: str | None = None,
api_auth_signature: str | None = None,
base_url: str | None = None,
timeout: float = 30.0,
max_retries: int = 5,
logger: Logger | None = None,
**httpx_kwargs: Any,
)
Bases: AuthenticatedClient
The pythonic StockTrim API client with automatic resilience.
This client inherits from AuthenticatedClient and can be passed directly to generated API methods without needing a .client property.
Features: - Automatic retries on server errors (5xx) for idempotent methods only - Custom header authentication (api-auth-id, api-auth-signature) - Rich error logging and observability - Minimal configuration - just works out of the box
Simplifications vs other APIs: - No rate limiting handling - StockTrim doesn't rate limit - No automatic pagination - StockTrim API doesn't paginate - Only retries idempotent methods (GET, HEAD, OPTIONS, TRACE) on 5xx errors
Usage
Basic usage with environment variables¶
async with StockTrimClient() as client: from stocktrim_public_api_client.api.products import get_api_products
response = await get_api_products.asyncio_detailed(
client=client # Pass client directly - no .client needed!
)
With explicit credentials¶
async with StockTrimClient( api_auth_id="your-id", api_auth_signature="your-signature" ) as client: # All API calls through client get automatic resilience response = await some_api_method.asyncio_detailed(client=client)
Initialize the StockTrim API client with automatic resilience features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_auth_id
|
str | None
|
StockTrim API authentication ID. If None, will try to load from STOCKTRIM_API_AUTH_ID env var. |
None
|
api_auth_signature
|
str | None
|
StockTrim API authentication signature. If None, will try to load from STOCKTRIM_API_AUTH_SIGNATURE env var. |
None
|
base_url
|
str | None
|
Base URL for the StockTrim API. Defaults to https://api.stocktrim.com |
None
|
timeout
|
float
|
Request timeout in seconds. Defaults to 30.0. |
30.0
|
max_retries
|
int
|
Maximum number of retry attempts for failed requests. Defaults to 5. |
5
|
logger
|
Logger | None
|
Logger instance for capturing client operations. If None, creates a default logger. |
None
|
**httpx_kwargs
|
Any
|
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:
| Type | Description |
|---|---|
ValueError
|
If no API credentials are provided and environment variables are not set. |
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 StockTrimClient() as client: ... # All API calls through client get automatic resilience ... response = await some_api_method.asyncio_detailed(client=client)
Source code in stocktrim_public_api_client/stocktrim_client.py
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 | |
bill_of_materials
property
¶
Access the BillOfMaterials helper for BOM management.
forecasting
property
¶
Access the Forecasting helper for forecast management and processing status.
order_plan
property
¶
Access the OrderPlan helper for forecast and demand planning operations.
purchase_orders
property
¶
Access the PurchaseOrders helper for purchase order management.
purchase_orders_v2
property
¶
Access the PurchaseOrdersV2 helper for V2 purchase order operations (recommended over V1).
sales_orders
property
¶
Access the SalesOrders helper for sales order management.
__aenter__
async
¶
Enter async context manager, returning self for proper type checking.
__aexit__
async
¶
__repr__ ¶
IdempotentOnlyRetry¶
IdempotentOnlyRetry ¶
Bases: Retry
Custom Retry class that only retries idempotent methods (GET, HEAD, OPTIONS, TRACE) on server errors (5xx status codes).
StockTrim doesn't have rate limiting (429), so we only need to handle 5xx errors and we only retry idempotent methods to avoid duplicate operations.
Initialize and track the current request method.
Source code in stocktrim_public_api_client/stocktrim_client.py
increment ¶
Return a new retry instance with the attempt count incremented.
Source code in stocktrim_public_api_client/stocktrim_client.py
is_retryable_method ¶
Allow all methods to pass through the initial check.
Store the method for later use in is_retryable_status_code.
Source code in stocktrim_public_api_client/stocktrim_client.py
is_retryable_status_code ¶
Check if a status code is retryable for the current method.
For 5xx errors, only allow idempotent methods.
Source code in stocktrim_public_api_client/stocktrim_client.py
ErrorLoggingTransport¶
ErrorLoggingTransport ¶
ErrorLoggingTransport(
wrapped_transport: AsyncHTTPTransport | None = None,
logger: Logger | None = None,
**kwargs: Any,
)
Bases: AsyncHTTPTransport
Transport layer that adds comprehensive logging for all HTTP requests and responses.
This transport wraps another AsyncHTTPTransport and intercepts responses to log: - DEBUG: Request details (sanitized headers), response bodies for 2xx responses - INFO: Successful 2xx responses with timing - WARNING: Null responses that may cause TypeErrors - ERROR: 4xx client errors and 5xx server errors with response details
Initialize the error logging transport.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wrapped_transport
|
AsyncHTTPTransport | None
|
The transport to wrap. If None, creates a new AsyncHTTPTransport. |
None
|
logger
|
Logger | None
|
Logger instance for capturing error details. If None, creates a default logger. |
None
|
**kwargs
|
Any
|
Additional arguments passed to AsyncHTTPTransport if wrapped_transport is None. |
{}
|
Source code in stocktrim_public_api_client/stocktrim_client.py
handle_async_request
async
¶
Handle request and log based on response status code.
Source code in stocktrim_public_api_client/stocktrim_client.py
log_parsing_error ¶
log_parsing_error(
error: TypeError
| ValueError
| AttributeError
| Exception,
response: Response,
request: Request,
) -> None
Log detailed information when an error occurs during response parsing.
This method intelligently inspects the error and response to provide actionable debugging information with fix suggestions: - For TypeErrors: Shows null fields and provides 3 fix options with doc links - For ValueErrors: Shows the error and response excerpt - For any parsing error: Logs the raw response for inspection
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
TypeError | ValueError | AttributeError | Exception
|
The exception that occurred during parsing |
required |
response
|
Response
|
The HTTP response that was being parsed |
required |
request
|
Request
|
The original HTTP request |
required |
Example output for TypeError with null fields
ERROR: TypeError during parsing for GET /api/V2/PurchaseOrders ERROR: TypeError: object of type 'NoneType' has no len() ERROR: Found 3 null field(s) in response: ERROR: - orderDate ERROR: - fullyReceivedDate ERROR: - supplier.supplierName ERROR: ERROR: Possible fixes: ERROR: 1. Add fields to NULLABLE_FIELDS in scripts/regenerate_client.py and regenerate ERROR: 2. Update OpenAPI spec to mark these fields as 'nullable: true' ERROR: 3. Handle null values defensively in helper methods ERROR: ERROR: See: docs/contributing/api-feedback.md#nullable-date-fields-not-marked-in-spec
Example output for ValueError
ERROR: ValueError during parsing for POST /api/Products ERROR: ValueError: invalid literal for int() with base 10: 'abc' ERROR: Response excerpt: {"id": "abc", "name": "Product 1"}
Source code in stocktrim_public_api_client/stocktrim_client.py
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |