2017 © Pedro Peláez
 

library api-response

Simple package to handle response properly in your API

image

ellipsesynergie/api-response

Simple package to handle response properly in your API

  • Tuesday, April 17, 2018
  • by maximebeaudoin
  • Repository
  • 14 Watchers
  • 341 Stars
  • 289,584 Installations
  • PHP
  • 16 Dependents
  • 0 Suggesters
  • 47 Forks
  • 4 Open issues
  • 32 Versions
  • 7 % Grown

The README.md

api-response

Latest Version Software License Api Response Tests Total Downloads, (*1)

Simple package to handle response properly in your API. This package uses Fractal and is based on Build APIs You Won't Hate book., (*2)

Install

Via Composer, (*3)

``` bash $ composer require ellipsesynergie/api-response, (*4)


## Requirements The following versions of PHP are supported by this version: >= PHP 8.1 ### Install in Laravel Add this following service provider to your `config/app.php` file. ```php EllipseSynergie\ApiResponse\Laravel\ResponseServiceProvider::class

Install in Lumen

Because of the request object change (see reference) you can no longer access Request object properly in Service provider. To be convenient, we have created a middleware to be used for parsing the include parameter., (*5)

Register this service provider to your bootstrap/app.php file., (*6)

$app->register('EllipseSynergie\ApiResponse\Laravel\LumenServiceProvider');

Register the global middleware bootstrap/app.php file., (*7)

$app->middleware([
    'EllipseSynergie\ApiResponse\Laravel\Middleware\ParseInclude'
]);

Install in your favorite framework or vanilla php

This package can be used in any framework or vanilla php. You simply need to extend EllipseSynergie\ApiResponse\AbstractResponse and implement the withArray() method in your custom class. You can take a look at EllipseSynergie\ApiResponse\Laravel\Response::withArray() for an example., (*8)

You will also need to instantiate the response class with a fractal manager instance., (*9)

// Instantiate the fractal manager
$manager = new \League\Fractal\Manager;

// Set the request scope if you need embed data
$manager->parseIncludes(explode(',', $_GET['include']));

// Instantiate the response object, replace the class name by your custom class
$response = new \EllipseSynergie\ApiResponse\AbstractResponse($manager);

For more options related to the fractal manager, you can take a look at the official Fractal website, (*10)

Example inside Laravel or Lumen controller

<?php

use EllipseSynergie\ApiResponse\Contracts\Response;

class BookController extends Controller {

    /**
     * @param Response $response
     */
    public function __construct(Response $response)
    {
        $this->response = $response;
    }

    /**
    * Example returning collection
    */
    public function index()
    {
        //Get all books
        $books = Book::all();

        // Return a collection of $books
        return $this->response->withCollection($books, new BookTransformer);
    }

    /**
    * Example returning collection with custom key
    */
    public function index()
    {
        //Get all books
        $books = Book::all();

        //Custom key
        $customKey = 'books';

        // Return a collection of books
        return $this->response->withCollection($books, new BookTransformer, $customKey);
    }

    /**
    * Example returning collection with paginator
    */
    public function index()
    {
        //Get all books
        $books = Book::paginate(15);

       // Return a collection of $books with pagination
       return $this->response->withPaginator(
           $books,
           new BookTransformer
       );
    }

    /**
    * Example returning collection with paginator with custom key and meta
    */
    public function index()
    {
        //Get all books
        $books = Book::paginate(15);

        //Custom key
        $customKey = 'books';

        //Custom meta
        $meta = [
            'category' => 'fantasy'
        ];

       // Return a collection of $books with pagination
       return $this->response->withPaginator(
           $books,
           new BookTransformer,
           $customKey,
           $meta
       );
    }

    /**
    * Example returning item
    */
    public function show($id)
    {
        //Get the book
        $book = Book::find($id);

        // Return a single book
        return $this->response->withItem($book, new BookTransformer);
    }

    /**
    * Example returning item with a custom key and meta
    */
    public function showWithCustomKeyAndMeta($id)
    {
        //Get the book
        $book = Book::find($id);

        //Custom key
        $customKey = 'book';

        //Custom meta
        $meta = [
            'readers' => $book->readers
        ];

        // Return a single book
        return $this->response->withItem($book, new BookTransformer, $customKey, $meta);
    }

