Installation Add the bundle via Composer in a Laravel project (via Symfony bridge if needed):
composer require digipolisgent/sock-api-bundle
Register the bundle in config/app.php (Symfony) or adapt the bridge layer for Laravel.
Configuration
Locate the default config in config/sock_api.yaml (Symfony) or equivalent Laravel config file. Key settings:
sock_api:
base_uri: "https://api.example.com/sock"
api_key: "your_api_key_here"
timeout: 30
Publish the config if needed:
php artisan vendor:publish --tag=sock-api-config
First Use Case: API Client Initialization Inject the client into a Laravel service or controller:
use DigipolisGent\SockApiBundle\Client\SockApiClient;
class MyService {
protected $sockClient;
public function __construct(SockApiClient $sockClient) {
$this->sockClient = $sockClient;
}
}
Call an endpoint (e.g., GET /users):
$response = $this->sockClient->get('/users');
$data = json_decode($response->getBody(), true);
RESTful Endpoint Calls Use the client for standard HTTP methods:
// GET
$this->sockClient->get('/endpoint', ['param' => 'value']);
// POST
$this->sockClient->post('/endpoint', ['data' => 'payload']);
// PUT/PATCH/DELETE
$this->sockClient->put('/endpoint', ['data' => 'payload']);
Authentication
$this->sockClient->withHeaders(['Authorization' => 'Bearer token'])->get('/secure');
Error Handling Wrap calls in a try-catch:
try {
$response = $this->sockClient->get('/endpoint');
} catch (\DigipolisGent\SockApiBundle\Exception\SockApiException $e) {
Log::error("SOCK API Error: " . $e->getMessage());
throw new \RuntimeException("Failed to fetch data", 0, $e);
}
Laravel Integration
AppServiceProvider:
$this->app->bind(SockApiClient::class, function ($app) {
return new SockApiClient($app['config']['sock_api']);
});
// app/Facades/SockApi.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class SockApi extends Facade { protected static function getFacadeAccessor() { return 'sock.api'; } }
Register in AppServiceProvider:
$this->app->singleton('sock.api', function ($app) {
return new \App\Facades\SockApi($app->make(SockApiClient::class));
});
Batch Operations Use loops for bulk requests (e.g., fetching multiple records):
$ids = [1, 2, 3];
$results = [];
foreach ($ids as $id) {
$results[$id] = $this->sockClient->get("/users/{$id}");
}
Deprecated Package
No Laravel-Specific Docs
Error Handling Gaps
SockApiException) may lack details. Extend or log raw responses:
catch (\RuntimeException $e) {
$response = $e->getPrevious()->getResponse();
Log::debug("Raw response: " . $response->getBody());
}
Configuration Overrides
base_uri) may conflict with Laravel’s dynamic config. Override via:
$client = new SockApiClient([
'base_uri' => config('services.sock_api.uri'),
'api_key' => config('services.sock_api.key'),
]);
Enable Guzzle Debugging Add to config:
sock_api:
debug: true
Or manually:
$client = new SockApiClient([...], new \GuzzleHttp\Client(['debug' => true]));
Mocking for Tests
Use Laravel’s HTTP client or mock the SockApiClient:
$mock = Mockery::mock(SockApiClient::class);
$mock->shouldReceive('get')->andReturn((object)['body' => '{"test":1}']);
Timeouts Default timeout (30s) may be too short for slow APIs. Adjust:
sock_api:
timeout: 60
Custom Middleware Add request/response middleware via Guzzle:
$client = new SockApiClient([...], new \GuzzleHttp\Client([
'middleware' => [new \App\Middleware\AddCustomHeader()]
]));
Response Transformers Extend the client to auto-transform responses:
class ExtendedSockApiClient extends SockApiClient {
public function get($endpoint, array $params = []) {
$response = parent::get($endpoint, $params);
return json_decode($response->getBody(), true);
}
}
Event Dispatching
Trigger Laravel events on API calls (e.g., sock.api.request):
event(new SockApiRequestEvent($request, $endpoint));
How can I help you explore Laravel packages today?