2017 © Pedro Peláez
 

library retrofit-bundle

A Symfony2 bundle for the retrofit-php library.

image

tebru/retrofit-bundle

A Symfony2 bundle for the retrofit-php library.

  • Wednesday, August 17, 2016
  • by natebrunette
  • Repository
  • 1 Watchers
  • 4 Stars
  • 15,260 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 12 Versions
  • 4 % Grown

The README.md

Retrofit PHP Bundle

This Symfony2 bundle aims to provide an easy way to use the tebru/retrofit-php library. Please review the documentation for functionality., (*1)

Installation

composer require tebru/retrofit-bundle

As Retrofit does not include an http client, install Guzzle., (*2)

composer require guzzlehttp/guzzle

Add a line to your AppKernel.php, (*3)

new Tebru\RetrofitBundle\TebruRetrofitBundle()

Usage

You will need to create providers for each API you want to consume. They should be created with the builder and return a RestAdapter. Here is an example:, (*4)

<?php

namespace AppBundle;

use Tebru\Retrofit\Adapter\Rest\RestAdapter;

class FooBarRestAdapterProvider
{
    public static function get($baseUrl)
    {
        // setup additional dependencies like an http client here and use
        // the setters to add them to the builder

        // build the rest adapter
        return RestAdapter::builder()
            ->setBaseUrl($baseUrl)
            ->build();
    }
}

Create annotated interfaces. A simple example is shown below, more detailed examples are available here., (*5)

<?php

namespace AppBundle;

use Tebru\Retrofit\Annotation as Rest;

interface Foo
{
    /**
     * @Rest\GET("/get/{id}")
     */
    public function getFoo($id);
}

Everything else can be configured in your service definition. A yaml example is shown., (*6)

parameters:
    foo.class: AppBundle\Foo
    bar.class: AppBundle\Bar

services:
    # create a rest adapter
    foobar_rest_adapter:
        class: %tebru_retrofit.rest_adapter.class%
        factory: [AppBundle\FooBarRestAdapterProvider, get]
        arguments:
             - "https://foobar.com"

    # use the rest adapter to create clients
    # clients must be tagged with 'tebru_retrofit.register'
    foo_service:
        class: %foo.class%
        factory: [@foobar_rest_adapter, create]
        arguments: [%foo.class%]
        tags:
            - { name: tebru_retrofit.register }

    bar_service:
        class: %bar.class%
        factory: [@foobar_rest_adapter, create]
        arguments: [%bar.class%]
        tags:
            - { name: tebru_retrofit.register }

    # inject your client services anywhere
    baz:
        class: AppBundle\Baz
        arguments: [@foo_service, @bar_service]

Mocking

Because Retrofit uses interfaces, it's easy to create mock implementations if you do not want to hit a real API., (*7)

services:
    foo_service:
        class: %foo.class%
        factory: [@foobar_rest_adapter, create]
        arguments: ['AppBundle\MockFoo']
        tags:
            - { name: tebru_retrofit.register }
services:
    foo_service:
        class: %foo.class%
        factory: [@foobar_rest_adapter, create]
        arguments: [@mock_foo]
        tags:
            - { name: tebru_retrofit.register }

Take note of the arguments key in the above example. You may pass in a concrete class as a string or service to use that instead of the generated class. This is especially useful during development., (*8)

The Versions

17/08 2016

dev-master

9999999-dev

A Symfony2 bundle for the retrofit-php library.

  Sources   Download

MIT

The Requires

 

by Nate Brunette

17/08 2016

v2.1.0

2.1.0.0

A Symfony2 bundle for the retrofit-php library.

  Sources   Download

MIT

The Requires

 

by Nate Brunette

21/08 2015

v2.0.1

2.0.1.0

A Symfony2 bundle for the retrofit-php library.

  Sources   Download

MIT

The Requires

 

by Nate Brunette

18/08 2015

v2.0.0

2.0.0.0

A Symfony2 bundle for the retrofit-php library.

  Sources   Download

MIT

The Requires

 

by Nate Brunette

12/06 2015

dev-release/v1.x

dev-release/v1.x

A Symfony2 bundle for the retrofit-php library.

  Sources   Download

MIT

The Requires

 

by Nate Brunette

12/06 2015

v1.0.0

1.0.0.0

A Symfony2 bundle for the retrofit-php library.

  Sources   Download

MIT

The Requires

 

by Nate Brunette

03/04 2015

v0.2.1

0.2.1.0

A Symfony2 bundle for the retrofit-php library.

  Sources   Download

MIT

The Requires

 

by Nate Brunette

03/04 2015

v0.2.0

0.2.0.0

A Symfony2 bundle for the retrofit-php library.

  Sources   Download

MIT

The Requires

 

by Nate Brunette

31/03 2015

v0.1.3

0.1.3.0

A Symfony2 bundle for the retrofit-php library.

  Sources   Download

MIT

The Requires

 

by Nate Brunette

18/03 2015

v0.1.2

0.1.2.0

A Symfony2 bundle for the retrofit-php library.

  Sources   Download

MIT

The Requires

 

by Nate Brunette

16/03 2015

v0.1.1

0.1.1.0

A Symfony2 bundle for the retrofit-php library.

  Sources   Download

MIT

The Requires

 

by Nate Brunette

01/02 2015

v0.1.0

0.1.0.0

A Symfony2 bundle for the retrofit-php library.

  Sources   Download

MIT

The Requires

 

by Nate Brunette