Skip to content
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

v3.0: Guzzle 6 Support and rewrite #50

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Add support for Key Auth
Plugin plus configuration support to allows users to pass a key that
will be used in requests.
  • Loading branch information
rdohms committed Oct 24, 2017
commit a5d03f9b12b88c5635112ce8911b1d658a39ae74
45 changes: 45 additions & 0 deletions src/DMS/Service/Meetup/ClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace DMS\Service\Meetup;

use DMS\Service\Meetup\Config\ClientConfig;
use Guzzle\Service\Loader\JsonLoader;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Symfony\Component\Config\FileLocator;

final class ClientFactory
{
private const DESCRIPTION_PATH = __DIR__ . '/../../../../description';

public static function buildClient(ClientConfig $config)
{

$default = [
'base_uri' => 'http://api.meetup.com/',
];

$locator = new FileLocator([self::DESCRIPTION_PATH]);
$jsonLoader = new JsonLoader($locator);

$description = $jsonLoader->load($locator->locate('meetup.json'));
$description = new Description($description);

$stack = HandlerStack::create();

foreach ($config->getMiddleware() as $name => $middleware) {
$stack->push(Middleware::mapRequest($middleware), $name);
}

$config = array_merge($default, [
'handler' => $stack
]);

$client = new GuzzleClient($config);


return new Client($client, $description);
}
}
11 changes: 11 additions & 0 deletions src/DMS/Service/Meetup/Config/ClientConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace DMS\Service\Meetup\Config;

interface ClientConfig
{
/**
* @return callable[]
*/
public function getMiddleware(): array;
}
29 changes: 29 additions & 0 deletions src/DMS/Service/Meetup/Config/KeyAuthConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace DMS\Service\Meetup\Config;

use DMS\Service\Meetup\Plugin\KeyAuthPlugin;

final class KeyAuthConfig implements ClientConfig
{
/**
* @var string
*/
protected $key;

/**
* @param string $key
*/
public function __construct($key)
{
$this->key = $key;
}

public function getMiddleware(): array
{
return [
'add key' => new KeyAuthPlugin($this->key)
];
}

}
26 changes: 26 additions & 0 deletions src/DMS/Service/Meetup/Plugin/KeyAuthPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace DMS\Service\Meetup\Plugin;

use Psr\Http\Message\RequestInterface;

final class KeyAuthPlugin
{
/**
* @var string
*/
protected $key;

public function __construct(string $key)
{
$this->key = $key;
}

public function __invoke(RequestInterface $request): RequestInterface
{
$uri = $request->getUri();
$uri = $uri->withQuery($uri->getQuery() . '&key=' . $this->key);

return $request->withUri($uri);
}
}