@@ -38,8 +38,9 @@ class HttpClient implements HttpClientInterface
38
38
* @var array
39
39
*/
40
40
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 ' ,
43
44
'user_agent ' => 'textmaster-api (http://github.com/worldia/textmaster-api) ' ,
44
45
];
45
46
@@ -56,29 +57,28 @@ class HttpClient implements HttpClientInterface
56
57
/**
57
58
* HttpClient constructor.
58
59
*
59
- * @param string $key
60
- * @param string $secret
61
- * @param array $options
60
+ * @param string $dsn
62
61
*/
63
- public function __construct ($ key , $ secret , array $ options = [] )
62
+ public function __construct ($ dsn )
64
63
{
65
- $ options = array_merge ($ this ->options , $ options );
64
+ $ this ->parseDsn ($ dsn );
65
+
66
+ $ options = $ this ->options ;
66
67
67
68
$ stack = new HandlerStack ();
68
69
$ 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 ) {
70
71
$ date = new \DateTime ('now ' , new \DateTimeZone ('UTC ' ));
71
72
72
73
return $ request
73
74
->withHeader ('User-Agent ' , $ options ['user_agent ' ])
74
- ->withHeader ('Apikey ' , $ key )
75
+ ->withHeader ('Apikey ' , $ options [ ' key ' ] )
75
76
->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 ' )))
77
78
;
78
79
}));
79
80
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 ]);
82
82
83
83
$ this ->client = new Client ($ this ->options );
84
84
}
@@ -193,4 +193,22 @@ protected function getFinalPath($path)
193
193
{
194
194
return $ this ->client ->getConfig ('base_uri ' )->getPath ().'/ ' .$ path ;
195
195
}
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
+ }
196
214
}
0 commit comments