2017 © Pedro PelĆ”ez
 

library retrofit-php

Retrofit for PHP - A type-safe PHP REST client.

image

tebru/retrofit-php

Retrofit for PHP - A type-safe PHP REST client.

  • Sunday, July 22, 2018
  • by natebrunette
  • Repository
  • 9 Watchers
  • 118 Stars
  • 36,077 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 19 Forks
  • 2 Open issues
  • 58 Versions
  • 15 % Grown

The README.md

Retrofit PHP

Build Status Code Coverage Scrutinizer Code Quality SensioLabsInsight, (*1)

Retrofit is a type-safe REST client. It is blatantly stolen from square/retrofit and implemented in PHP., (*2)

ā—UPGRADE NOTICEā—

Version 3 introduces many breaking changes. Please review the upgrade guide before upgrading., (*3)

Overview

The following is for version 3, please check out the corresponding tag for version 2 documentation, (*4)

Retrofit allows you to define your REST API with a simple interface. The follow example will attempt to display a typical use-case, but requires two additional libraries. The first uses Guzzle to make http requests as Retrofit does not ship with any default way to make network requests. The second uses a serializer (Gson) to hook into Retrofit's Converter functionality. This allows for automatic serialization of request bodies and deserialization of response bodies., (*5)

interface GitHubService
{
    /**
     * @GET("/users/{user}/list")
     * @Path("user")
     * @ResponseBody("App\GithubService\ListRepo")
     * @ErrorBody("App\GitHubService\ApiError")
     */
    public function listRepos(string $user): Call;
}

Annotations are used to configure the endpoint. Then, the Retrofit class generates a working implementation of the service interface., (*6)

$retrofit = Retrofit::builder()
    ->setBaseUrl('https://api.github.com')
    ->setHttpClient(new Guzzle6HttpClient(new Client())) // requires a separate library
    ->addConverterFactory(new GsonConverterFactory(Gson::builder()->build())) // requies a separate library
    ->build();

$gitHubService = $retrofit->create(GitHubService::class);

Our newly created service is capable of making GET requests to /users/{user}/list, which returns a Call object., (*7)

$call = $gitHubService->listRepos('octocat');

The Call object is then used to execute the request synchronously or asynchronously, returning a response., (*8)

$response = $call->execute();

// or

$call->enqueue(
    function(Response $response) { }, // response callback (optional)
    function(Throwable $throwable) { } // error callback (optional)
);
$call->wait();

You can then check to see if the request was successful and get the deserialized response body., (*9)

if (!$response->isSuccessful()) {
    throw new ApiException($response->errorBody());
}

$responseBody = $response->body();

Usage examples are referenced from Square's documentation, (*10)

Installation & Usage

Retrofit 3 requires PHP 7.1, (*11)

composer require tebru/retrofit-php

Please make sure you also install an http client., (*12)

composer require tebru/retrofit-php-http-guzzle6

Install a converter to handle more advanced request and response body conversions., (*13)

composer require tebru/retrofit-php-converter-gson

Documentation

License

This project is licensed under the MIT license. Please see the LICENSE file for more information., (*14)

The Versions

22/07 2018

dev-master

9999999-dev

Retrofit for PHP - A type-safe PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

22/07 2018

v3.2.0

3.2.0.0

Retrofit for PHP - A type-safe PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

22/07 2018

dev-simpler-requests

dev-simpler-requests

Retrofit for PHP - A type-safe PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

02/04 2018

v3.1.0

3.1.0.0

Retrofit for PHP - A type-safe PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

13/03 2018

v3.0.3

3.0.3.0

Retrofit for PHP - A type-safe PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

25/02 2018

v3.0.2

3.0.2.0

Retrofit for PHP - A type-safe PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

25/09 2017

v3.0.1

3.0.1.0

Retrofit for PHP - A type-safe PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

13/09 2017

v3.0.0

3.0.0.0

Retrofit for PHP - A type-safe PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

13/09 2017

v3.0.0-rc2

3.0.0.0-RC2

Retrofit for PHP - A type-safe PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

13/09 2017

v3.0.0-rc1

3.0.0.0-RC1

Retrofit for PHP - A type-safe PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

26/08 2017

dev-v3-wip

dev-v3-wip

Retrofit for PHP - A typesafe PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

18/08 2015
18/08 2015
18/08 2015
18/08 2015

v2.0.0

2.0.0.0

Retrofit for PHP - A PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

01/04 2015

v0.2.1

0.2.1.0

Retrofit for PHP - A PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

31/03 2015

v0.2.0

0.2.0.0

Retrofit for PHP - A PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

30/03 2015

v0.1.3

0.1.3.0

Retrofit for PHP - A PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

18/03 2015

v0.1.2

0.1.2.0

Retrofit for PHP - A PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

11/03 2015

v0.1.1

0.1.1.0

Retrofit for PHP - A PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette

06/02 2015

v0.1.0

0.1.0.0

Retrofit for PHP - A PHP REST client.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nate Brunette