api-clients / transport
Middleware infused clue/buzz-react
Installs: 93 713
Dependents: 16
Suggesters: 0
Security: 0
Stars: 4
Watchers: 6
Forks: 3
Open Issues: 4
Requires
- php: ^7.0
- api-clients/middleware: ^4.0
- api-clients/rx: ^2.0
- clue/buzz-react: ^2.0 || ^1.4
- react/event-loop: ^1.0 || ^0.5 || ^0.4
- react/http-client: ^0.4.17 || ^0.5
- react/promise-stream: ^1.0 || ^0.1.0
Requires (Dev)
Suggests
- api-clients/middleware-cache: Caching middleware
- api-clients/middleware-oauth1: Oauth 1.0 middleware
- api-clients/resource-generator: Easy wireframing of resources
- dev-master
- 3.2.0
- 3.1.0
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.0.0
- 1.0.1
- 1.0.0
- dev-renovate/configure
- dev-dependabot/add-v2-config-file
- dev-dependabot/composer/clue/buzz-react-2.6.0
- dev-proxy-support
- dev-allow-middleware-instances
- dev-react-promise-stream-1.0-and-0.1
- dev-buzz-2.0-and-1.1
- dev-feature-error-handling-middlware
- dev-feature-request-processing
- dev-var.ci
This package is auto-updated.
Last update: 2024-12-15 07:09:53 UTC
README
In a nutshell this package is a wrapper around clue/buzz-react
adding middleware.
Install
To install via Composer, use the command below, it will automatically detect the latest version and bind it with ^
.
composer require api-clients/transport
Usage
Creating the client can be done using the factory:
$locator = new MiddlewareLocator(); // A concrete class implementing the middleware locator interface $loop = EventLoopFactory::create(); // The event loop $options = []; // Client options, as described below $client = Factory::create($locator, $loop, $options);
Next you can make PSR-7 request:
$request = new PsrRequest(); // $requestOptions = []; // Options such as middleware settings $client->request($request, $requestOptions)->done(function (ResponseInterface $response) { // Handle the response });
Middleware
Middlewares are passed into the client with the options argument. In this example the api-clients/middleware-delay
is used. Adding middlewares to the client is simple, add an array to $options
with Options::MIDDLEWARE
as index cosisting of middleware class names. Optionally you can pass options for the middleware through the $options
array. Simply add a new array inside the array with the middlware class name as index and pass the desired options into it.
$options = [ Options::DEFAULT_REQUEST_OPTIONS => [ \ApiClients\Middleware\Delay\DelayMiddleware::class => [ \ApiClients\Middleware\Delay\Options::DELAY => 3, ], ], Options::MIDDLEWARE => [ \ApiClients\Middleware\Delay\DelayMiddleware::class, ], ]; $client = new Client($loop, $container, $browser, $options);
Middleware options can be changed per request, this specific request only will have a delay of 5 seconds instead of the default 3:
$requestOptions = [ \ApiClients\Middleware\Delay\DelayMiddleware::class => [ \ApiClients\Middleware\Delay\Options::DELAY => 5, ], ]; $client->request($request, $requestOptions);
Options
Options::DNS
DNS server to use resolving hostnames, defaults to 8.8.8.8
.
Options::SCHEMA
Schema part of the URI, defaults to https
.
Options::HOST
Host part of the URI, required.
Options::PORT
Port part of the URI, optional.
Options::PATH
Path part of the URI, defaults to /
.
Options::HEADERS
Key value array with headers, defaults to []
.
Options::MIDDLEWARE
Array with middleware class names, for example [MiddlewareOne::class, MiddlewareTwo::class]
, defaults to []
.
Options::DEFAULT_REQUEST_OPTIONS
Set of default request options, mainly useful for middlewares needed for all requests, defaults to []
.
License
The MIT License (MIT)
Copyright (c) 2017 Cees-Jan Kiewiet
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.