https://github.com/torann/api-client
A reusable base API client for use with remote services.
https://github.com/torann/api-client
Last synced: about 2 months ago
JSON representation
A reusable base API client for use with remote services.
- Host: GitHub
- URL: https://github.com/torann/api-client
- Owner: Torann
- License: bsd-2-clause
- Created: 2016-03-10T22:45:58.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-05-29T20:40:42.000Z (almost 7 years ago)
- Last Synced: 2025-03-21T08:34:53.349Z (2 months ago)
- Language: PHP
- Size: 18.6 KB
- Stars: 4
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Base API Client
[](https://packagist.org/packages/torann/api-client)
[](https://packagist.org/packages/torann/api-client)
[](https://www.patreon.com/torann)
[](https://gratipay.com/~torann)
[](https://flattr.com/profile/torann)
[](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=4CJA2A97NPYVU)A reusable base API client for use with remote services.
- [Base API Client on Packagist](https://packagist.org/packages/torann/api-client)
- [Base API Client on GitHub](https://github.com/Torann/api-client)## Installation
Install using composer:
```
$ composer require torann/api-client
```## Creating Clients
Once installed we need to create some clients. First we need to extend the `\BaseApiClient\Client` class and set endpoint namespace.
### Client
```php
request->get('posts', $params);return new Collection($response, 'Post');
}/**
* Create a new post.
*
* @param array $params
*
* @return Post
* @throws \BaseApiClient\Exceptions\ApiException
*/
public function create(array $params)
{
$response = $this->request->post('posts', $params);return new Post($response);
}/**
* Delete the provided post.
*
* @param string $id
*
* @return bool
* @throws \BaseApiClient\Exceptions\ApiException
*/
public function delete($id)
{
$response = $this->request->delete(sprintf('posts/%s', $id));return $response->getResponseCode() === 200;
}
}
```### Models
```php
registerBlogService();
$this->registerAnotherService();
}/**
* Register blog manager services.
*
* @return void
*/
public function registerBlogService()
{
$this->app->bind(Client::class, function () {
return new Client([
'domain' => 'http://some.fancy.ip/',
'secret' => env('BLOG_MANAGER_API_SECRET'),
]);
});
}/**
* Register blog manager services.
*
* @return void
*/
public function registerAnotherService()
{
$this->app->bind(Client::class, function () {
return new Client([
'domain' => 'http://some.fancy.ip/',
'secret' => env('ANOTHER_API_SECRET'),
]);
});
}
}
```## Calling an Endpoint
Below is an example of using our `\App\BlogApi\Client` inside of a controller.
```php
client = $client;
}/**
* Display the specified resource.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$posts = $this->client->posts->index($request->only('page'));return view('posts.index')->with([
'posts' => $posts->paginate()
]);
}/**
* Display the specified resource.
*
* @param Request $request
* @param string $slug
*
* @return \Illuminate\Http\Response
*/
public function show(Request $request, $slug)
{
$post = $this->client->posts->find($slug);return view('posts.show')->with([
'post' => $post
]);
}
}
```## Change Log
**0.2.0**
- Add support for Laravel 5.4
**0.1.4**
- Return null for empty values
**0.1.3**
- Add support for Laravel 5.3
**0.1.2**
- Remove trailing slash
**0.1.1**
- Bug fixes
**0.1.0**
- First release