comsolit/imasys-php
PHP client library for the IMAsys platform by Comsolit. Provides helpers to connect to IMAsys services and work with the API from your PHP application, aiming to simplify integration and common requests.
Installation
composer require comsolit/imasys-php
Add the service provider to config/app.php under providers:
Comsolit\Imasys\ImasysServiceProvider::class,
Configuration Publish the config file:
php artisan vendor:publish --provider="Comsolit\Imasys\ImasysServiceProvider" --tag="imasys-config"
Update config/imasys.php with your API credentials (client ID, secret, and endpoint).
First Use Case: Authentication Authenticate via the facade:
use Comsolit\Imasys\Facades\Imasys;
$token = Imasys::auth()->getToken();
Service Initialization Use dependency injection or the facade to instantiate the client:
// In a controller/service
public function __construct(private ImasysClient $imasys) {}
// Or via facade
$response = Imasys::endpoint('patients')->get();
CRUD Operations
$patient = Imasys::endpoint('patients')->create([
'firstName' => 'John',
'lastName' => 'Doe'
]);
$patients = Imasys::endpoint('patients')->get(['limit' => 10]);
Imasys::endpoint('patients/{id}')->update(['status' => 'active']);
Imasys::endpoint('patients/{id}')->delete();
Pagination Handling Loop through paginated results:
$patients = Imasys::endpoint('patients')->get();
foreach ($patients->getCollection() as $patient) {
// Process each patient
}
Error Handling
Wrap API calls in a try-catch:
try {
$response = Imasys::endpoint('patients')->get();
} catch (ImasysException $e) {
Log::error('Imasys API Error: ' . $e->getMessage());
return response()->json(['error' => 'Failed to fetch data'], 500);
}
event(new PatientCreated($patient));
dispatch(new SyncPatientsJob());
$this->mock(Imasys::class)->shouldReceive('endpoint')->andReturnSelf();
Token Expiry
401 Unauthorized responses by re-authenticating:
if ($response->status() === 401) {
Imasys::auth()->refreshToken();
return $this->handleRequest(); // Retry the request
}
Rate Limiting
429 Too Many Requests responses. Implement exponential backoff:
if ($response->status() === 429) {
sleep($retryAfter ?? 1);
return $this->handleRequest();
}
Endpoint Caching
Cache::forget() when needed.Enable Debug Mode
Set debug to true in config/imasys.php to log raw API responses:
'debug' => env('IMASYS_DEBUG', false),
Log Requests Use Laravel’s logging to track API calls:
Imasys::setLogger(function ($message) {
Log::debug($message);
});
Custom Endpoints
Extend the ImasysClient to add domain-specific endpoints:
class CustomImasysClient extends ImasysClient {
public function customEndpoint() {
return $this->request('GET', '/custom');
}
}
Middleware Add middleware to modify requests/responses:
Imasys::middleware(function ($request) {
$request->header('X-Custom-Header', 'value');
});
Response Transformation Override the default response handling:
Imasys::transformer(function ($response) {
return $response->json()['data'];
});
base_url in config/imasys.php ends with a trailing slash (/).connect_timeout and timeout in the config for slow networks:
'timeout' => 30, // seconds
'connect_timeout' => 5,
How can I help you explore Laravel packages today?