alxxc/simple-api-client-bundle
Installation:
composer require alxxc/simple-api-client-bundle:dev-master
Add to config/bundles.php:
return [
// ...
SimpleApiClientBundle\SimpleApiClientBundle::class => ['all' => true],
];
First Use Case: Inject the service into a controller or service:
use SimpleApiClientBundle\Client\SimpleApiClientInterface;
class LocationController extends AbstractController
{
public function __construct(private SimpleApiClientInterface $apiClient) {}
public function getLocations()
{
$url = 'https://api.example.com/locations';
$locations = $this->apiClient->loadLocations($url);
return $this->json($locations);
}
}
Key Files:
config/packages/simple_api_client.yaml (if auto-generated)src/SimpleApiClientBundle/ (core logic)API Integration:
loadLocations($url) to fetch and parse JSON data from a predefined API endpoint.$locations = $this->apiClient->loadLocations('https://api.example.com/geodata');
return $this->render('map.html.twig', ['locations' => $locations]);
Dependency Injection:
public function __construct(SimpleApiClientInterface $client) {}
Caching Responses (if extended):
# config/services.yaml
services:
App\Service\CachedApiClient:
decorates: 'simple_api_client.client'
arguments: ['@.inner']
Error Handling:
try {
$data = $this->apiClient->loadLocations($url);
} catch (\RuntimeException $e) {
$this->addFlash('error', 'Failed to fetch data: ' . $e->getMessage());
}
Configuration:
config/packages/simple_api_client.yaml:
simple_api_client:
default_timeout: 30
user_agent: 'MyApp/1.0'
No Built-in Rate Limiting:
$client = new \GuzzleHttp\Client([
'timeout' => 10,
'headers' => ['User-Agent' => 'MyApp/1.0'],
]);
Assumes JSON Structure:
loadLocations() method expects a specific JSON format. Validate responses:
$data = $this->apiClient->loadLocations($url);
if (!isset($data['locations'])) {
throw new \RuntimeException('Invalid API response format');
}
Symfony 3.4 Only:
No Retry Logic:
use GuzzleHttp\Exception\RequestException;
try {
$this->apiClient->loadLocations($url);
} catch (RequestException $e) {
if ($e->hasResponse() && $e->getResponse()->getStatusCode() === 503) {
sleep(2); // Retry after delay
return $this->getLocations(); // Recursive call
}
}
Extend the Client:
namespace App\Service;
use SimpleApiClientBundle\Client\SimpleApiClientInterface;
class EnhancedApiClient implements SimpleApiClientInterface
{
public function __construct(private SimpleApiClientInterface $decorated) {}
public function loadLocations(string $url): array
{
$data = $this->decorated->loadLocations($url);
return $this->transformData($data); // Add custom logic
}
}
Logging:
# config/services.yaml
services:
simple_api_client.client:
calls:
- [setLogger, ['@logger']]
Testing:
$mockClient = $this->createMock(SimpleApiClientInterface::class);
$mockClient->method('loadLocations')->willReturn(['test' => 'data']);
$this->container->set('simple_api_client.client', $mockClient);
Configuration Overrides:
services:
simple_api_client.client:
arguments:
$client: '@custom.guzzle.client'
How can I help you explore Laravel packages today?