Installation:
composer require canaltp/tyr-component:~1.2
Ensure your Laravel project uses Guzzle 5 (default) or Guzzle 3 (legacy).
Basic Initialization:
use CanalTP\TyrComponent\TyrService;
$tyrUrl = config('services.tyr.url'); // e.g., 'http://tyr.dev.canaltp.fr/v0/'
$endpointId = config('services.tyr.endpoint_id'); // e.g., 2
$tyrApi = new TyrService($tyrUrl, $endpointId);
First Use Case: Create a user via the API:
$user = $tyrApi->createUser('user@example.com', 'johndoe');
$response = $tyrApi->getLastResponse();
$status = $response->getStatusCode(); // Check success (200/201)
TyrService.php: Core class with all API endpoints (e.g., createUser, updateUser).config/services.php: Store tyr.url and endpoint_id for Laravel config.Register the service in a Service Provider for reusable access:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton('tyr', function ($app) {
return new TyrService(
config('services.tyr.url'),
config('services.tyr.endpoint_id')
);
});
}
Usage in Controllers:
use Illuminate\Support\Facades\App;
$tyrApi = App::make('tyr');
$user = $tyrApi->createUser('test@example.com', 'testuser');
CRUD Operations:
// Create
$tyrApi->createUser('email', 'login');
// Read (assuming a `getUser` method exists)
$tyrApi->getUser($userId);
// Update
$tyrApi->updateUser($userId, ['email' => 'new@example.com']);
// Delete (if supported)
$tyrApi->deleteUser($userId);
Error Handling:
try {
$tyrApi->createUser('invalid-email');
} catch (\GuzzleHttp\Exception\RequestException $e) {
$response = $e->getResponse();
$error = json_decode($response->getBody(), true);
Log::error("Tyr API Error: " . $error['message']);
}
Response Inspection:
$response = $tyrApi->getLastResponse();
if ($response->getStatusCode() !== 200) {
throw new \RuntimeException("API request failed");
}
throttle:60,1).$tyrApi->getLastResponse()->getBody()->rewind();
Log::debug("Tyr API Response: " . $tyrApi->getLastResponse()->getBody());
Mockery to stub Guzzle responses:
$mock = Mockery::mock('overload:' . TyrService::class);
$mock->shouldReceive('getLastResponse')->andReturn(new \GuzzleHttp\Psr7\Response(200));
Guzzle Version Conflicts:
guzzlehttp/guzzle:^5.3 in composer.json or use the Guzzle3\TyrService class.Deprecated Methods:
appName was removed in v1.1.0; ensure no legacy code references it.No Official Laravel Integration:
Outdated Releases:
$status = $tyrApi->getLastResponse()->getStatusCode();
if ($status >= 400) {
$body = json_decode($tyrApi->getLastResponse()->getBody(), true);
throw new \RuntimeException("Tyr API Error: " . $body['error']);
}
$client = new \GuzzleHttp\Client(['debug' => true]);
$tyrApi->setClient($client);
Configuration:
Store API credentials in .env:
TYR_URL=http://tyr.dev.canaltp.fr/v0/
TYR_ENDPOINT_ID=2
Then access via config('services.tyr').
Extending the Package:
TyrService to add custom endpoints:
class CustomTyrService extends TyrService {
public function customEndpoint($data) {
return $this->client->post($this->url . 'custom', ['json' => $data]);
}
}
Testing:
Http facade to mock API calls:
$response = Http::fake([
'tyr.dev.canaltp.fr/v0/*' => Http::response(['success' => true], 200),
]);
Performance:
TyrService instance (singleton pattern) to avoid reinitializing Guzzle clients.License Note:
How can I help you explore Laravel packages today?