Skip to content

Commit 6492edc

Browse files
authored
feat(Config): configure http client through dsn string for eased env var usage (#109)
1 parent bd248b8 commit 6492edc

File tree

6 files changed

+41
-35
lines changed

6 files changed

+41
-35
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
},
3434
"extra": {
3535
"branch-alias": {
36-
"dev-master": "0.7.x-dev"
36+
"dev-master": "1.0.x-dev"
3737
}
3838
}
3939
}

lib/Textmaster/HttpClient/HttpClient.php

+30-12
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ class HttpClient implements HttpClientInterface
3838
* @var array
3939
*/
4040
protected $options = [
41-
'base_uri' => 'http://api.textmaster.com/%s',
42-
'api_version' => 'v1',
41+
'key' => null,
42+
'secret' => null,
43+
'base_uri' => 'http://api.textmaster.com/v1',
4344
'user_agent' => 'textmaster-api (http://github.com/worldia/textmaster-api)',
4445
];
4546

@@ -56,29 +57,28 @@ class HttpClient implements HttpClientInterface
5657
/**
5758
* HttpClient constructor.
5859
*
59-
* @param string $key
60-
* @param string $secret
61-
* @param array $options
60+
* @param string $dsn
6261
*/
63-
public function __construct($key, $secret, array $options = [])
62+
public function __construct($dsn)
6463
{
65-
$options = array_merge($this->options, $options);
64+
$this->parseDsn($dsn);
65+
66+
$options = $this->options;
6667

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

7273
return $request
7374
->withHeader('User-Agent', $options['user_agent'])
74-
->withHeader('Apikey', $key)
75+
->withHeader('Apikey', $options['key'])
7576
->withHeader('Date', $date->format('Y-m-d H:i:s'))
76-
->withHeader('Signature', sha1($secret.$date->format('Y-m-d H:i:s')))
77+
->withHeader('Signature', sha1($options['secret'].$date->format('Y-m-d H:i:s')))
7778
;
7879
}));
7980

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

8383
$this->client = new Client($this->options);
8484
}
@@ -193,4 +193,22 @@ protected function getFinalPath($path)
193193
{
194194
return $this->client->getConfig('base_uri')->getPath().'/'.$path;
195195
}
196+
197+
/**
198+
* @param string $dsn
199+
*/
200+
private function parseDsn($dsn)
201+
{
202+
$scheme = parse_url($dsn, PHP_URL_SCHEME);
203+
$host = parse_url($dsn, PHP_URL_HOST);
204+
$path = parse_url($dsn, PHP_URL_PATH);
205+
206+
parse_str(parse_url($dsn, PHP_URL_QUERY), $options);
207+
208+
$this->options = array_merge($this->options, $options);
209+
210+
$this->options['base_uri'] = sprintf('%s://%s%s', $scheme, $host, $path);
211+
$this->options['key'] = urldecode(parse_url($dsn, PHP_URL_USER));
212+
$this->options['secret'] = urldecode(parse_url($dsn, PHP_URL_PASS));
213+
}
196214
}

test/Textmaster/Functional/Api/AuthorTest.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ public function setUp()
3636
{
3737
parent::setUp();
3838

39-
$httpClient = new HttpClient(
40-
'GFHunwb2DHw',
41-
'gqvE7aZS_JM',
42-
['base_uri' => 'http://api.sandbox.textmaster.com/%s']
43-
);
39+
$httpClient = new HttpClient('http://GFHunwb2DHw:gqvE7aZS_JM@api.sandbox.textmaster.com/v1');
4440
$client = new Client($httpClient);
4541
$this->api = $client->author();
4642
}

test/Textmaster/Functional/Api/ProjectTest.php

+2-10
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ public function setUp()
5353
{
5454
parent::setUp();
5555

56-
$httpClient = new HttpClient(
57-
'GFHunwb2DHw',
58-
'gqvE7aZS_JM',
59-
['base_uri' => 'http://api.sandbox.textmaster.com/%s']
60-
);
56+
$httpClient = new HttpClient('http://GFHunwb2DHw:gqvE7aZS_JM@api.sandbox.textmaster.com/v1');
6157
$client = new Client($httpClient);
6258
$this->api = $client->project();
6359
}
@@ -69,11 +65,7 @@ public static function tearDownAfterClass()
6965
{
7066
sleep(self::WAIT_TIME);
7167

72-
$httpClient = new HttpClient(
73-
'GFHunwb2DHw',
74-
'gqvE7aZS_JM',
75-
['base_uri' => 'http://api.sandbox.textmaster.com/%s']
76-
);
68+
$httpClient = new HttpClient('http://GFHunwb2DHw:gqvE7aZS_JM@api.sandbox.textmaster.com/v1');
7769
$client = new Client($httpClient);
7870
$api = $client->project();
7971

test/Textmaster/Functional/ManagerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function setUp()
2929
{
3030
parent::setUp();
3131

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

test/Textmaster/Unit/HttpClient/HttpClientTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function shouldDoGETRequest()
2424
$parameters = ['a' => 'b'];
2525
$headers = ['c' => 'd'];
2626

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

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

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)