A Python client library for the 42 School API
Project description
42 Client
A Python client library for the 42 School API that simplifies authentication and data retrieval.
Features
- 🔐 Easy authentication - OAuth2 handled automatically
- 📊 Resource managers - Convenient methods for users, projects, locations, and more
- 🚦 Rate limiting - Built-in support for Memory and Redis-based rate limiting
- 🔑 Secret management - Flexible credential storage (Memory, HashiCorp Vault)
- 🛡️ Error handling - Automatic retry and error management
- 📝 Type hints - Full type annotation support
- ⚙️ Customizable - Flexible configuration and parameters
- 🔄 Pagination - Easy iteration over paginated results
Installation
From PyPI (recommended)
# Using pip
pip install fortytwo-client
# Using uv (recommended)
uv add fortytwo-client
From source
git clone https://github.com/lucas-ht/fortytwo-client.git
cd fortytwo-client
uv sync
Development installation
git clone https://github.com/lucas-ht/fortytwo-client.git
cd fortytwo-client
uv sync --group dev
Quick Start
1. Get your API credentials
First, you need to create an application on the 42 API to get your client ID and secret.
2. Basic usage
from fortytwo import FortyTwoClient
# Create client instance with credentials
client = FortyTwoClient(
client_id="your_client_id",
client_secret="your_client_secret"
)
# Fetch user information
user = client.users.get_by_id(user_id=12345)
print(f"User: {user['login']}")
# Fetch projects
projects = client.projects.get_by_cursus_id(cursus_id=21)
3. Advanced usage with custom parameters
from fortytwo import FortyTwoClient, parameter
client = FortyTwoClient(
client_id="your_client_id",
client_secret="your_client_secret"
)
# Use custom parameters for filtering and pagination
users = client.users.get_all(
parameter.UserParameters.Filter.by_login("example"),
parameter.PageSize(50),
parameter.PageNumber(1)
)
Examples
See the example/ directory for more detailed usage examples:
fetch_user_by_id.py- Fetching user information by IDfetch_user_by_login.py- Fetching user information by loginfetch_project.py- Working with projectsfetch_location.py- Location data retrievalredis_rate_limiter.py- Redis-based distributed rate limitingvault_secret_manager.py- HashiCorp Vault secret management
Documentation
Core Features
- Resources Overview - API resource documentation
- Rate Limiter - Request rate limiting strategies (Memory, Redis)
- Secret Manager - Credential management strategies (Memory, Vault)
API Resources
The client provides managers for accessing different 42 API resources:
- Users -
client.users.*- User information and profiles - Projects -
client.projects.*- Project data and details - Locations -
client.locations.*- Campus location tracking - Project Users -
client.project_users.*- User-project relationships - Tokens -
client.tokens.*- API token management
Each resource manager provides methods like:
get_by_id(id)- Fetch a single resource by IDget_all(*params)- Fetch multiple resources with filtering- Custom methods specific to each resource type
See individual resource documentation in fortytwo/resources/ for details.
Advanced Configuration
Rate Limiting
The client supports both in-memory and Redis-based rate limiting:
from fortytwo import FortyTwoClient
# Memory-based rate limiter (default)
config = FortyTwoClient.Config(
rate_limiter=FortyTwoClient.RateLimiter.Memory(
requests_per_hour=1200,
requests_per_second=2
)
)
# Redis-based rate limiter (for distributed systems)
import redis
redis_client = redis.Redis(host='localhost', port=6379)
config = FortyTwoClient.Config(
rate_limiter=FortyTwoClient.RateLimiter.Redis(
redis_client=redis_client,
requests_per_hour=1200,
requests_per_second=2
)
)
client = FortyTwoClient(
client_id="your_client_id",
client_secret="your_client_secret",
config=config
)
See Rate Limiter Documentation for details.
Secret Management
The client supports multiple secret storage backends:
from fortytwo import FortyTwoClient
import hvac
# Memory-based secrets (default)
client = FortyTwoClient(
client_id="your_client_id",
client_secret="your_client_secret"
)
# HashiCorp Vault integration
vault_client = hvac.Client(url='https://vault.example.com', token='...')
config = FortyTwoClient.Config(
secret_manager=FortyTwoClient.SecretManager.Vault(
vault_client=vault_client,
secret_path='fortytwo/api'
)
)
client = FortyTwoClient(config=config)
See Secret Manager Documentation for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
MIT License - see the LICENSE file for details.
Links
Project details
Release history Release notifications | RSS feed
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 fortytwo_client-2.2.0.tar.gz.
File metadata
- Download URL: fortytwo_client-2.2.0.tar.gz
- Upload date:
- Size: 56.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bae603d1b7cca522a8250898de539c483f6793b1448cf3eab810e961ee762eb8
|
|
| MD5 |
f66c0eb67f6f04cc44167fc1a7a9108f
|
|
| BLAKE2b-256 |
95fc97d3137d1483311ec4d781e322a8f05aa484155de849adf968ea0f245e39
|
File details
Details for the file fortytwo_client-2.2.0-py3-none-any.whl.
File metadata
- Download URL: fortytwo_client-2.2.0-py3-none-any.whl
- Upload date:
- Size: 65.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81bdf113d8ab7c90d6747e45845ebb8f97e38a7666953997b4bb7c26a31df103
|
|
| MD5 |
7b078eae4984338e124de664120030e0
|
|
| BLAKE2b-256 |
bce2d11b8ede0e4ea3172deda6df247039d658da6a837c2b420fbebfa69dafaa
|