Skip to content

Commit

Permalink
Merge pull request #95 from Happyr/no-discovery
Browse files Browse the repository at this point in the history
Make sure we do not have to use discovery
  • Loading branch information
Nyholm committed Mar 6, 2016
2 parents a84236a + 5ee6c1e commit a47bd8b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 10 deletions.
20 changes: 13 additions & 7 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,31 @@ composer require php-http/guzzle6-adapter

If you are updating form a previous version make sure to read [the upgrade documentation](Upgrade.md).

### Auto discovery with Puli
### Finding the HTTP client.

This library has a dependency on `php-http/discovery` that will find a HTTP client and a library that can
create PSR-7 messages that you already have installed. It uses [Puli](http://docs.puli.io/en/latest/) for the actual
discovery. If you do not know about Puli yet you should check it out.
The LinkedIn client need to know what library you are using to send HTTP messages. You could provide an instance of
HttpClient and MessageFactory or you could fallback on auto discovery. Below is an example on where you provide a Guzzle6
instance.

Puli is in currently under delveopment but they are aiming for a stable release in Mars 2016. To be able to use the beta
version of Puli you need to add the following packages to your composer.json.
```php
$linkedIn=new Happyr\LinkedIn\LinkedIn('app_id', 'app_secret');
$linkedIn->setHttpClient(new \Http\Adapter\Guzzle6\Client());
$linkedIn->setHttpMessageFactory(new Http\Message\MessageFactory\GuzzleMessageFactory());

```

If you do not provide anything we will fallback on using auto discovery with [Puli](http://docs.puli.io/en/latest/). It will automatically find the
client you have installed. In order to use Puli you need to add the following to your composer.json.
```json
"require": {
// ...
"puli/composer-plugin": "^1.0.0-beta9",
"puli/repository": "^1.0-beta9",
"puli/discovery": "^1.0-beta9",
"puli/url-generator": "^1.0-beta4"
},
```


## Usage

In order to use this API client (or any other LinkedIn clients) you have to [register your application][register-app]
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
"php": ">=5.4",
"php-http/client-implementation": "^1.0",
"php-http/httplug": "^1.0",
"php-http/discovery": "^0.7|^0.8"
"php-http/message": "^1.0",
"php-http/message-factory": "^1.0",
"php-http/discovery": "^0.8"
},
"require-dev": {
"php-http/guzzle5-adapter": "^0.4",
"php-http/guzzle5-adapter": "^0.5",
"guzzlehttp/psr7": "^1.2",
"mockery/mockery": "^0.9",
"illuminate/support": "^5.0"
Expand Down
38 changes: 37 additions & 1 deletion src/Http/RequestManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,31 @@
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\MessageFactory;

/**
* A class to create HTTP requests and to send them.
*
* @author Tobias Nyholm <[email protected]>
*/
class RequestManager implements RequestManagerInterface
{
/**
* @var \Http\Client\HttpClient
*/
private $httpClient;

/**
* @var \Http\Message\MessageFactory
*/
private $messageFactory;

/**
* {@inheritdoc}
*/
public function sendRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1')
{
$request = MessageFactoryDiscovery::find()->createRequest($method, $uri, $headers, $body, $protocolVersion);
$request = $this->getMessageFactory()->createRequest($method, $uri, $headers, $body, $protocolVersion);

try {
return $this->getHttpClient()->sendRequest($request);
Expand Down Expand Up @@ -50,4 +61,29 @@ protected function getHttpClient()

return $this->httpClient;
}

/**
* @param MessageFactory $messageFactory
*
* @return RequestManager
*/
public function setMessageFactory(MessageFactory $messageFactory)
{
$this->messageFactory = $messageFactory;

return $this;
}

/**
*
* @return \Http\Message\MessageFactory
*/
private function getMessageFactory()
{
if ($this->messageFactory === null) {
$this->messageFactory = MessageFactoryDiscovery::find();
}

return $this->messageFactory;
}
}
11 changes: 11 additions & 0 deletions src/LinkedIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Happyr\LinkedIn\Http\UrlGeneratorInterface;
use Happyr\LinkedIn\Storage\DataStorageInterface;
use Http\Client\HttpClient;
use Http\Message\MessageFactory;
use Psr\Http\Message\ResponseInterface;

/**
Expand Down Expand Up @@ -354,6 +355,16 @@ public function setHttpClient(HttpClient $client)
return $this;
}

/**
* {@inheritdoc}
*/
public function setHttpMessageFactory(MessageFactory $factory)
{
$this->getRequestManager()->setMessageFactory($factory);

return $this;
}

/**
* @return RequestManager
*/
Expand Down
10 changes: 10 additions & 0 deletions src/LinkedInInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Happyr\LinkedIn\Http\UrlGeneratorInterface;
use Happyr\LinkedIn\Storage\DataStorageInterface;
use Http\Client\HttpClient;
use Http\Message\MessageFactory;
use Psr\Http\Message\ResponseInterface;

/**
Expand Down Expand Up @@ -165,4 +166,13 @@ public function setStorage(DataStorageInterface $storage);
* @return $this
*/
public function setHttpClient(HttpClient $client);

/**
* Set a http message factory.
*
* @param MessageFactory $factory
*
* @return $this
*/
public function setHttpMessageFactory(MessageFactory $factory);
}

0 comments on commit a47bd8b

Please sign in to comment.