An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

        

# Base API Client

[![Latest Stable Version](https://poser.pugx.org/torann/api-client/v/stable.png)](https://packagist.org/packages/torann/api-client)
[![Total Downloads](https://poser.pugx.org/torann/api-client/downloads.png)](https://packagist.org/packages/torann/api-client)
[![Patreon donate button](https://img.shields.io/badge/patreon-donate-yellow.svg)](https://www.patreon.com/torann)
[![Donate weekly to this project using Gratipay](https://img.shields.io/badge/gratipay-donate-yellow.svg)](https://gratipay.com/~torann)
[![Donate to this project using Flattr](https://img.shields.io/badge/flattr-donate-yellow.svg)](https://flattr.com/profile/torann)
[![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&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