Skip to main content

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:

Documentation

Core Features

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 ID
  • get_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.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

MIT License - see the LICENSE file for details.

Links

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

fortytwo_client-2.2.0.tar.gz (56.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fortytwo_client-2.2.0-py3-none-any.whl (65.8 kB view details)

Uploaded Python 3

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

Hashes for fortytwo_client-2.2.0.tar.gz
Algorithm Hash digest
SHA256 bae603d1b7cca522a8250898de539c483f6793b1448cf3eab810e961ee762eb8
MD5 f66c0eb67f6f04cc44167fc1a7a9108f
BLAKE2b-256 95fc97d3137d1483311ec4d781e322a8f05aa484155de849adf968ea0f245e39

See more details on using hashes here.

File details

Details for the file fortytwo_client-2.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fortytwo_client-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 81bdf113d8ab7c90d6747e45845ebb8f97e38a7666953997b4bb7c26a31df103
MD5 7b078eae4984338e124de664120030e0
BLAKE2b-256 bce2d11b8ede0e4ea3172deda6df247039d658da6a837c2b420fbebfa69dafaa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page