    /**
    * Example resource not found
    */
    public function delete($id)
    {
        //Try to get the book
        $book = Book::find($id);

        //Book not found sorry !
        if(!$book){
            return $this->response->errorNotFound('Book Not Found');
        }
    }

    /**
    * Example method not implemented
    */
    public function whatAreYouTryingToDo()
    {
        return $this->response->errorMethodNotAllowed("Please don't try this again !");
    }
}

Ouput example

One book

{
    "data": {
        "id": 1,
        "title": "My name is Bob!.",
        "created_at": {
            "date": "2014-03-25 18:54:18",
            "timezone_type": 3,
            "timezone": "UTC"
        },
        "updated_at": {
            "date": "2014-03-25 18:54:18",
            "timezone_type": 3,
            "timezone": "UTC"
        },
        "deleted_at": null
    }
}

Collection of books

{
    "data": [
        {
           "id": 1,
           "title": "My name is Bob!",
           "created_at": {
               "date": "2014-03-25 18:54:18",
               "timezone_type": 3,
               "timezone": "UTC"
           },
           "updated_at": {
               "date": "2014-03-25 18:54:18",
               "timezone_type": 3,
               "timezone": "UTC"
           },
           "deleted_at": null
        },
        {
           "id": 2,
           "title": "Who's your dady ?",
           "created_at": {
               "date": "2014-03-26 18:54:18",
               "timezone_type": 3,
               "timezone": "UTC"
           },
           "updated_at": {
               "date": "2014-03-26 18:54:18",
               "timezone_type": 3,
               "timezone": "UTC"
           },
           "deleted_at": null
        }
    ]
}

Error

{
    "error": {
        "code": "GEN-NOT-FOUND",
        "http_code": 404,
        "message": "Book Not Found"
    }
}

Testing the package

bash $ phpunit, (*11)

Testing within Laravel

According to the issue #31, we have found some problem when it's time to test the include query parameter value. If you want to resolve this issue in your test, you must use the trait EllipseSynergie\ApiResponse\Testing\Laravel\AddTestingSupportForInclude. To replace the call method from Illuminate\Foundation\Testing\Concerns\MakesHttpRequests::call, (*12)

Contributing

Please see CONTRIBUTING for details., (*13)

Credits

License

The MIT License (MIT). Please see License File for more information., (*14)

The Versions

17/04 2018

dev-master

9999999-dev https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

11/09 2017

0.15.0

0.15.0.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

06/07 2017

0.14.2

0.14.2.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

06/07 2017

0.14.1

0.14.1.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

05/07 2017

0.14.0

0.14.0.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

10/05 2017

0.13.0

0.13.0.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

14/07 2016

0.12.3

0.12.3.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

07/06 2016

0.12.2

0.12.2.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

28/01 2016

0.12.1

0.12.1.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

08/01 2016

0.12.0

0.12.0.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

04/01 2016

0.11.0

0.11.0.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

23/09 2015

0.10.1

0.10.1.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

20/04 2015

0.10.0

0.10.0.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

19/03 2015

0.9.0

0.9.0.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

23/02 2015

dev-0.8-dev

dev-0.8-dev https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

23/02 2015

0.8.1

0.8.1.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

10/12 2014

0.8.0

0.8.0.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

07/10 2014

0.7.0

0.7.0.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

10/09 2014

0.6.1

0.6.1.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

10/09 2014

0.6.0

0.6.0.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

19/08 2014

0.5.1

0.5.1.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

10/07 2014

0.5.0

0.5.0.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

20/06 2014

0.4.0

0.4.0.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

06/06 2014

0.3.3

0.3.3.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

03/06 2014

0.3.2

0.3.2.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

31/05 2014

0.3.1

0.3.1.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

31/05 2014

0.3.0

0.3.0.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

30/03 2014

0.2.2

0.2.2.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

30/03 2014

0.2.1

0.2.1.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

30/03 2014

0.2.0

0.2.0.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

27/03 2014

0.1.1

0.1.1.0 https://github.com/ellipsesynergie/api-response

Simple package to handle response properly in your API

  Sources   Download

MIT

The Requires

 

The Development Requires

api json fractal

25/03 2014