-
-
Notifications
You must be signed in to change notification settings - Fork 456
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
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @sstok
There was a problem hiding this comment.
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.
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.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sstok
🚧 Never link to branches 🚧
and https://help.github.com/articles/getting-permanent-links-to-files/
y
There was a problem hiding this comment.
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).