Skip to content

http client factory should allow to specify additional plugins #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions plugins/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,13 @@ Libraries that Require Plugins

When :doc:`writing a library based on HTTPlug <../httplug/library-developers>`, you might require
specific plugins to be active. The recommended way for doing this is to provide a factory method
for the ``PluginClient`` that your library users should use. This allows them to inject their own
for the ``PluginClient`` that library users should use. This allows them to inject their own
plugins or configure a different client. For example::

$myApiClient = new My\Api\Client('https://api.example.org', My\Api\HttpClientFactory::create('john', 's3cr3t'));

use Http\Client\HttpClient;
use Http\Client\Common\Plugin;
use Http\Client\Common\Plugin\AuthenticationPlugin;
use Http\Client\Common\Plugin\ErrorPlugin;
use Http\Discovery\HttpClientDiscovery;
Expand All @@ -124,22 +125,22 @@ plugins or configure a different client. For example::
/**
* Build the HTTP client to talk with the API.
*
* @param string $user Username
* @param string $pass Password
* @param HttpClient $client Base HTTP client
* @param string $user Username for the application on the API
* @param string $pass Password for the application on the API
* @param Plugin[] $plugins List of additional plugins to use
* @param HttpClient $client Base HTTP client
*
* @return HttpClient
*/
public static function create($user, $pass, HttpClient $client = null)
public static function create($user, $pass, array $plugins = [], HttpClient $client = null)
{
if (!$client) {
$client = HttpClientDiscovery::find();
}
return new PluginClient($client, [
new ErrorPlugin(),
new AuthenticationPlugin(
// This API has it own authentication algorithm
new ApiAuthentication(Client::AUTH_OAUTH_TOKEN, $user, $pass)
),
]);
$plugins[] = new ErrorPlugin();
$plugins[] = new AuthenticationPlugin(
// This API has it own authentication algorithm
new ApiAuthentication(Client::AUTH_OAUTH_TOKEN, $user, $pass)
);
return new PluginClient($client, $plugins);
}