emporium / emporium-api-client
Emporium RESTFul API Client
Requires
- guzzlehttp/guzzle: ^6.2
- krak/array: ^0.4.1
Requires (Dev)
- illuminate/container: ^5.4
- illuminate/support: ^5.4
- peridot-php/peridot: ^1.18
- phake/phake: ^2.3
- pimple/pimple: ^3.0
README
PHP client and interface into the Emporium APIs.
Installation
Install with composer:
{
"repositories": [
{
"type": "vcs",
"url": "git@gitlab.venue.com:emporium/php-api-client.git"
}
],
"require": {
"emporium/php-api-client": "^0.2"
}
}
Usage
Create an Api Client
<?php
$payment = Emporium\Api\Payment\PaymentApiClient::create('http://svc-payment-stg.emporium.com/', getenv('API_TOKEN'));
Call an API Method
<?php
$resp = $payment->getSource(1);
For more documentation view each API's documentation and the Api interfaces in the codebase.
Making one off requests
<?php
$resp = $payment->request('GET', 'sources', [
'query' => [
'user_id' => '1',
]
]);
// this will perform a request to the API with proper authentication.
The array of options are the same request options supported by the GuzzleHttp Client.
Note that there is no leading slash. Adding a leading slash could mess up the path to the API resource.
Using Responses
<?php
if ($resp->isOk()) {
} else if ($resp->isError()) {
} else if ($resp->getStatus() == 302) {
}
$body = $resp->getBody(); // returns parsed body for json or a PSR Stream
$id = $resp['id']; // accesses the parsed body
$card_number = $resp['card.number']; // access $body['card']['number']
Paged Result Iteration
Certain API endpoints return paged results. The PagedResultIterator
provides a convenient way to iterate the collection using PHP iterators.
<?php
use Emporium\Api;
$order_api = Api\Order\OrderApiClient::create($base_uri, $token);
$orders = new Api\PagedResultIterator(function($page) use ($order_api) {
return $order_api->search('order.id < 50', 'order.created_at DESC', $with = 0, $page, $per_page = 10);
});
// this will iterate over all the results and automatically page the result set.
foreach ($orders as $order) {
}
Providers
We support Pimple and Laravel
Pimple
<?php
use Emporium\Api;
$c->register(new Api\Provider\Pimple\EmporiumApiServiceProvider(), [
'emporium.api.env' => 'stg', // or prd
'emporium.api.payment.token' => '', // specific token for payment api
'emporium.api.token' => '', // default token for all services
]);
$payment = $c[Api\Payment\PaymentApi::class];
In addition to defining parameters, you can also define env vars for defining the token and base uri per service.
Laravel
<?php
use Emporium\Api\Provider\Laravel\EmporiumApiServiceProvider;
$app->register(EmporiumApiServiceProvider::class);
$app['emporium.api.env'] = 'stg'; // or prd
$app['emporium.api.payment.token'] = ''; // specific token for payment api
$app['emporium.api.token'] = ''; // default token for all services
$app['emporium.api.order.base_uri'] = 'https://stage.emporium.com/api/'; // specific uri for order service, (MAKE SURE IT ENDS WITH A SLASH)
In addition to defining parameters, you can also define env vars for the specific token and base uri per service.