Download the PHP package cubesystems/api-client without Composer
On this page you can find all versions of the php package cubesystems/api-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package api-client
API Client
This package defines contracts and provides abstract partial implementations of those contracts. It is meant to be used as a base building block for your own highly modular and customisable implementation of API consumption. Feel free to use as little or as much of it as you need.
Installation
You can install the package via composer:
Configuration
To use Laravel Debugbar integration, enable it within debugbar.php
config file:
Creating custom API client implementation
First of all, let's define some vocabulary.
SOAP
Methods, services and endpoints
REST
Methods, services and endpoints
After this, we can look at the relations between all interfaces:
Relationship between main interfaces
Define an endpoint
For this, we need to implement CubeSystems\ApiClient\Client\Contracts\Endpoint
contract which can be done by extending CubeSystems\ApiClient\Client\AbstractRestEndpoint
or CubeSystems\ApiClient\Client\AbstractSoapEndpoint
class:
Define a service accessible within an endpoint
For this, we need to implement CubeSystems\ApiClient\Client\Contracts\Service
contract which can be done by extending CubeSystems\ApiClient\Client\AbstractRestService
or CubeSystems\ApiClient\Client\AbstractSoapService
class:
Create a request payload class
This can be done by implementing CubeSystems\ApiClient\Client\Contracts\Payload
contract directly or by extending CubeSystems\ApiClient\Client\Payloads\AbstractPayload
class:
Create a response class
This can be done by implementing CubeSystems\ApiClient\Client\Contracts\Response
contract directly or by extending CubeSystems\ApiClient\Client\Responses\AbstractResponse
class:
Create a method for a service
CubeSystems\ApiClient\Client\Contracts\Method
is implemented by CubeSystems\ApiClient\Client\Methods\AbstractRestMethod
and CubeSystems\ApiClient\Client\Methods\AbstractSoapMethod
classes.
Calling an API
After all that, you can make calls like this:
Or take advantage of dependency injection done by Laravel.
Extend CubeSystems\ApiClient\ApiClientServiceProvider
class and register your bindings:
Now the same thing can be done more concisely:
Take a look at tests for more detailed examples.
Caching
AbstractMethod
class includes a caching mechanism. Just pass desired cache strategy to the constructor. There are 3 strategies available:
CubeSystems\ApiClient\Client\Cache\NeverCacheStrategy
- never cache the responseCubeSystems\ApiClient\Client\Cache\RequestCacheStrategy
- cache the response for the duration of the requestCubeSystems\ApiClient\Client\Cache\TimeIntervalCacheStrategy
- cache the response for a given time interval
You can also create your own strategy by implementing CubeSystems\ApiClient\Client\Contracts\CacheStrategy
contract.
CubeSystems\ApiClient\Client\Contracts\Payload::getCacheKey()
method is used to decide if there are valid cache entries for a given payload. If there are, the response is retrieved from cache. Otherwise, the remote API is called.
Hierarchical caching
If payload returns true
from isUsingCacheHierarchy
method, then corresponding call will be made part of the cache hierarchy. This allows to invalidate all cache entries for given hierarchy at once by calling AbstractMethod
's removeHierarchyFromCache
method.
For this to work, CubeSystems\ApiClient\Client\Contracts\Payload::getCacheHierarchyKey
method must be implemented according to your needs.
Moreover, AbstractPayload
now accepts optional CachePrefix
parameter which can be used to differentiate between cache hierarchies for the same type of method and payload. Typical use case would be to distinguish between different users' cache hierarchies.
There is more
Events
CubeSystems\ApiClient\Client\Methods\AbstractMethod
class fires the following events:
CubeSystems\ApiClient\Events\ApiCalled
- after a remote API is calledCubeSystems\ApiClient\Events\ResponseRetrievedFromCache
- after response is retrieved from cache without calling a remote API
You can listen to those and do some additional stuff like logging.
Laravel Debugbar integration
If debugbar.collectors.api
configuration option is set to true
as described above, all API calls (and cache retrievals) will be visible within Laravel Debugbar:
Debugbar integration
Testing
This package uses Pest testing framework. You can run tests with
Changelog
Please see CHANGELOG for more information on what has changed recently.
All versions of api-client with dependencies
ext-bcmath Version *
ext-intl Version *
ext-soap Version *
ext-xsl Version *
codedredd/laravel-soap Version ^3.0
illuminate/contracts Version ^9.0
nesbot/carbon Version ^2.66
spatie/laravel-package-tools Version ^1.14.0