asanak/laravel-web-call-client
Laravel package for Asanak WebCall REST API: upload voice files, place voice or OTP calls, check call status, and get credit. Configure via .env, auto-registers service provider/facade, and optionally logs requests/responses to Laravel logs.
Installation
composer require asanak/laravel-web-call-client
Publish the config file (if needed):
php artisan vendor:publish --provider="Asanak\WebCallClient\WebCallClientServiceProvider"
Configuration
Edit config/web-call-client.php to define your API endpoints and default headers:
'endpoints' => [
'default' => [
'url' => 'https://api.example.com',
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer YOUR_TOKEN',
],
],
],
First API Call Use the facade to make a simple GET request:
use Asanak\WebCallClient\Facades\WebCallClient;
$response = WebCallClient::get('/users');
$data = $response->json();
Quick Reporting Log a response for debugging:
$response = WebCallClient::post('/report', ['data' => 'test']);
$response->report(); // Logs full request/response to storage/logs/web-call-client.log
Structured Requests Use named endpoints for clarity:
$response = WebCallClient::endpoint('default')->get('/users');
Request/Response Chaining Chain methods for fluent API calls:
$response = WebCallClient::get('/users')
->withHeaders(['X-Custom-Header' => 'value'])
->asJson();
Dynamic Endpoints Override endpoints per request:
$response = WebCallClient::endpoint([
'url' => 'https://custom-api.com',
'headers' => ['Authorization' => 'Bearer DYNAMIC_TOKEN'],
])->get('/data');
Error Handling Use exceptions for robust error handling:
try {
$response = WebCallClient::post('/create', ['data' => 'test']);
} catch (\Asanak\WebCallClient\Exceptions\WebCallException $e) {
// Handle error (e.g., log, notify user)
report($e);
}
Laravel HTTP Client Bridge Leverage Laravel’s built-in HTTP client for advanced features:
$response = WebCallClient::get('/users')
->withHttpClient(app(\Illuminate\Http\Client\PendingRequest::class));
Middleware Integration Attach middleware to requests:
$response = WebCallClient::get('/users')
->withMiddleware(new \Asanak\WebCallClient\Middleware\LogRequest);
Testing Mock responses in tests:
$mock = Mockery::mock(\Asanak\WebCallClient\Contracts\Response::class);
$mock->shouldReceive('json')->andReturn(['test' => 'data']);
$this->app->instance(\Asanak\WebCallClient\Contracts\Response::class, $mock);
Header Overrides Headers defined in the config are merged with per-request headers. Unexpected behavior may occur if not explicit:
// Overrides all headers for this request
$response = WebCallClient::get('/users')->withHeaders(['Accept' => 'text/plain']);
Response Parsing
Always check response->successful() before parsing JSON to avoid exceptions:
if ($response->successful()) {
$data = $response->json();
}
Rate Limiting
The package doesn’t handle rate limiting by default. Implement middleware or use Laravel’s retry method:
$response = WebCallClient::get('/users')->retry(3, 100);
Logging Overhead
Frequent report() calls may bloat logs. Use sparingly in production.
Enable Verbose Logging
Set WEB_CALL_CLIENT_LOG_VERBOSE to true in .env for detailed logs.
Inspect Raw Response
Use response->raw() to debug unexpected formats:
$raw = $response->raw();
Check for Deprecated Methods Monitor deprecation notices in logs for breaking changes.
Custom Responses
Extend the Response class to add domain-specific methods:
class CustomResponse extends \Asanak\WebCallClient\Response {
public function getUserId() {
return $this->json()['id'] ?? null;
}
}
Add Middleware
Register custom middleware in config/web-call-client.php:
'middleware' => [
\App\Middleware\CustomAuth::class,
],
Event Listeners Listen for request/response events:
\Asanak\WebCallClient\Events\BeforeRequest::class => [
\App\Listeners\LogBeforeRequest::class,
],
How can I help you explore Laravel packages today?