Installation
composer require artack/mx-api
(Note: Due to the package being archived, verify compatibility with your Laravel version.)
Service Provider & Facade
Add to config/app.php under providers:
Artack\MxApi\MxApiServiceProvider::class,
Publish config (if available):
php artisan vendor:publish --provider="Artack\MxApi\MxApiServiceProvider"
First API Call
use Artack\MxApi\Facades\MxApi;
$response = MxApi::get('/endpoint', ['param' => 'value']);
$data = $response->json();
Configuration
Check .env or config/mx-api.php for:
Accept: application/json)// Fetch a user's profile
$userData = MxApi::get('/users/{id}', ['id' => 123]);
// Handle response
if ($userData->successful()) {
$name = $userData->json()['name'];
} else {
$error = $userData->json()['error'];
}
MxApi::get('/users', ['active' => true]);
MxApi::post('/users', ['name' => 'John', 'email' => 'john@example.com']);
MxApi::withHeaders(['Authorization' => 'Bearer ' . $token])
->get('/protected-route');
$data = MxApi::get('/data')->json();
if (MxApi::get('/status')->ok()) {
// Success logic
}
try {
$response = MxApi::get('/fail');
} catch (\Artack\MxApi\Exceptions\ApiException $e) {
Log::error($e->getMessage());
}
Queue Jobs for Async Calls
dispatch(new FetchMxDataJob($params));
(Assuming the package supports queuing or you wrap it in a job.)
Middleware for API Guard
// app/Http/Middleware/CheckMxApi.php
public function handle($request, Closure $next) {
if (MxApi::get('/health')->failed()) {
abort(503, 'MX API unavailable');
}
return $next($request);
}
Caching Responses
$data = Cache::remember('mx_user_123', now()->addHours(1), function () {
return MxApi::get('/users/123')->json();
});
$mock = Mockery::mock('overload', Artack\MxApi\Facades\MxApi::class);
$mock->shouldReceive('get')
->with('/test')
->andReturn(response()->json(['mocked' => true]));
Archived Package Risks
Error Handling Gaps
try {
$response = MxApi::post('/create', $data);
} catch (\Exception $e) {
throw new \Artack\MxApi\Exceptions\ValidationException(
$response->json()['errors'] ?? $e->getMessage()
);
}
Rate Limiting
use Symfony\Component\HttpClient\RetryStrategy;
$client = MxApi::getClient()
->withOptions([
'max_retries' => 3,
'retry_delay' => 100,
]);
Config Overrides
$api = new \Artack\MxApi\Client(['base_uri' => $dynamicUrl]);
Enable Guzzle Debugging
Add to config/mx-api.php:
'debug' => env('APP_DEBUG', false),
(If supported; otherwise, use a Guzzle middleware.)
Log Raw Responses
$response = MxApi::get('/data');
Log::debug('MX API Response', [
'status' => $response->status(),
'body' => $response->getBody(),
'headers' => $response->headers(),
]);
Custom Request Factories Extend the base client for project-specific needs:
class ProjectMxApi extends \Artack\MxApi\Client {
public function customEndpoint($params) {
return $this->post('/custom', $params)->json();
}
}
Event Listeners Trigger events on API calls (if the package supports it):
// Example: Log all API calls
MxApi::getClient()->on('request', function ($request) {
Log::info('MX API Request', [
'method' => $request->getMethod(),
'uri' => $request->getUri(),
]);
});
Middleware for Requests Add preprocessing/POST-processing:
MxApi::getClient()->getEmitter()->addSubscriber(
new class implements \Symfony\Contracts\HttpClient\EventListener\EventListenerInterface {
public function onEvent(object $event) { /* ... */ }
}
);
Connection Pooling Reuse the Guzzle client instance:
$client = MxApi::getClient(); // Singleton
$response = $client->request('GET', '/data');
Parallel Requests
Use Guzzle’s Promise for concurrent calls:
$promises = [
MxApi::getClient()->request('GET', '/users'),
MxApi::getClient()->request('GET', '/posts'),
];
$results = \GuzzleHttp\Promise\Utils::settle($promises)->wait();
How can I help you explore Laravel packages today?