Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/timostamm/json-client
A simple client for JSON APIs using Guzzle and the Symfony Serializer.
https://github.com/timostamm/json-client
api guzzle http json serializer symfony
Last synced: about 1 month ago
JSON representation
A simple client for JSON APIs using Guzzle and the Symfony Serializer.
- Host: GitHub
- URL: https://github.com/timostamm/json-client
- Owner: timostamm
- License: mit
- Created: 2018-05-11T14:30:19.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-04-17T13:53:59.000Z (over 1 year ago)
- Last Synced: 2024-10-12T04:29:46.508Z (2 months ago)
- Topics: api, guzzle, http, json, serializer, symfony
- Language: PHP
- Homepage:
- Size: 48.8 KB
- Stars: 1
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# json-client
[![build](https://github.com/timostamm/json-client/workflows/CI/badge.svg)](https://github.com/timostamm/json-client/actions?query=workflow:"CI")
![Packagist PHP Version](https://img.shields.io/packagist/dependency-v/timostamm/json-client/php)
[![GitHub tag](https://img.shields.io/github/tag/timostamm/json-client?include_prereleases=&sort=semver&color=blue)](https://github.com/timostamm/json-client/releases/)
[![License](https://img.shields.io/badge/License-MIT-blue)](#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.```PHP
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
{
return $this->http->get('model/'.$id, [
'deserialize_to' => Model::class
]);
}}
```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.