Skip to content

Commit 7802b5e

Browse files
committed
Added HTTP docs
1 parent 3e8c8d2 commit 7802b5e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+14609
-4
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
---
2+
title: Overview
3+
description: 'Unified HTTP client API for PHP, designed for LLM interactions. Framework agnostic, middleware support, and streaming capabilities.'
4+
---
5+
6+
## Purpose and Goals
7+
8+
The unified HTTP client API is designed to provide a consistent interface for making HTTP requests across different PHP environments, regardless of the underlying HTTP client implementation. This library is a part of the Instructor project, which is a developer-friendly library for working with LLM structured outputs supporting various LLM (Large Language Model) providers and modes of structured data extraction.
9+
10+
The primary goals of the API are:
11+
12+
- **Easily Switch Between HTTP Clients**: Allow developers to switch between different HTTP client libraries (like Guzzle, Symfony, and Laravel) without changing the code that makes HTTP requests
13+
- **Framework Agnostic**: Work seamlessly in Laravel, Symfony, or any PHP application without framework-specific dependencies
14+
- **Consistent Interface**: Provide a unified way to make HTTP requests regardless of the underlying client library
15+
- **Middleware Support**: Enable easy extension through a powerful middleware system
16+
- **Adaptability**: Allow switching between different HTTP client implementations with minimal code changes
17+
- **Streaming Support**: Provide first-class support for streaming responses, which is crucial for LLM interactions
18+
- **Concurrency**: Support parallel requests through request pooling
19+
20+
By abstracting away the differences between various HTTP client libraries, the unified HTTP client API allows developers to write code that works across different environments and frameworks without modification.
21+
22+
## Key Features
23+
24+
### Multiple Client Support
25+
26+
The API supports multiple HTTP client libraries through specialized drivers:
27+
28+
- **Guzzle**: A popular and feature-rich HTTP client for PHP
29+
- **Symfony HTTP Client**: The HTTP client component from the Symfony framework
30+
- **Laravel HTTP Client**: The HTTP client built into the Laravel framework
31+
32+
### Middleware System
33+
34+
A powerful middleware architecture allows for:
35+
36+
- **Request Pre-processing**: Modify requests before they are sent
37+
- **Response Post-processing**: Transform or analyze responses
38+
- **Response Streaming**: Process streaming responses chunk by chunk
39+
- **Debugging**: Log requests and responses for troubleshooting
40+
- **Custom Behaviors**: Add specialized behaviors like caching or rate limiting
41+
42+
### Streaming Response Support
43+
44+
First-class support for streaming HTTP responses, which is essential for:
45+
46+
- **LLM Text Generation**: Process token-by-token responses from AI models
47+
- **Large File Downloads**: Handle large files without excessive memory usage
48+
- **Real-time Data**: Process server-sent events or other real-time data streams
49+
50+
### Request Pooling
51+
52+
Execute multiple HTTP requests concurrently for better performance:
53+
54+
- **Concurrent Execution**: Send multiple requests in parallel
55+
- **Configurable Concurrency**: Control the maximum number of concurrent requests
56+
- **Result Collection**: Process results as they arrive
57+
- **Error Handling**: Flexible error handling strategies
58+
59+
### Flexible Configuration
60+
61+
Comprehensive configuration options:
62+
63+
- **Per-Client Configuration**: Different settings for each client type
64+
- **Named Configurations**: Multiple configurations for different use cases
65+
- **Runtime Configuration**: Change configuration during execution
66+
- **Timeout Controls**: Fine-grained control over various timeout settings
67+
68+
### Debug and Testing Support
69+
70+
Built-in features for debugging and testing:
71+
72+
- **Request/Response Logging**: Detailed logging of HTTP interactions
73+
- **Mock Client**: Test your code without making actual HTTP requests
74+
- **Record/Replay**: Record HTTP interactions and replay them later
75+
76+
## Architecture Overview
77+
78+
The unified HTTP client API follows a layered architecture with several key components:
79+
80+
### Client Layer
81+
82+
The `HttpClient` class serves as the main entry point and provides a fluent interface for configuring and using the HTTP client.
83+
84+
```
85+
HttpClient
86+
└── withClient() - Switch to a different client configuration
87+
└── withConfig() - Apply custom configuration
88+
└── withDriver() - Use a custom driver
89+
└── withMiddleware() - Add middleware components
90+
└── handle() - Process an HTTP request
91+
```
92+
93+
### Middleware Layer
94+
95+
The middleware system allows for processing requests and responses through a chain of handlers:
96+
97+
```
98+
Request -> Middleware 1 -> Middleware 2 -> ... -> Driver -> External API
99+
100+
Response <- Middleware 1 <- Middleware 2 <- ... <- Driver <- HTTP Response
101+
```
102+
103+
Key components:
104+
- `MiddlewareStack`: Manages the collection of middleware
105+
- `MiddlewareHandler`: Orchestrates the middleware chain execution
106+
- `BaseMiddleware`: Base class for implementing middleware
107+
108+
### Driver Layer
109+
110+
Drivers implement the `CanHandleHttpRequest` interface and adapt different HTTP client libraries:
111+
112+
```
113+
CanHandleHttpRequest (interface)
114+
├── GuzzleDriver
115+
├── SymfonyDriver
116+
├── LaravelDriver
117+
└── MockHttpDriver (for testing)
118+
```
119+
120+
### Adapter Layer
121+
122+
Response adapters convert client-specific responses to a common interface:
123+
124+
```
125+
HttpClientResponse (interface)
126+
├── PsrHttpResponse (Guzzle)
127+
├── SymfonyHttpResponse
128+
├── LaravelHttpResponse
129+
└── MockHttpResponse
130+
```
131+
132+
## Supported HTTP Clients
133+
134+
### Guzzle HTTP Client
135+
136+
The [Guzzle HTTP Client](https://docs.guzzlephp.org/) is a powerful HTTP client library for PHP. It provides:
137+
138+
- Promise-based asynchronous requests
139+
- PSR-7 HTTP message implementation
140+
- Middleware system
141+
- Request and response plugins
142+
- HTTP/2 support (via cURL)
143+
144+
The `GuzzleDriver` adapts Guzzle to the unified HTTP client API interface.
145+
146+
### Symfony HTTP Client
147+
148+
The [Symfony HTTP Client](https://symfony.com/doc/current/http_client.html) is a component of the Symfony framework. Features include:
149+
150+
- Asynchronous requests
151+
- HTTP/2 push support
152+
- PSR-18 compatibility
153+
- Automatic content-type detection
154+
- Proxy support
155+
156+
The `SymfonyDriver` adapts the Symfony HTTP Client to the unified HTTP client API.
157+
158+
### Laravel HTTP Client
159+
160+
The [Laravel HTTP Client](https://laravel.com/docs/http-client) is built into the Laravel framework and provides:
161+
162+
- Fluent, readable syntax
163+
- Request macros
164+
- Automatic JSON handling
165+
- Rate limiting
166+
- Retry logic
167+
168+
The `LaravelDriver` adapts the Laravel HTTP Client to the unified HTTP client API.
169+
170+
### Mock HTTP Driver
171+
172+
The `MockHttpDriver` provides a test double for unit testing. It doesn't make actual HTTP requests but returns predefined responses based on matching rules.

0 commit comments

Comments
 (0)