Skip to content

Http client adapters #50

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ Model Usage
You can also use the library in an object oriented manner.

```php
$client = new \Gitlab\Client('http://git.yourdomain.com/api/v3/'); // change here
$client->authenticate('your_gitlab_token_here', \Gitlab\Client::AUTH_URL_TOKEN); // change here
$guzzle = new \Gitlab\HttpClient\Adapter\Guzzle4();
$client = new \Gitlab\Client('http://git.yourdomain.com/api/v3/', $guzzle); // change here
$client->authenticate('your_gitlab_token_here', \Gitlab\HttpClient\HttpClientInterface::AUTH_URL_TOKEN); // change here
```

Creating a new project
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
}
],
"require": {
"php": ">=5.3.2",
"ext-curl": "*",
"kriswallsmith/buzz": ">=0.7"
"php": ">=5.3.2"
},
"suggest": {
"kriswallsmith/buzz": "To use Buzz as the HTTP adapter (~0.7)",
"guzzlehttp/guzzle": "To use Guzzle as the HTTP adapter (~3.7, ~4.1)"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you know that 3.7->3.9.* is not in guzzlehttp/guzzle but guzzle/guzzle so this should be 2 suggests

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthiasnoback book on principles of package design has some good info about avoiding these suggests i believe. But i am still working through it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @sstok

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @matthiasnoback has already explained once There's no such thing as an optional dependency

This library cannot work work without the an actual HTTP adapter, so there are two options here.

  1. Pick one and just use it, make it a requirement.
  2. Don't ship this core-package with any adapter-implementation at all.
    Create additional packages where each package provides an adapter and composer requirements for actual library.

For example: php-gitlab-api-buzz, provides the buzz http-adapter for the php-gitlab-api core package, and its composer.json requires the php-gitlab-api and buzz-library.

So when someone requires the php-gitlab-api-buzz package, this will install the php-gitlab-api and buzz-library, and no dependencies will be missing.

This also what I have done for RollerworksSearch.
https://github.com/rollerworks/rollerworks-search-doctrine-orm/blob/master/composer.json#L19-L21

Edit. Wrong markup for the article link.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sstok thanks for the explanations :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tip, but this issue is really old 🤪 I'm no longer using Gush (or Gitlab for that matter).

},
"autoload": {
"psr-0": { "Gitlab\\": "lib/" }
}
Expand Down
10 changes: 5 additions & 5 deletions lib/Gitlab/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function configure()
*/
protected function get($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->get($path, $parameters, $requestHeaders);
$response = $this->client->get($path, $parameters, $requestHeaders);

return $response->getContent();
}
Expand All @@ -50,7 +50,7 @@ protected function get($path, array $parameters = array(), $requestHeaders = arr
*/
protected function post($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->post($path, $parameters, $requestHeaders);
$response = $this->client->post($path, $parameters, $requestHeaders);

return $response->getContent();
}
Expand All @@ -60,7 +60,7 @@ protected function post($path, array $parameters = array(), $requestHeaders = ar
*/
protected function patch($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->patch($path, $parameters, $requestHeaders);
$response = $this->client->patch($path, $parameters, $requestHeaders);

return $response->getContent();
}
Expand All @@ -70,7 +70,7 @@ protected function patch($path, array $parameters = array(), $requestHeaders = a
*/
protected function put($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->put($path, $parameters, $requestHeaders);
$response = $this->client->put($path, $parameters, $requestHeaders);

return $response->getContent();
}
Expand All @@ -80,7 +80,7 @@ protected function put($path, array $parameters = array(), $requestHeaders = arr
*/
protected function delete($path, array $parameters = array(), $requestHeaders = array())
{
$response = $this->client->getHttpClient()->delete($path, $parameters, $requestHeaders);
$response = $this->client->delete($path, $parameters, $requestHeaders);

return $response->getContent();
}
Expand Down
71 changes: 21 additions & 50 deletions lib/Gitlab/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,16 @@

namespace Gitlab;

use Buzz\Client\Curl;
use Buzz\Client\ClientInterface;

use Gitlab\Api\ApiInterface;
use Gitlab\Exception\InvalidArgumentException;
use Gitlab\HttpClient\Adapter\AdapterInterface;
use Gitlab\HttpClient\HttpClient;
use Gitlab\HttpClient\HttpClientInterface;
use Gitlab\HttpClient\Listener\AuthListener;

/**
* Simple API wrapper for Gitlab
*/
class Client
{
/**
* Constant for authentication method. Indicates the default, but deprecated
* login with username and token in URL.
*/
const AUTH_URL_TOKEN = 'url_token';

/**
* Constant for authentication method. Indicates the new login method with
* with username and token via HTTP Authentication.
*/
const AUTH_HTTP_TOKEN = 'http_token';

/**
* @var array
*/
Expand All @@ -36,10 +20,8 @@ class Client
'timeout' => 60
);

private $base_url = null;

/**
* The Buzz instance used to communicate with Gitlab
* The HTTP client instance used to communicate with Gitlab
*
* @var HttpClient
*/
Expand All @@ -48,17 +30,12 @@ class Client
/**
* Instantiate a new Gitlab client
*
* @param string $baseUrl
* @param null|ClientInterface $httpClient Buzz client
* @param string $baseUrl
* @param AdapterInterface $adapter
*/
public function __construct($baseUrl, ClientInterface $httpClient = null)
public function __construct($baseUrl, AdapterInterface $adapter)
{
$httpClient = $httpClient ?: new Curl();
$httpClient->setTimeout($this->options['timeout']);
$httpClient->setVerifyPeer(false);

$this->base_url = $baseUrl;
$this->httpClient = new HttpClient($this->base_url, $this->options, $httpClient);
$this->httpClient = new HttpClient($baseUrl, $adapter);
}

/**
Expand Down Expand Up @@ -131,38 +108,32 @@ public function api($name)
*/
public function authenticate($token, $authMethod = null, $sudo = null)
{
$this->httpClient->addListener(
new AuthListener(
$authMethod,
$token,
$sudo
)
);
$this->httpClient->authenticate($token, $authMethod, $sudo);
}

/**
* @return HttpClient
*/
public function getHttpClient()
public function get($path, array $parameters = array(), array $headers = array())
{
return $this->httpClient;
return $this->httpClient->get($path, $parameters, $headers);
}

/**
* @param HttpClientInterface $httpClient
*/
public function setHttpClient(HttpClientInterface $httpClient)
public function post($path, array $parameters = array(), array $headers = array())
{
$this->httpClient = $httpClient;
return $this->httpClient->post($path, $parameters, $headers);
}

public function setBaseUrl($url)
public function patch($path, array $parameters = array(), array $headers = array())
{
$this->base_url = $url;
return $this->httpClient->patch($path, $parameters, $headers);
}
public function getBaseUrl()

public function delete($path, array $parameters = array(), array $headers = array())
{
return $this->httpClient->delete($path, $parameters, $headers);
}

public function put($path, array $parameters = array(), array $headers = array())
{
return $this->base_url;
return $this->httpClient->put($path, $parameters, $headers);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions lib/Gitlab/HttpClient/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Gitlab\HttpClient\Adapter;

interface AdapterInterface
{
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array());

public function authenticate($token, $authMethod, $sudo = null);
}
119 changes: 119 additions & 0 deletions lib/Gitlab/HttpClient/Adapter/BuzzAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
namespace Gitlab\HttpClient\Adapter;

use Buzz\Client\AbstractClient;
use Buzz\Client\Curl;
use Buzz\Listener\ListenerInterface;
use Gitlab\Exception\ErrorException;
use Gitlab\Exception\RuntimeException;
use Gitlab\HttpClient\Listener\AuthListener;
use Gitlab\HttpClient\Listener\ErrorListener;
use Gitlab\HttpClient\Message\Request;
use Gitlab\HttpClient\Message\BuzzResponse;

class BuzzAdapter implements AdapterInterface
{
/**
* @var AbstractClient
*/
protected $client;

/**
* @var ListenerInterface[]
*/
protected $listeners = array();

/**
* @param AbstractClient $client
*/
public function __construct(AbstractClient $client = null)
{
$this->client = $client ?: new Curl();

$this->addListener(new ErrorListener());
}

/**
* @param int $timeout
*
* @return $this
*/
public function setTimeout($timeout)
{
$this->client->setTimeout($timeout);

return $this;
}

/**
* @param bool $verify
*
* @return $this
*/
public function verifyPeer($verify)
{
$this->client->setVerifyPeer($verify);

return $this;
}

/**
* @param string $path
* @param array $parameters
* @param string $httpMethod
* @param array $headers
*
* @throws ErrorException
* @throws RuntimeException
*
* @return BuzzResponse
*/
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array())
{
$request = new Request($httpMethod);
$request->setHeaders($headers);
$request->fromUrl($path);
$request->setContent(http_build_query($parameters));

$hasListeners = 0 < count($this->listeners);
if ($hasListeners) {
foreach ($this->listeners as $listener) {
$listener->preSend($request);
}
}

$response = new BuzzResponse();

try {
$this->client->send($request, $response);
} catch (\LogicException $e) {
throw new ErrorException($e->getMessage(), $e->getCode(), $e);
} catch (\RuntimeException $e) {
throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
}

if ($hasListeners) {
foreach ($this->listeners as $listener) {
$listener->postSend($request, $response);
}
}

return $response;
}

public function authenticate($token, $authMethod, $sudo = null)
{
$this->addListener(
new AuthListener(
$authMethod,
$token,
$sudo
)
);
}

public function addListener(ListenerInterface $listener)
{
$this->listeners[get_class($listener)] = $listener;
}
}
56 changes: 56 additions & 0 deletions lib/Gitlab/HttpClient/Adapter/Guzzle3Adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
namespace Gitlab\HttpClient\Adapter;

use Gitlab\Exception\ErrorException;
use Gitlab\Exception\RuntimeException;
use Gitlab\HttpClient\Message\Guzzle3Response;
use Gitlab\HttpClient\Subscriber\AuthSubscriber;
use Guzzle\Http\Client;
use Guzzle\Http\ClientInterface;

class Guzzle3Adapter implements AdapterInterface
{
/**
* @var ClientInterface
*/
protected $client;

/**
* @param ClientInterface $client
*/
public function __construct(ClientInterface $client = null)
{
$this->client = $client ?: new Client();
}

/**
* @param string $path
* @param array $parameters
* @param string $httpMethod
* @param array $headers
*
* @throws ErrorException
* @throws RuntimeException
*
* @return Guzzle3Response
*/
public function request($path, array $parameters = array(), $httpMethod = 'GET', array $headers = array())
{
$request = $this->client->createRequest($httpMethod, $path, $headers, http_build_query($parameters));

try {
$response = $this->client->send($request);
} catch (\LogicException $e) {
throw new ErrorException($e->getMessage(), $e->getCode(), $e);
} catch (\RuntimeException $e) {
throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
}

return new Guzzle3Response($response);
}

public function authenticate($token, $authMethod, $sudo = null)
{
$this->client->addSubscriber(new AuthSubscriber($authMethod, $token, $sudo));
}
}
Loading