2017 © Pedro Peláez
 

library json-client

A simple client for JSON APIs using Guzzle and the Symfony Serializer.

image

timostamm/json-client

A simple client for JSON APIs using Guzzle and the Symfony Serializer.

  • Thursday, July 26, 2018
  • by timostamm
  • Repository
  • 1 Watchers
  • 0 Stars
  • 46 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 130 % Grown

The README.md

json-client

build Packagist PHP Version GitHub tag License, (*1)

A simple client for JSON APIs using Guzzle and the Symfony Serializer., (*2)

To implement a API client, you can extend AbstractApiClient and write your methods, using the Guzzle Http Client to transmit., (*3)

class MyClient extends AbstractApiClient {


    /**
     * @throws TransferException
     */
    public function send(Model $model):void
    {
        // The data will automatically be 
        // serialized to JSON. 
        $this->http->post('model', [
            'data' => $model
        ]);
    }


    /**
     * @param int $id
     * @throws TransferException
     * @returns Model 
     */
    public function get(int $id):Model
    {
        $response = $this->http->get('model/'.$id, [
            'deserialize_to' => Model::class
        ]);

        if (!$response instanceof DeserializedResponse) {
            throw new \RuntimeException('Expected a DeserializedResponse, got: '.get_class($response));
        }

        return $response->getDeserializedData();
    }


}

All functionality is implemented as middleware, the AbstractApiClient just configures the Guzzle HandlerStack for you., (*4)

Provided middleware

Serialization

See DeserializeResponseMiddleware and SerializeRequestBodyMiddleware., (*5)

Server error messages

ServerMessageMiddleware provides support for JSON error messages., (*6)

Response expectations

If you want to make sure that a response has a specific header, content type or other feature, use ResponseExpectationMiddleware., (*7)

Logging

There is also middleware to log all HTTP requests (and corresponding response or exception), see HttpLoggingMiddleware, (*8)

An adapter for Psr\Log\LoggerInterface is available., (*9)

This middleware is not added by default because the order is important: The HttpLoggingMiddleware must be added last., (*10)

The Versions

26/07 2018

dev-master

9999999-dev

A simple client for JSON APIs using Guzzle and the Symfony Serializer.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Timo Stamm

26/07 2018

v1.0.3

1.0.3.0

A simple client for JSON APIs using Guzzle and the Symfony Serializer.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Timo Stamm

15/05 2018

v1.0.2

1.0.2.0

A simple client for JSON APIs using Guzzle and the Symfony Serializer.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Timo Stamm

15/05 2018

v1.0.1

1.0.1.0

A simple client for JSON APIs using Guzzle and the Symfony Serializer.

  Sources   Download

MIT

The Requires

 

by Timo Stamm

14/05 2018

v1.0.0

1.0.0.0

A simple client for JSON APIs using Guzzle and the Symfony Serializer.

  Sources   Download

MIT

The Requires

 

by Timo Stamm