{"id":18310337,"url":"https://github.com/torann/api-client","last_synced_at":"2025-04-05T18:31:39.616Z","repository":{"id":57071912,"uuid":"53622578","full_name":"Torann/api-client","owner":"Torann","description":"A reusable base API client for use with remote services.","archived":false,"fork":false,"pushed_at":"2018-05-29T20:40:42.000Z","size":19,"stargazers_count":4,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-21T08:34:53.349Z","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":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Torann.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-03-10T22:45:58.000Z","updated_at":"2018-05-29T20:40:43.000Z","dependencies_parsed_at":"2022-08-24T14:54:32.676Z","dependency_job_id":null,"html_url":"https://github.com/Torann/api-client","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Torann%2Fapi-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Torann%2Fapi-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Torann%2Fapi-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Torann%2Fapi-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Torann","download_url":"https://codeload.github.com/Torann/api-client/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247383835,"owners_count":20930357,"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","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-05T16:14:09.499Z","updated_at":"2025-04-05T18:31:39.269Z","avatar_url":"https://github.com/Torann.png","language":"PHP","readme":"# Base API Client\n\n[![Latest Stable Version](https://poser.pugx.org/torann/api-client/v/stable.png)](https://packagist.org/packages/torann/api-client)\n[![Total Downloads](https://poser.pugx.org/torann/api-client/downloads.png)](https://packagist.org/packages/torann/api-client)\n[![Patreon donate button](https://img.shields.io/badge/patreon-donate-yellow.svg)](https://www.patreon.com/torann)\n[![Donate weekly to this project using Gratipay](https://img.shields.io/badge/gratipay-donate-yellow.svg)](https://gratipay.com/~torann)\n[![Donate to this project using Flattr](https://img.shields.io/badge/flattr-donate-yellow.svg)](https://flattr.com/profile/torann)\n[![Donate to this project using Paypal](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=4CJA2A97NPYVU)\n\nA reusable base API client for use with remote services.\n\n- [Base API Client on Packagist](https://packagist.org/packages/torann/api-client)\n- [Base API Client on GitHub](https://github.com/Torann/api-client)\n\n## Installation\n\nInstall using composer:\n\n```\n$ composer require torann/api-client\n```\n\n## Creating Clients\n\nOnce installed we need to create some clients. First we need to extend the `\\BaseApiClient\\Client` class and set endpoint namespace.\n\n### Client\n\n```php\n\u003c?php\n\nnamespace App\\BlogApi;\n\nclass Client extends \\BaseApiClient\\Client\n{\n    /**\n     * Namespace for the endpoints\n     *\n     * @var string\n     */\n    protected $endpointNamespace = 'App\\BlogApi\\Endpoints';\n}\n```\n\nThe `$endpointNamespace` variable is the prefix for the namespace of our endpoints.\n\n### Endpoints\n\nFrom the endpoint we make our API calls and return the models.\n\n```php\n\u003c?php\n\nnamespace App\\BlogApi\\Endpoints;\n\nuse App\\BlogApi\\Models\\Post;\n\nuse BaseApiClient\\Endpoint;\nuse BaseApiClient\\Models\\Collection;\n\nclass Posts extends Endpoint\n{\n    /**\n     * Get pages for the provided website.\n     *\n     * @param  array $params\n     *\n     * @return Collection\n     * @throws \\BaseApiClient\\Exceptions\\ApiException\n     */\n    public function index(array $params = [])\n    {\n        $response = $this-\u003erequest-\u003eget('posts', $params);\n\n        return new Collection($response, 'Post');\n    }\n\n    /**\n     * Create a new post.\n     *\n     * @param  array $params\n     *\n     * @return Post\n     * @throws \\BaseApiClient\\Exceptions\\ApiException\n     */\n    public function create(array $params)\n    {\n        $response = $this-\u003erequest-\u003epost('posts', $params);\n\n        return new Post($response);\n    }\n\n    /**\n     * Delete the provided post.\n     *\n     * @param  string $id\n     *\n     * @return bool\n     * @throws \\BaseApiClient\\Exceptions\\ApiException\n     */\n    public function delete($id)\n    {\n        $response = $this-\u003erequest-\u003edelete(sprintf('posts/%s', $id));\n\n        return $response-\u003egetResponseCode() === 200;\n    }\n}\n```\n\n### Models\n\n```php\n\u003c?php\n\nnamespace App\\BlogApi\\Models;\n\nuse BaseApiClient\\Models\\Model;\n\nclass Post extends Model\n{\n    /**\n     * The attributes that should be mutated to dates.\n     *\n     * @var array\n     */\n    protected $dates = [\n        'publish_at',\n    ];\n}\n```\n\n## Registering Our Clients\n\n```php\n\u003c?php\n\nnamespace App\\Providers;\n\nuse App\\BlogApi\\Client;\nuse App\\AnotherApi\\Client;\nuse Illuminate\\Support\\ServiceProvider;\n\nclass AppServiceProvider extends ServiceProvider\n{\n    /**\n     * Bootstrap any application services.\n     *\n     * @return void\n     */\n    public function boot()\n    {\n        //\n    }\n\n    /**\n     * Register any application services.\n     *\n     * @return void\n     */\n    public function register()\n    {\n        $this-\u003eregisterBlogService();\n        $this-\u003eregisterAnotherService();\n    }\n\n    /**\n     * Register blog manager services.\n     *\n     * @return void\n     */\n    public function registerBlogService()\n    {\n        $this-\u003eapp-\u003ebind(Client::class, function () {\n            return new Client([\n                'domain' =\u003e 'http://some.fancy.ip/',\n                'secret' =\u003e env('BLOG_MANAGER_API_SECRET'),\n            ]);\n        });\n    }\n\n    /**\n     * Register blog manager services.\n     *\n     * @return void\n     */\n    public function registerAnotherService()\n    {\n        $this-\u003eapp-\u003ebind(Client::class, function () {\n            return new Client([\n                'domain' =\u003e 'http://some.fancy.ip/',\n                'secret' =\u003e env('ANOTHER_API_SECRET'),\n            ]);\n        });\n    }\n}\n```\n\n## Calling an Endpoint\n\nBelow is an example of using our `\\App\\BlogApi\\Client` inside of a controller.\n\n```php\n\u003c?php\n\nnamespace App\\Http\\Controllers;\n\nuse App\\BlogApi\\Client;\nuse Illuminate\\Http\\Request;\n\nclass BlogController extends Controller\n{\n    /**\n     * Blog manager client instance.\n     *\n     * @var \\App\\BlogApi\\Client\n     */\n    protected $client;\n\n    /**\n     * Initializer constructor.\n     *\n     * @param Client $client\n     */\n    public function __construct(Client $client)\n    {\n        parent::__construct();\n\n        $this-\u003eclient = $client;\n    }\n\n    /**\n     * Display the specified resource.\n     *\n     * @param  \\Illuminate\\Http\\Request $request\n     *\n     * @return \\Illuminate\\Http\\Response\n     */\n    public function index(Request $request)\n    {\n        $posts = $this-\u003eclient-\u003eposts-\u003eindex($request-\u003eonly('page'));\n\n        return view('posts.index')-\u003ewith([\n            'posts' =\u003e $posts-\u003epaginate()\n        ]);\n    }\n\n    /**\n     * Display the specified resource.\n     *\n     * @param  Request $request\n     * @param  string  $slug\n     *\n     * @return \\Illuminate\\Http\\Response\n     */\n    public function show(Request $request, $slug)\n    {\n        $post = $this-\u003eclient-\u003eposts-\u003efind($slug);\n\n        return view('posts.show')-\u003ewith([\n            'post' =\u003e $post\n        ]);\n    }\n}\n```\n\n## Change Log\n\n**0.2.0**\n\n- Add support for Laravel 5.4\n\n**0.1.4**\n\n- Return null for empty values\n\n**0.1.3**\n\n- Add support for Laravel 5.3\n\n**0.1.2**\n\n- Remove trailing slash\n\n**0.1.1**\n\n- Bug fixes\n\n**0.1.0**\n\n- First release","funding_links":["https://www.patreon.com/torann","https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=4CJA2A97NPYVU"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftorann%2Fapi-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftorann%2Fapi-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftorann%2Fapi-client/lists"}