How jeandemeusy/api-lib works
Project type and purpose
• This is an open-source Python library (“api-lib”) whose sole purpose is to simplify both building and consuming HTTP-based APIs.
• It provides core client functionality (in src/api_lib/api_lib.py), pluggable request headers, HTTP method abstractions, and object (de)serialization for requests and responses.
File structure
• Top-level:
• src/api_lib/ – the library code
– api_lib.py: core ApiLib base class and request/response orchestration
– headers/: header-building subpackage (Accept, Authorization, generic Header class)
– method.py: HTTP method enum or constants
– objects/: request/response object definitions (Request, Response)
• test/: unit tests and fixtures for every submodule
• CI & config (GitHub Actions, Makefile, pyproject.toml, etc.)
Insights from README
• The ApiLib class is designed for subclassing by concrete APIs (e.g., GithubAPI).
• Users supply base URL and authentication headers; they define async methods that call self.req(Method, path, Model) to marshal responses.
• Extensibility points: headers list, custom request/response models.
Main components and how they interact
a. ApiLib Core
– Orchestrates HTTP calls, serializes request models, deserializes response models.
– Exposes a generic req(method, path, model) async API.
b. Header Factory Subsystem
– A set of Header classes (Accept, Authorization) that generate HTTP headers at runtime.
– Injected into ApiLib via the headers attribute or constructor.
c. Method Definition
– Enumeration or constants for HTTP methods (GET, POST, etc.).
d. Object (De)serialization
– Request/Response model classes under objects/ that map JSON payloads to typed Python objects.
e. HTTP Transport Layer (implicit)
– The underlying HTTP client (e.g., aiohttp or httpx – may be hidden or pluggable).
f. External API Service
– Any upstream REST API endpoint (e.g., GitHub API).
Architectural patterns & design principles
• Separation of concerns: transport vs. header generation vs. business models.
• Strategy/Factory pattern: pluggable Header classes.
• Template Method: ApiLib defines the request flow; concrete subclasses supply endpoints and models.
• Async I/O: non-blocking HTTP calls (as indicated by async/await usage).