Skip to content

Commit

Permalink
feat(Config): configure http client through dsn string for eased env …
Browse files Browse the repository at this point in the history
…var usage (#109)
  • Loading branch information
cdaguerre authored Mar 16, 2018
1 parent bd248b8 commit 6492edc
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 35 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "0.7.x-dev"
"dev-master": "1.0.x-dev"
}
}
}
42 changes: 30 additions & 12 deletions lib/Textmaster/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ class HttpClient implements HttpClientInterface
* @var array
*/
protected $options = [
'base_uri' => 'http://api.textmaster.com/%s',
'api_version' => 'v1',
'key' => null,
'secret' => null,
'base_uri' => 'http://api.textmaster.com/v1',
'user_agent' => 'textmaster-api (http://github.com/worldia/textmaster-api)',
];

Expand All @@ -56,29 +57,28 @@ class HttpClient implements HttpClientInterface
/**
* HttpClient constructor.
*
* @param string $key
* @param string $secret
* @param array $options
* @param string $dsn
*/
public function __construct($key, $secret, array $options = [])
public function __construct($dsn)
{
$options = array_merge($this->options, $options);
$this->parseDsn($dsn);

$options = $this->options;

$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(Middleware::mapRequest(function (RequestInterface $request) use ($key, $secret, $options) {
$stack->push(Middleware::mapRequest(function (RequestInterface $request) use ($options) {
$date = new \DateTime('now', new \DateTimeZone('UTC'));

return $request
->withHeader('User-Agent', $options['user_agent'])
->withHeader('Apikey', $key)
->withHeader('Apikey', $options['key'])
->withHeader('Date', $date->format('Y-m-d H:i:s'))
->withHeader('Signature', sha1($secret.$date->format('Y-m-d H:i:s')))
->withHeader('Signature', sha1($options['secret'].$date->format('Y-m-d H:i:s')))
;
}));

$this->options = array_merge($this->options, $options, ['handler' => $stack]);
$this->options['base_uri'] = sprintf($this->options['base_uri'], $this->options['api_version']);
$this->options = array_merge($options, ['handler' => $stack]);

$this->client = new Client($this->options);
}
Expand Down Expand Up @@ -193,4 +193,22 @@ protected function getFinalPath($path)
{
return $this->client->getConfig('base_uri')->getPath().'/'.$path;
}

/**
* @param string $dsn
*/
private function parseDsn($dsn)
{
$scheme = parse_url($dsn, PHP_URL_SCHEME);
$host = parse_url($dsn, PHP_URL_HOST);
$path = parse_url($dsn, PHP_URL_PATH);

parse_str(parse_url($dsn, PHP_URL_QUERY), $options);

$this->options = array_merge($this->options, $options);

$this->options['base_uri'] = sprintf('%s://%s%s', $scheme, $host, $path);
$this->options['key'] = urldecode(parse_url($dsn, PHP_URL_USER));
$this->options['secret'] = urldecode(parse_url($dsn, PHP_URL_PASS));
}
}
6 changes: 1 addition & 5 deletions test/Textmaster/Functional/Api/AuthorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ public function setUp()
{
parent::setUp();

$httpClient = new HttpClient(
'GFHunwb2DHw',
'gqvE7aZS_JM',
['base_uri' => 'http://api.sandbox.textmaster.com/%s']
);
$httpClient = new HttpClient('http://GFHunwb2DHw:[email protected]/v1');
$client = new Client($httpClient);
$this->api = $client->author();
}
Expand Down
12 changes: 2 additions & 10 deletions test/Textmaster/Functional/Api/ProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ public function setUp()
{
parent::setUp();

$httpClient = new HttpClient(
'GFHunwb2DHw',
'gqvE7aZS_JM',
['base_uri' => 'http://api.sandbox.textmaster.com/%s']
);
$httpClient = new HttpClient('http://GFHunwb2DHw:[email protected]/v1');
$client = new Client($httpClient);
$this->api = $client->project();
}
Expand All @@ -69,11 +65,7 @@ public static function tearDownAfterClass()
{
sleep(self::WAIT_TIME);

$httpClient = new HttpClient(
'GFHunwb2DHw',
'gqvE7aZS_JM',
['base_uri' => 'http://api.sandbox.textmaster.com/%s']
);
$httpClient = new HttpClient('http://GFHunwb2DHw:[email protected]/v1');
$client = new Client($httpClient);
$api = $client->project();

Expand Down
2 changes: 1 addition & 1 deletion test/Textmaster/Functional/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function setUp()
{
parent::setUp();

$httpClient = new HttpClient('GFHunwb2DHw', 'gqvE7aZS_JM', ['base_uri' => 'http://api.sandbox.textmaster.com/%s']);
$httpClient = new HttpClient('http://GFHunwb2DHw:gqvE7aZS_JM@api.sandbox.textmaster.com/v1');
$this->client = new Client($httpClient);
}

Expand Down
12 changes: 6 additions & 6 deletions test/Textmaster/Unit/HttpClient/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function shouldDoGETRequest()
$parameters = ['a' => 'b'];
$headers = ['c' => 'd'];

$httpClient = new HttpClient('key', 'secret', []);
$httpClient = new HttpClient('http://key:secret@api.sandbox.textmaster.com/v1');
$response = $httpClient->get($path, $parameters, $headers);

$this->assertTrue(in_array('Psr\Http\Message\ResponseInterface', class_implements($response), true));
Expand All @@ -39,7 +39,7 @@ public function shouldDoPOSTRequest()
$body = ['a' => 'b'];
$headers = ['c' => 'd'];

$httpClient = new HttpClient('key', 'secret', []);
$httpClient = new HttpClient('http://key:secret@api.sandbox.textmaster.com/v1');
$response = $httpClient->post($path, $body, $headers);

$this->assertTrue(in_array('Psr\Http\Message\ResponseInterface', class_implements($response), true));
Expand All @@ -52,7 +52,7 @@ public function shouldDoPOSTRequestWithoutContent()
{
$path = '/some/path';

$httpClient = new HttpClient('key', 'secret', []);
$httpClient = new HttpClient('http://key:secret@api.sandbox.textmaster.com/v1');
$response = $httpClient->post($path);

$this->assertTrue(in_array('Psr\Http\Message\ResponseInterface', class_implements($response), true));
Expand All @@ -67,7 +67,7 @@ public function shouldDoPATCHRequest()
$body = ['a' => 'b'];
$headers = ['c' => 'd'];

$httpClient = new HttpClient('key', 'secret', []);
$httpClient = new HttpClient('http://key:secret@api.sandbox.textmaster.com/v1');
$response = $httpClient->patch($path, $body, $headers);

$this->assertTrue(in_array('Psr\Http\Message\ResponseInterface', class_implements($response), true));
Expand All @@ -82,7 +82,7 @@ public function shouldDoDELETERequest()
$body = ['a' => 'b'];
$headers = ['c' => 'd'];

$httpClient = new HttpClient('key', 'secret', []);
$httpClient = new HttpClient('http://key:secret@api.sandbox.textmaster.com/v1');
$response = $httpClient->delete($path, $body, $headers);

$this->assertTrue(in_array('Psr\Http\Message\ResponseInterface', class_implements($response), true));
Expand All @@ -96,7 +96,7 @@ public function shouldDoPUTRequest()
$path = '/some/path';
$headers = ['c' => 'd'];

$httpClient = new HttpClient('key', 'secret', []);
$httpClient = new HttpClient('http://key:secret@api.sandbox.textmaster.com/v1');
$response = $httpClient->put($path, $headers);

$this->assertTrue(in_array('Psr\Http\Message\ResponseInterface', class_implements($response), true));
Expand Down

0 comments on commit 6492edc

Please sign in to comment.