{"id":18814290,"url":"https://github.com/cubesystems/api-client","last_synced_at":"2025-09-02T17:32:40.061Z","repository":{"id":212359001,"uuid":"731227331","full_name":"cubesystems/api-client","owner":"cubesystems","description":null,"archived":false,"fork":false,"pushed_at":"2024-02-06T01:46:45.000Z","size":719,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-08-28T06:18:09.241Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cubesystems.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-13T16:00:46.000Z","updated_at":"2023-12-13T16:01:58.000Z","dependencies_parsed_at":"2023-12-13T21:35:45.435Z","dependency_job_id":"598f603b-fb16-43a8-80e3-5928d09581da","html_url":"https://github.com/cubesystems/api-client","commit_stats":null,"previous_names":["cubesystems/api-client"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/cubesystems/api-client","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cubesystems%2Fapi-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cubesystems%2Fapi-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cubesystems%2Fapi-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cubesystems%2Fapi-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cubesystems","download_url":"https://codeload.github.com/cubesystems/api-client/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cubesystems%2Fapi-client/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273322145,"owners_count":25085029,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-02T02:00:09.530Z","response_time":77,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T23:40:03.801Z","updated_at":"2025-09-02T17:32:39.747Z","avatar_url":"https://github.com/cubesystems.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# API Client\n\nThis 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.\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require cubesystems/api-client\n```\n\n## Configuration\n\nTo use Laravel Debugbar integration, enable it within `debugbar.php` config file:\n\n```php\nreturn [\n    ...\n    'collectors' =\u003e [\n        ...\n        'api' =\u003e true\n    ]\n];\n```\n\n## Creating custom API client implementation\n\nFirst of all, let's define some vocabulary.\n\n### SOAP\n![Method vs service vs endpoint](images/soap.png) *Methods, services and endpoints*\n\n### REST\n\n![Method vs service vs endpoint](images/rest.png) *Methods, services and endpoints*\n\nAfter this, we can look at the relations between all interfaces:\n\n![Interfaces](images/er.png) *Relationship between main interfaces*\n\n### Define an endpoint\n\nFor 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:\n\n```php\nuse CubeSystems\\ApiClient\\Client\\AbstractSoapEndpoint;\n\nclass MyEndpoint extends AbstractSoapEndpoint {}\n```\n\n### Define a service accessible within an endpoint\n\nFor 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:\n\n```php\nuse CubeSystems\\ApiClient\\Client\\AbstractSoapService;\n\nclass MyService extends AbstractSoapService\n{\n    protected const SERVICE_PATH = 'path/to/service';\n}\n```\n\n### Create a request payload class\n\nThis can be done by implementing `CubeSystems\\ApiClient\\Client\\Contracts\\Payload` contract directly or by extending `CubeSystems\\ApiClient\\Client\\Payloads\\AbstractPayload` class:\n```php\nuse CubeSystems\\ApiClient\\Client\\Payloads\\AbstractPayload;\n\nclass MyPayload extends AbstractPayload\n{\n    private string $parameter;\n\n    public function setId(string $id): MyPayload\n    {\n        $this-\u003eid = $id;\n\n        return $this;\n    }\n\n    public function toArray(): array\n    {\n        return [\n            'id' =\u003e $this-\u003eid\n        ];\n    }\n\n    public function getCacheKey(): string\n    {\n        return self::class . $this-\u003eid;\n    }\n}\n```\n\n### Create a response class\n\nThis can be done by implementing `CubeSystems\\ApiClient\\Client\\Contracts\\Response` contract directly or by extending `CubeSystems\\ApiClient\\Client\\Responses\\AbstractResponse` class:\n\n```php\nuse CubeSystems\\ApiClient\\Client\\Responses\\AbstractResponse;\n\nclass MyResponse extends AbstractResponse\n{\n    private MyDto $myDto;\n\n    public function getMyDto(): MyDto\n    {\n        return $this-\u003emyDto;\n    }\n\n    public function setMyDto(MyDto $myDto): MyResponse\n    {\n        $this-\u003emyDto = $myDto;\n\n        return $this;\n    }\n}\n```\n\n### Create a method for a service\n\n`CubeSystems\\ApiClient\\Client\\Contracts\\Method` is implemented by `CubeSystems\\ApiClient\\Client\\Methods\\AbstractRestMethod` and `CubeSystems\\ApiClient\\Client\\Methods\\AbstractSoapMethod` classes.\n\n```php\nuse CubeSystems\\ApiClient\\Client\\Methods\\AbstractSoapMethod;\nuse CubeSystems\\ApiClient\\Client\\Plugs\\PlugManager;\nuse CubeSystems\\ApiClient\\Client\\Strategies\\NeverCacheStrategy;\nuse Illuminate\\Support\\Arr;\n\nclass MyMethod extends AbstractSoapMethod\n{\n    protected const METHOD_NAME = 'MyMethod';\n\n    public function __construct(\n        MyService $service,\n        NeverCacheStrategy $cacheStrategy,\n        PlugManager $plugManager,\n    ) {\n        parent::__construct($service, $cacheStrategy, $plugManager);\n    }\n    \n    protected function toResponse(array $rawResponse, int $httpCode): MyResponse\n    {\n        $response = new MyResponse();\n        ...\n        $myDto = new MyDto();\n        $myDto-\u003esetName(Arr::get($rawResponse, 'name'));\n        $myDto-\u003esetAge((int) Arr::get($rawResponse, 'age'));\n        $response-\u003esetDto($myDto);\n        \n        return $response;\n    }\n}\n```\n\n## Calling an API\n\nAfter all that, you can make calls like this:\n\n```php\nuse Foo\\Endpoints\\MyEndpoint;\nuse Foo\\Services\\MyService;\nuse Foo\\Methods\\MyMethod;\nuse Foo\\Payloads\\MyPayload;\n\nclass MyRepository\n{\n    public function getMyDtoById(string $id): MyDto\n    {\n        $myEndpoint = new MyEndpoint(config('api-client.endpoints.myEndpoint.url'));\n        $myService = new MyService($myEndpoint, collect(), collect(), new ApiClient());\n        $myMethod = new MyMethod($myService, new NeverCacheStrategy());\n        $myPayload = new MyPayload();\n        $myPayload-\u003esetId($id);\n        \n        return $myMethod-\u003ecall($myPayload)-\u003egetMyDto();\n    }\n}\n\n```\n\nOr take advantage of dependency injection done by Laravel.\n\nExtend `CubeSystems\\ApiClient\\ApiClientServiceProvider` class and register your bindings:\n\n```php\npublic function register(): void\n{\n    parent::register();\n    \n    $this-\u003eapp-\u003esingleton(MyEndpoint::class, function (Application $app) {\n        $url = config('api-client.endpoints.myEndpoint.url');\n\n        return new MyEndpoint($url);\n    });\n}\n```\n\nNow the same thing can be done more concisely:\n\n```php\nuse Foo\\Methods\\MyMethod;\nuse Foo\\Payloads\\MyPayload;\n\nclass MyRepository\n{\n    public function getMyDtoById(string $id): MyDto\n    {\n        $myMethod = app(MyMethod::class);\n        $myPayload = new MyPayload();\n        $myPayload-\u003esetId($id);\n        \n        return $myMethod-\u003ecall($myPayload)-\u003egetMyDto();\n    }\n}\n```\n\nTake a look at tests for more detailed examples.\n\n## Caching\n\n`AbstractMethod` class includes a caching mechanism. Just pass desired cache strategy to the constructor. There are 3 strategies available:\n* `CubeSystems\\ApiClient\\Client\\Cache\\NeverCacheStrategy` - never cache the response\n* `CubeSystems\\ApiClient\\Client\\Cache\\RequestCacheStrategy` - cache the response for the duration of the request\n* `CubeSystems\\ApiClient\\Client\\Cache\\TimeIntervalCacheStrategy` - cache the response for a given time interval\n\nYou can also create your own strategy by implementing `CubeSystems\\ApiClient\\Client\\Contracts\\CacheStrategy` contract.\n\n`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.\n\n### Hierarchical caching\n\nIf 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.\n\nFor this to work, `CubeSystems\\ApiClient\\Client\\Contracts\\Payload::getCacheHierarchyKey` method must be implemented according to your needs.\n\nMoreover, `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.\n\n## There is more\n\n### Events\n\n`CubeSystems\\ApiClient\\Client\\Methods\\AbstractMethod` class fires the following events:\n* `CubeSystems\\ApiClient\\Events\\ApiCalled` - after a remote API is called\n* `CubeSystems\\ApiClient\\Events\\ResponseRetrievedFromCache` - after response is retrieved from cache without calling a remote API\n\nYou can listen to those and do some additional stuff like logging.\n\n\n### Laravel Debugbar integration\nIf `debugbar.collectors.api` configuration option is set to `true` as described above, all API calls (and cache retrievals) will be visible within Laravel Debugbar:\n\n![Debugbar integration](images/debugbar.png) *Debugbar integration*\n\n## Testing\n\nThis package uses [Pest testing framework](https://pestphp.com/). You can run tests with\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcubesystems%2Fapi-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcubesystems%2Fapi-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcubesystems%2Fapi-client/lists"}