ccproxy.http¶
ccproxy.http
¶
HTTP package for CCProxy - consolidated HTTP functionality.
BaseHTTPHandler
¶
Bases: ABC
Abstract base class for HTTP handlers with common functionality.
handle_request
abstractmethod
async
¶
Handle an HTTP request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
HTTP method |
required |
url
|
str
|
Target URL |
required |
headers
|
dict[str, str]
|
Request headers |
required |
body
|
bytes
|
Request body |
required |
handler_config
|
HandlerConfig
|
Handler configuration |
required |
**kwargs
|
Any
|
Additional handler-specific arguments |
{}
|
Returns:
| Type | Description |
|---|---|
Response | StreamingResponse | DeferredStreaming
|
Response or StreamingResponse |
Source code in ccproxy/http/base.py
prepare_request
abstractmethod
async
¶
Prepare request for sending.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request_body
|
bytes
|
Original request body |
required |
handler_config
|
HandlerConfig
|
Handler configuration |
required |
**kwargs
|
Any
|
Additional preparation parameters |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[bytes, dict[str, str], bool]
|
Tuple of (transformed_body, headers, is_streaming) |
Source code in ccproxy/http/base.py
cleanup
async
¶
Cleanup handler resources.
Default implementation does nothing. Override in subclasses if cleanup is needed.
HTTPClientFactory
¶
Factory for creating optimized HTTP clients.
Provides centralized configuration for HTTP clients with: - Consistent timeout/retry configuration - Unified connection limits - HTTP/2 multiplexing for non-streaming endpoints - Centralized observability hooks (via HookableHTTPClient)
create_client
staticmethod
¶
create_client(
*,
settings=None,
timeout_connect=5.0,
timeout_read=240.0,
max_keepalive_connections=100,
max_connections=1000,
http2=True,
verify=True,
hook_manager=None,
**kwargs,
)
Create an optimized HTTP client with recommended configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Settings | None
|
Optional settings object for additional configuration |
None
|
timeout_connect
|
float
|
Connection timeout in seconds |
5.0
|
timeout_read
|
float
|
Read timeout in seconds (long for streaming) |
240.0
|
max_keepalive_connections
|
int
|
Max keep-alive connections for reuse |
100
|
max_connections
|
int
|
Max total concurrent connections |
1000
|
http2
|
bool
|
Enable HTTP/2 multiplexing |
True
|
verify
|
bool | str
|
SSL verification (True/False or path to CA bundle) |
True
|
hook_manager
|
Any | None
|
Optional HookManager for request/response interception |
None
|
**kwargs
|
Any
|
Additional httpx.AsyncClient arguments |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncClient
|
Configured httpx.AsyncClient instance |
Source code in ccproxy/http/client.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
create_shared_client
staticmethod
¶
Create an optimized HTTP client.
Prefer managing lifecycle via ServiceContainer + HTTPPoolManager. Kept for compatibility with existing factory call sites.
Source code in ccproxy/http/client.py
create_short_lived_client
staticmethod
¶
Create a client for short-lived operations like version checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float
|
Short timeout for quick operations |
15.0
|
**kwargs
|
Any
|
Additional client configuration |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncClient
|
Configured httpx.AsyncClient instance for short operations |
Source code in ccproxy/http/client.py
managed_client
async
staticmethod
¶
Create a managed HTTP client with automatic cleanup.
This context manager ensures proper cleanup of HTTP clients in error cases and provides a clean resource management pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Settings | None
|
Optional settings for configuration |
None
|
**kwargs
|
Any
|
Additional client configuration |
{}
|
Yields:
| Type | Description |
|---|---|
AsyncGenerator[AsyncClient, None]
|
Configured httpx.AsyncClient instance |
Example
async with HTTPClientFactory.managed_client() as client: response = await client.get("https://api.example.com")
Source code in ccproxy/http/client.py
HTTPConnectionError
¶
HTTPError
¶
HTTPTimeoutError
¶
HookableHTTPClient
¶
Bases: AsyncClient
HTTP client wrapper that emits hooks for all requests/responses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Arguments for httpx.AsyncClient |
()
|
hook_manager
|
Any | None
|
Optional HookManager instance for emitting hooks |
None
|
**kwargs
|
Any
|
Keyword arguments for httpx.AsyncClient |
{}
|
Source code in ccproxy/http/hooks.py
request
async
¶
request(
method,
url,
*,
content=None,
data=None,
files=None,
params=None,
headers=None,
json=None,
**kwargs,
)
Make an HTTP request with hook emissions.
Emits
- HTTP_REQUEST before sending
- HTTP_RESPONSE after receiving response
- HTTP_ERROR on errors
Source code in ccproxy/http/hooks.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | |
stream
async
¶
stream(
method,
url,
*,
content=None,
data=None,
files=None,
params=None,
headers=None,
json=None,
**kwargs,
)
Make a streaming HTTP request with hook emissions.
This method emits HTTP hooks for streaming requests, capturing the complete response body while maintaining streaming behavior.
Emits
- HTTP_REQUEST before sending
- HTTP_RESPONSE after receiving complete response
- HTTP_ERROR on errors
Source code in ccproxy/http/hooks.py
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 | |
HTTPPoolManager
¶
Manages HTTP connection pools for different base URLs.
This manager ensures that: - Each unique base URL gets its own optimized connection pool - Connection pools are reused across all components - Resources are properly cleaned up on shutdown - Configuration is consistent across all clients
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
Settings | None
|
Optional application settings for configuration |
None
|
hook_manager
|
Any | None
|
Optional hook manager for request/response tracing |
None
|
Source code in ccproxy/http/pool.py
get_client
async
¶
Get or create an HTTP client for the specified base URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_url
|
str | None
|
Optional base URL for the client. If None, returns the default client |
None
|
timeout
|
float | None
|
Optional custom timeout for this client |
None
|
headers
|
dict[str, str] | None
|
Optional default headers for this client |
None
|
**kwargs
|
Any
|
Additional configuration for the client |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncClient
|
Configured httpx.AsyncClient instance |
Source code in ccproxy/http/pool.py
get_shared_client
async
¶
Get the default general-purpose HTTP client.
This client is used for requests without a specific base URL and is managed by this pool manager for reuse during the app lifetime.
Returns:
| Type | Description |
|---|---|
AsyncClient
|
The default httpx.AsyncClient instance |
Source code in ccproxy/http/pool.py
get_streaming_client
async
¶
Get or create a client optimized for streaming.
Uses a longer read timeout appropriate for SSE/streaming endpoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_url
|
str | None
|
Optional base URL for the client |
None
|
headers
|
dict[str, str] | None
|
Optional default headers |
None
|
**kwargs
|
Any
|
Additional client kwargs merged into configuration |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncClient
|
Configured httpx.AsyncClient instance |
Source code in ccproxy/http/pool.py
get_shared_client_sync
¶
Get or create the default client synchronously.
This is used during initialization when we're not in an async context. Note: This doesn't use locking, so it should only be called during single-threaded initialization.
Returns:
| Type | Description |
|---|---|
AsyncClient
|
The default httpx.AsyncClient instance |
Source code in ccproxy/http/pool.py
get_pool_client
¶
Get an existing client for a base URL without creating one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_url
|
str
|
The base URL to look up |
required |
Returns:
| Type | Description |
|---|---|
AsyncClient | None
|
Existing client or None if not found |
Source code in ccproxy/http/pool.py
close_pool
async
¶
Close and remove a specific connection pool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_url
|
str
|
The base URL of the pool to close |
required |
Source code in ccproxy/http/pool.py
close_all
async
¶
Close all connection pools and clean up resources.
This should be called during application shutdown.
Source code in ccproxy/http/pool.py
get_proxy_url
¶
Get proxy URL from environment variables.
Returns:
| Type | Description |
|---|---|
str | None
|
str or None: Proxy URL if any proxy is set |
Source code in ccproxy/http/client.py
get_ssl_context
¶
Get SSL context configuration from environment variables.
Returns:
| Type | Description |
|---|---|
str | bool
|
SSL verification configuration: |
str | bool
|
|
str | bool
|
|
str | bool
|
|