Add your description here
Project description
BCOData
A Python library for interacting with Business Central OData API with built-in rate limiting, retry mechanisms, and concurrent request handling.
Features
- Asynchronous API client for Business Central OData endpoints
- Automatic rate limiting to prevent API throttling
- Built-in retry mechanism for failed requests
- Concurrent request handling with configurable limits
- Comprehensive error handling with detailed error messages
- Automatic pagination handling for large datasets
Installation
pip install bcodata
Usage
Basic Usage
import asyncio
from bcodata import Client
async def main():
# Initialize the client with your Business Central OData API URL and credentials
async with Client(
base_url="https://api.businesscentral.dynamics.com/v2.0/tenant_id/api/v2.0",
credentials=("username", "password")
) as client:
# Fetch data from an endpoint
data = await client.get_data("companies")
print(f"Retrieved {len(data)} companies")
if __name__ == "__main__":
asyncio.run(main())
Advanced Configuration
import asyncio
from bcodata import Client
async def main():
async with Client(
base_url="https://api.businesscentral.dynamics.com/v2.0/tenant_id/api/v2.0",
credentials=("username", "password"),
max_rate=20, # Maximum requests per time period
time_period=1, # Time period in seconds
max_concurrency=10, # Maximum concurrent requests
max_retries=3, # Number of retry attempts
base_retry_delay=1, # Base delay between retries in seconds
timeout=90 # Request timeout in seconds
) as client:
data = await client.get_data("companies")
print(f"Retrieved {len(data)} companies")
if __name__ == "__main__":
asyncio.run(main())
Concurrent Data Fetching
Here's an example of how to fetch data from multiple endpoints concurrently:
import asyncio
from bcodata import Client
async def fetch_multiple_endpoints(client: Client):
# Define the endpoints to fetch
endpoints = [
"companies",
"customers",
"items",
"salesOrders"
]
# Create tasks for each endpoint
tasks = [client.get_data(endpoint) for endpoint in endpoints]
# Execute all tasks concurrently
results = await asyncio.gather(*tasks)
# Process results
for endpoint, data in zip(endpoints, results):
print(f"Retrieved {len(data)} records from {endpoint}")
async def main():
async with Client(
base_url="https://api.businesscentral.dynamics.com/v2.0/tenant_id/api/v2.0",
credentials=("username", "password")
) as client:
await fetch_multiple_endpoints(client)
if __name__ == "__main__":
asyncio.run(main())
Error Handling
The library provides specific exceptions for different types of errors:
from bcodata import Client
from bcodata.exceptions import (
ODataConnectionError,
ODataHTTPError,
ODataJSONDecodeError,
ODataTimeoutError,
ODataRequestError
)
async def main():
try:
async with Client(
base_url="https://api.businesscentral.dynamics.com/v2.0/tenant_id/api/v2.0",
credentials=("username", "password")
) as client:
data = await client.get_data("companies")
except ODataConnectionError as e:
print(f"Connection error: {e}")
except ODataTimeoutError as e:
print(f"Timeout error: {e}")
except ODataHTTPError as e:
print(f"HTTP error: {e}")
except ODataJSONDecodeError as e:
print(f"JSON decode error: {e}")
except ODataRequestError as e:
print(f"Request error: {e}")
if __name__ == "__main__":
asyncio.run(main())
Examples
The repository includes a comprehensive set of examples in the examples/ directory:
basic_usage.py: Simple example of fetching data from a single endpointconcurrent_fetching.py: Demonstrates concurrent data fetching from multiple endpointserror_handling.py: Shows how to handle various error scenarios and implement retry logic
Each example is well-documented and includes comments explaining the code. To run the examples:
# Clone the repository
git clone https://github.com/yourusername/bcodata.git
cd bcodata
# Install the package
pip install -e .
# Run an example
python examples/basic_usage.py
For more details about each example, see the examples README.
Configuration Options
The Client class accepts the following configuration parameters:
base_url(str): The base URL of the Business Central OData APIcredentials(tuple[str, str] | None): Username and password for authenticationmax_rate(int): Maximum number of requests per time period (default: 10)time_period(int): Time period in seconds for rate limiting (default: 1)max_concurrency(int): Maximum number of concurrent requests (default: 5)max_retries(int): Number of retry attempts for failed requests (default: 3)base_retry_delay(int): Base delay between retries in seconds (default: 1)timeout(int): Request timeout in seconds (default: 90)
Best Practices
- Always use the client as a context manager (
async with) to ensure proper resource cleanup - Configure appropriate rate limits based on your Business Central API tier
- Use concurrent fetching for multiple endpoints to improve performance
- Implement proper error handling for production use
- Monitor the logs for any rate limiting or retry events
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bcodata-0.1.0.tar.gz.
File metadata
- Download URL: bcodata-0.1.0.tar.gz
- Upload date:
- Size: 13.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a30d68b0b9df31692f54bfc59103a6f3669ce4f53b6a8d9d830076665b50aec3
|
|
| MD5 |
cc956e3729981c11d80ea9623e270223
|
|
| BLAKE2b-256 |
c8448d7aed3ebd9ed55636fb23663435f70aa4cd9b05d032775d59865bd65dca
|
Provenance
The following attestation bundles were made for bcodata-0.1.0.tar.gz:
Publisher:
ci-cd.yml on akshaypra26/bcodata
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bcodata-0.1.0.tar.gz -
Subject digest:
a30d68b0b9df31692f54bfc59103a6f3669ce4f53b6a8d9d830076665b50aec3 - Sigstore transparency entry: 211532545
- Sigstore integration time:
-
Permalink:
akshaypra26/bcodata@1043d3c5b48522a782f2ed902e298e066753700d -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/akshaypra26
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci-cd.yml@1043d3c5b48522a782f2ed902e298e066753700d -
Trigger Event:
push
-
Statement type:
File details
Details for the file bcodata-0.1.0-py3-none-any.whl.
File metadata
- Download URL: bcodata-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
861d32ecbe92884b4f758fc1f309f4a82a30c417ae4d3a303b95e890af12cc69
|
|
| MD5 |
c3add7f760acc76940311820e74b8f99
|
|
| BLAKE2b-256 |
37e0552367d4142a816172f4e0c67f4fa278040e28b60b2ae5b072dfa67d90f5
|
Provenance
The following attestation bundles were made for bcodata-0.1.0-py3-none-any.whl:
Publisher:
ci-cd.yml on akshaypra26/bcodata
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bcodata-0.1.0-py3-none-any.whl -
Subject digest:
861d32ecbe92884b4f758fc1f309f4a82a30c417ae4d3a303b95e890af12cc69 - Sigstore transparency entry: 211532547
- Sigstore integration time:
-
Permalink:
akshaypra26/bcodata@1043d3c5b48522a782f2ed902e298e066753700d -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/akshaypra26
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci-cd.yml@1043d3c5b48522a782f2ed902e298e066753700d -
Trigger Event:
push
-
Statement type: