Skip to content

Latest commit

 

History

History
70 lines (53 loc) · 1.79 KB

File metadata and controls

70 lines (53 loc) · 1.79 KB

LibHttp Examples

The snippets below cover individual API pieces. The complete api_webhook_download.php example shows a more realistic plugin integration that combines external JSON APIs, Discord webhooks and checksum-verified downloads.

Example PHP files are linted by the repository quality workflow. Keep them copyable and avoid committing real webhook URLs, tokens or private API keys.

JSON POST

use imperazim\http\client\JsonHttpClient;
use imperazim\http\HttpRequest;

$client = new JsonHttpClient();
$payload = $client->sendJson(HttpRequest::json('POST', 'https://example.com/api', [
    'server' => 'lobby',
]));

Bearer Auth

use imperazim\http\auth\HttpAuth;
use imperazim\http\HttpRequest;

$request = HttpRequest::get('https://example.com/api/private')
    ->withHeaders(HttpAuth::bearerToken($token));

Default Headers And Rate Limit

use imperazim\http\client\HttpClient;
use imperazim\http\interceptor\DefaultHeadersInterceptor;
use imperazim\http\rate\FixedWindowRateLimiter;

$client = new HttpClient(
    interceptors: [new DefaultHeadersInterceptor(['User-Agent' => 'MyPlugin/1.0'])],
    rateLimiter: FixedWindowRateLimiter::perSecond(5)
);

Typed Errors

use imperazim\http\exception\StatusHttpException;
use imperazim\http\exception\TransportHttpException;

try {
    $client->sendOrFail($request);
} catch (TransportHttpException) {
    // Connection, DNS, timeout or TLS failure.
} catch (StatusHttpException $error) {
    $status = $error->getResponse()->getStatusCode();
}

Async Request

use imperazim\http\async\AsyncHttpClient;
use imperazim\http\HttpRequest;

(new AsyncHttpClient())->send(HttpRequest::get('https://example.com/ping'), function($response): void {
    // Main thread callback.
});