cobhimself/api-services-bundle
This package, API Services Bundle, is designed to simplify interactions with external APIs in Laravel applications. To begin:
Installation: Requires Composer installation via:
composer require cobhimself/api-services-bundle
Publish the configuration file (if needed) with:
php artisan vendor:publish --provider="Cobhimself\ApiServicesBundle\ApiServicesServiceProvider"
First Use Case: Define an API service in config/api-services.php:
'services' => [
'example' => [
'base_url' => 'https://api.example.com',
'timeout' => 30,
'headers' => [
'Accept' => 'application/json',
],
],
],
Then inject the service into a controller or service class:
use Cobhimself\ApiServicesBundle\Services\ApiService;
public function __construct(ApiService $apiService)
{
$this->apiService = $apiService;
}
Make requests using the fluent API:
$response = $this->apiService->for('example')->get('/endpoint');
Service Configuration:
base_url, timeout, headers, and optional auth (e.g., API keys or OAuth tokens).API_KEY) and reference them in the config:
'auth' => [
'token' => env('API_KEY'),
],
Request Handling:
$this->apiService->for('example')
->withHeaders(['X-Custom-Header' => 'value'])
->get('/data', ['param' => 'value'])
->json();
post(), put(), delete(), and patch() for different HTTP verbs.Response Processing:
->raw(), JSON with ->json(), or throw exceptions on failure:
try {
$data = $this->apiService->for('example')->get('/data')->json();
} catch (\Cobhimself\ApiServicesBundle\Exceptions\ApiException $e) {
// Handle error
}
Middleware Integration:
'middleware' => [
'example' => [
\Cobhimself\ApiServicesBundle\Middleware\LogRequests::class,
],
],
$service = $this->apiService->for('dynamic-service-name');
retry helper.Configuration Overrides:
config/api-services.php are not overridden by cached configs. Clear the config cache after changes:
php artisan config:clear
Authentication Handling:
beforeRequest middleware for this.Rate Limiting:
429 Too Many Requests and implement delays or retries.Exception Handling:
ApiException for HTTP errors (4xx/5xx). Customize error handling in your middleware or controllers.Logging: Enable request/response logging via middleware:
'middleware' => [
'global' => [
\Cobhimself\ApiServicesBundle\Middleware\LogRequests::class,
],
],
Logs will appear in Laravel’s default log channel.
Testing:
Mock the ApiService in tests:
$mock = Mockery::mock(\Cobhimself\ApiServicesBundle\Services\ApiService::class);
$mock->shouldReceive('for')->andReturnSelf();
$mock->shouldReceive('get')->andReturn((object) ['json' => fn() => ['test' => 'data']]);
Custom Request Transformers:
ApiService to add request/response transformers:
$this->apiService->for('example')->transformRequest(function ($data) {
return array_merge($data, ['custom_field' => 'value']);
});
Event Listeners:
api.request and api.response events to intercept requests/responses globally.Service Providers:
$this->app->bind('custom.api', function ($app) {
return new CustomApiService($app['api-service']);
});
How can I help you explore Laravel packages today?