Code Monkey home page Code Monkey logo

api-client's Introduction

Base API Client

Latest Stable Version Total Downloads Patreon donate button Donate weekly to this project using Gratipay Donate to this project using Flattr Donate to this project using Paypal

A reusable base API client for use with remote services.

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

namespace App\BlogApi;

class Client extends \BaseApiClient\Client
{
    /**
     * Namespace for the endpoints
     *
     * @var string
     */
    protected $endpointNamespace = 'App\BlogApi\Endpoints';
}

The $endpointNamespace variable is the prefix for the namespace of our endpoints.

Endpoints

From the endpoint we make our API calls and return the models.

<?php

namespace App\BlogApi\Endpoints;

use App\BlogApi\Models\Post;

use BaseApiClient\Endpoint;
use BaseApiClient\Models\Collection;

class Posts extends Endpoint
{
    /**
     * Get pages for the provided website.
     *
     * @param  array $params
     *
     * @return Collection
     * @throws \BaseApiClient\Exceptions\ApiException
     */
    public function index(array $params = [])
    {
        $response = $this->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

namespace App\BlogApi\Models;

use BaseApiClient\Models\Model;

class Post extends Model
{
    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = [
        'publish_at',
    ];
}

Registering Our Clients

<?php

namespace App\Providers;

use App\BlogApi\Client;
use App\AnotherApi\Client;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->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

namespace App\Http\Controllers;

use App\BlogApi\Client;
use Illuminate\Http\Request;

class BlogController extends Controller
{
    /**
     * Blog manager client instance.
     *
     * @var \App\BlogApi\Client
     */
    protected $client;

    /**
     * Initializer constructor.
     *
     * @param Client $client
     */
    public function __construct(Client $client)
    {
        parent::__construct();

        $this->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

api-client's People

Contributors

netsnatch avatar torann avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.