Swift OAuth2 Client is a Python SDK that provides a simple and efficient way to interact with OAuth2-protected APIs. It handles token management, API calls, and file downloads with support for asynchronous operations.
- Asynchronous API calls using
httpx - Automatic token management and renewal
- Support for various request body types and response formats
- File download capabilities
- Easy-to-use interface with type hints
- Comprehensive documentation
pip install swift-oauth2-clientHere's a basic example of how to use the Swift OAuth2 Client:
from oauth2_client import OAuth2Config, new_api_client
config = OAuth2Config(
token_url="https://api.example.com/oauth/token",
client_id="your_client_id",
client_secret="your_client_secret",
scopes=["read", "write"]
)
with new_api_client(config, "https://api.example.com") as client:
response, status, content_type = client.call_api("GET", "/users")
print(f"Status: {status}, Content Type: {content_type}, Response: {response}")Asynchronous usage:
import asyncio
from oauth2_client import OAuth2Config, new_api_client_async
async def main():
config = OAuth2Config(
token_url="https://api.example.com/oauth/token",
client_id="your_client_id",
client_secret="your_client_secret",
scopes=["read", "write"]
)
async with new_api_client_async(config, "https://api.example.com") as client:
response, status, content_type = await client.call_api("GET", "/users")
print(f"Status: {status}, Content Type: {content_type}, Response: {response}")
asyncio.run(main())For more comprehensive examples, check out the examples/oauth2_client_example_async.py and examples/oauth2_client_example.pyfiles in the repository.
For detailed documentation, including API reference and usage guides, visit our documentation site.
To set up the development environment:
- Ensure you have Poetry installed
- Clone the repository
- Run
poetry installto install dependencies - Run tests with
poetry run pytest
- Build documentation with
poetry run mkdocs build - Serve the documentation with
poetry run mkdocs serve. - Open a web browser and go to http://127.0.0.1:8000/.
This project is licensed under the Apache License, Version 2.0.