Installation
composer require badpixxel/php-sdk
Verify the package loads by checking composer.json for autoloading.
First Use Case: Basic Initialization
use Badpixxel\SDK\Client;
$client = new Client(config([
'api_key' => env('BADPIXXEL_API_KEY'),
'base_uri' => env('BADPIXXEL_API_URL', 'https://api.badpixxel.example/v1'),
]));
src/Client.php and src/Exceptions/ for core logic and error handling.First API Call
try {
$response = $client->get('/resources');
$data = json_decode($response->getBody(), true);
} catch (\Badpixxel\SDK\Exception\ApiException $e) {
// Handle API errors
}
src/ directory (if available).Standard Requests
// GET
$client->get('/endpoint', ['param' => 'value']);
// POST with JSON body
$client->post('/endpoint', ['key' => 'value'], [
'headers' => ['Content-Type' => 'application/json'],
]);
withHeaders() or withAuth() for reusable configurations.Authentication
$client->withAuth(env('API_TOKEN'))->get('/protected');
config/services.php or .env.Pagination
$response = $client->get('/items', ['page' => 1, 'per_page' => 20]);
$data = json_decode($response->getBody(), true);
collect($data['items'])->each(...) for pagination handling.Service Provider Binding
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(Client::class, function ($app) {
return new Client(config('services.badpixxel'));
});
}
config/services.php:
'badpixxel' => [
'api_key' => env('BADPIXXEL_API_KEY'),
'timeout' => 30,
],
Middleware for API Calls
// app/Http/Middleware/HandleBadpixxelApi.php
public function handle($request, Closure $next)
{
$client = app(Client::class);
$response = $client->get('/check-status');
// Process response or redirect
return $next($request);
}
Events and Observers
BadpixxelApiCalled) after each request for logging/auditing.Error Handling
try-catch blocks and log errors:
catch (\Badpixxel\SDK\Exception\ApiException $e) {
Log::error('Badpixxel API Error: ' . $e->getMessage());
}
Rate Limiting
429 Too Many Requests responses.if ($response->getStatusCode() === 429) {
sleep(2 ** $retryCount); // Exponential backoff
}
Deprecated Methods
composer.json:
"badpixxel/php-sdk": "1.0.*"
Enable Guzzle Middleware
$client = new Client([...], [
'middleware' => [
new \GuzzleHttp\Middleware\LogMiddleware(
new \GuzzleHttp\HandlerStack(),
new \Psr\Log\NullLogger()
)
]
]);
storage/logs/laravel.log for raw request/response data.Mocking for Tests
// tests/Feature/BadpixxelTest.php
public function test_api_call()
{
$mockHandler = HandlerStack::create();
$mockHandler->push(Middleware::tap(function ($request) {
return new Response(200, [], json_encode(['test' => true]));
}));
$client = new Client([], ['handler' => $mockHandler]);
$response = $client->get('/test');
$this->assertEquals(200, $response->getStatusCode());
}
Custom Request Classes
Badpixxel\SDK\Request to add domain-specific logic:
class CustomRequest extends \Badpixxel\SDK\Request {
public function withCustomHeader()
{
return $this->withHeader('X-Custom', 'value');
}
}
Response Decorators
$client->get('/data')->then(function ($response) {
return collect(json_decode($response->getBody(), true));
});
Webhook Handling
route:webhook to validate and process incoming webhooks:
Route::post('/badpixxel-webhook', function (Request $request) {
$payload = $request->json()->all();
// Verify signature, process payload
});
How can I help you explore Laravel packages today?