项目作者: timostamm

项目描述 :
A simple client for JSON APIs using Guzzle and the Symfony Serializer.
高级语言: PHP
项目地址: git://github.com/timostamm/json-client.git
创建时间: 2018-05-11T14:30:19Z
项目社区:https://github.com/timostamm/json-client

开源协议:MIT License

下载


json-client

build
Packagist PHP Version
GitHub tag
License

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

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

  1. class MyClient extends AbstractApiClient {
  2. /**
  3. * @throws TransferException
  4. */
  5. public function send(Model $model):void
  6. {
  7. // The data will automatically be
  8. // serialized to JSON.
  9. $this->http->post('model', [
  10. 'data' => $model
  11. ]);
  12. }
  13. /**
  14. * @param int $id
  15. * @throws TransferException
  16. * @returns Model
  17. */
  18. public function get(int $id):Model
  19. {
  20. return $this->http->get('model/'.$id, [
  21. 'deserialize_to' => Model::class
  22. ]);
  23. }
  24. }

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

Provided middleware

Serialization

See DeserializeResponseMiddleware and SerializeRequestBodyMiddleware.

Server error messages

ServerMessageMiddleware provides support for JSON error messages.

Response expectations

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

Logging

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

An adapter for Psr\Log\LoggerInterface is available.

This middleware is not added by default because the order is
important: The HttpLoggingMiddleware must be added last.