Skip to main content

A Python client library for the 42 School API

Project description

FortyTwo 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
  • 🔑 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 Client

# Create client instance with credentials
client = Client(
    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.id}")
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 Client, parameter

client = Client(
    client_id="your_client_id",
    client_secret="your_client_secret"
)

# Use custom parameters for filtering
users = client.users.get_all(
    parameter.UserParameters.Filter.by_login("jdoe"),
)

4. Pagination support

All manager methods that return lists support pagination through page and page_size keyword arguments:

from fortytwo import Client

client = Client(
    client_id="your_client_id",
    client_secret="your_client_secret"
)

# Fetch first page with 50 items
users = client.users.get_all(page=1, page_size=50)

# Fetch second page
users_page2 = client.users.get_all(page=2, page_size=50)

# Works with all list-returning methods
projects = client.projects.get_by_cursus_id(21, page=1, page_size=25)
locations = client.locations.get_by_user_id(12345, page=1, page_size=100)
project_users = client.project_users.get_by_project_id(1337, page=2, page_size=50)

# Iterate through all pages
all_users = []
page = 1
while True:
    users = client.users.get_all(page=page, page_size=100)
    if not users:
        break
    all_users.extend(users)
    if len(users) < 100:  # Last page
        break
    page += 1

Pagination parameters:

  • page (int, optional): Page number to fetch (1-indexed)
  • page_size (int, optional): Number of items per page (1-100)

[!NOTE] The page_size parameter must be between 1 and 100, as enforced by the 42 API.

5. Error handling

The library raises exceptions for failed requests. Always use try-catch blocks to handle potential errors:

from fortytwo import Client
from fortytwo.exceptions import (
    FortyTwoClientException,
    FortyTwoNotFoundException,
    FortyTwoRateLimitException,
    FortyTwoNetworkException,
    FortyTwoUnauthorizedException,
)

client = Client(
    client_id="your_client_id",
    client_secret="your_client_secret"
)

try:
    user = client.users.get_by_id(user_id=12345)
    print(f"User: {user.login}")
except FortyTwoNotFoundException:
    print("User not found")
except FortyTwoUnauthorizedException:
    print("Authentication failed")
except FortyTwoRateLimitException as e:
    print(f"Rate limit exceeded. Wait {e.wait_time} seconds")
except FortyTwoNetworkException:
    print("Network error occurred")
except FortyTwoClientException as e:
    print(f"Request failed: {e}")

Available exceptions:

  • FortyTwoClientException - Base exception for all client errors
  • FortyTwoAuthException - Authentication-related errors
  • FortyTwoRequestException - General request errors
  • FortyTwoRateLimitException - Rate limit exceeded (includes wait_time attribute)
  • FortyTwoNetworkException - Network connectivity issues
  • FortyTwoParsingException - Response parsing failures
  • FortyTwoNotFoundException - Resource not found (404)
  • FortyTwoUnauthorizedException - Unauthorized access (401)
  • FortyTwoServerException - Server errors (5xx)

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

Secret Management

The client supports multiple secret storage backends:

from fortytwo import Client
import hvac

# Memory-based secrets (default)
client = Client(
    client_id="your_client_id",
    client_secret="your_client_secret"
)

# HashiCorp Vault integration
vault_client = hvac.Client(url='https://vault.example.com', token='...')
config = Client.Config(
    secret_manager=Client.SecretManager.Vault(
        vault_client=vault_client,
        secret_path='fortytwo/api'
    )
)
client = Client(config=config)

See Secret Manager Documentation for details.

Logging Configuration

The library uses Python's standard logging module. By default, it uses a NullHandler to avoid interfering with your application's logging configuration.

See Logger 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-3.0.0.tar.gz (61.4 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-3.0.0-py3-none-any.whl (71.4 kB view details)

Uploaded Python 3

File details

Details for the file fortytwo_client-3.0.0.tar.gz.

File metadata

  • Download URL: fortytwo_client-3.0.0.tar.gz
  • Upload date:
  • Size: 61.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.22

File hashes

Hashes for fortytwo_client-3.0.0.tar.gz
Algorithm Hash digest
SHA256 ac4dff0875f7fce497f1bcb838aa142d714c012c5cb2bab55880d11cec688067
MD5 072910c4cee81bff232bd37e7f66231b
BLAKE2b-256 83aedf8e689c6da708e66d5e31667b0533b0987f7f24ca565af900224b51aab8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fortytwo_client-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f8756656e4e5e55b2f965e46e8c5c6b5ebf7425031ca6668e18dd0871b8fda61
MD5 1276b4a4f4106964d1f74224143b59a6
BLAKE2b-256 30b887e849393fa7262035d953312750a966dbfe1967f251396988d81fc0afdf

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