VOOZH about

URL: https://deepwiki.com/hypervel/api-client/4-core-architecture

⇱ Core Architecture | hypervel/api-client | DeepWiki


Loading...
Menu

Core Architecture

This section provides a comprehensive overview of the fundamental components and design patterns that comprise the Hypervel API Client system. It explains how the main classes interact, the layered architecture, and the flow of data through the system from request creation to response handling.

For step-by-step usage instructions, see Quick Start. For detailed middleware implementation, see Middleware System. For configuration options, see Configuration Reference.

Component Overview

The API client is built around five primary classes that work together to handle HTTP communication:

ComponentFileRoleTemplate Parameters
ApiClientsrc/ApiClient.phpEntry point and configuration hubTConfig, TResource
PendingRequestsrc/PendingRequest.phpRequest orchestrator and middleware executorTResource
ApiRequestN/A (from hypervel/http-client)PSR-7 compliant request builderNone
ApiResponseN/A (from hypervel/http-client)PSR-7 compliant response wrapperNone
ApiResourceN/A (to be documented in 4.7)Response data container with array/object accessNone

Sources: src/ApiClient.php1-116 src/PendingRequest.php1-342

Class Responsibilities

ApiClient (4.3) serves as the factory and configuration container:

PendingRequest (4.4) orchestrates the entire request lifecycle:

Sources: src/ApiClient.php19-45 src/PendingRequest.php18-62 src/PendingRequest.php265-326

Architectural Layers

The system is organized into five distinct layers, each with clear responsibilities:


Sources: src/ApiClient.php14-116 src/PendingRequest.php22-342

Layer Details

Layer 1: Configuration holds all settings including:

Layer 2: Client Factory provides the public API:

Layer 3: Request Orchestration manages the request lifecycle:

Layer 4: HTTP Abstraction provides PSR-7 compatibility:

  • ApiRequest wraps PSR-7 requests with mutable interface
  • ApiResponse wraps PSR-7 responses with mutable interface
  • beforeSending() hook intercepts requests src/PendingRequest.php272-278

Layer 5: Transport handles actual HTTP communication:

Sources: src/ApiClient.php19-115 src/PendingRequest.php175-341

Request-Response Flow

The following diagram shows the complete execution path from application code to API response:


Sources: src/ApiClient.php41-45 src/PendingRequest.php175-307

Flow Steps

  1. Method Delegation: ApiClient::__call() intercepts HTTP method calls src/ApiClient.php41-45
  2. Request Creation: PendingRequest is instantiated with client configuration src/ApiClient.php112-115
  3. HTTP Method Call: Delegated to sendRequest() src/PendingRequest.php177
  4. Client Initialization: getClient() creates ClientPendingRequest via Http::throw() src/PendingRequest.php328-341
  5. Request Transformation: beforeSending() hook wraps PSR-7 in ApiRequest src/PendingRequest.php272-278
  6. Request Middleware: Pipeline processes request if enabled src/PendingRequest.php274-276
  7. HTTP Execution: Transport layer sends request src/PendingRequest.php278
  8. Response Wrapping: Raw PSR-7 response wrapped in ApiResponse src/PendingRequest.php284
  9. Response Middleware: Pipeline processes response if enabled src/PendingRequest.php286-288
  10. Resource Creation: ApiResource::make() creates final data container src/PendingRequest.php290

Sources: src/PendingRequest.php265-291

Type System and Generics

The architecture uses PHP generics (via PHPDoc) to maintain type safety throughout the request-response cycle:


Template Parameters:

Sources: src/ApiClient.php9-24 src/PendingRequest.php18-29 src/PendingRequest.php290

Magic Method Delegation

Both ApiClient and PendingRequest use the __call() magic method to provide fluent interfaces:

ApiClient Delegation src/ApiClient.php41-45:


PendingRequest Delegation src/PendingRequest.php257-263:


This creates a seamless API where configuration methods from the underlying HTTP client are available directly on PendingRequest instances via the @mixin annotation src/PendingRequest.php20

Sources: src/ApiClient.php41-45 src/PendingRequest.php257-263

Middleware Architecture

The middleware system provides two interception points in the request-response cycle:

PipelineProcessesTimingMethod
Request MiddlewareApiRequestBefore HTTP executionhandleMiddlewareRequest() src/PendingRequest.php293-299
Response MiddlewareApiResponseAfter HTTP executionhandleMiddlewareResponse() src/PendingRequest.php301-307

Both pipelines:

For detailed middleware documentation, see Middleware System.

Sources: src/PendingRequest.php31-51 src/PendingRequest.php293-326

Further Reading

This section provides subsections detailing each component:

For practical usage examples, see Making Requests and Response Handling.