33namespace Xolphin ;
44
55use GuzzleHttp \Exception \RequestException ;
6+ use Psr \Http \Message \StreamInterface ;
67use Xolphin \Endpoint \Certificate ;
8+ use Xolphin \Endpoint \Invoice ;
79use Xolphin \Endpoint \Request ;
810use Xolphin \Endpoint \Support ;
11+ use Xolphin \Exception \XolphinRequestException ;
912
1013class Client {
1114 const BASE_URI = 'https://api.xolphin.com/v%d/ ' ;
1215 const BASE_URI_TEST = 'https://test-api.xolphin.com/v%d/ ' ;
1316 const API_VERSION = 1 ;
14- const VERSION = '1.6.0 ' ;
17+ const VERSION = '1.8.3 ' ;
1518
1619 private $ username = '' ;
1720 private $ password = '' ;
1821 private $ guzzle ;
1922
23+ /** @var int */
24+ private $ limit ;
25+
26+ /** @var int */
27+ private $ requestsRemaining ;
28+
2029 /**
2130 * Client constructor.
2231 * @param string $username
@@ -27,6 +36,9 @@ function __construct($username, $password, $test=false) {
2736 $ this ->username = $ username ;
2837 $ this ->password = $ password ;
2938
39+ $ this ->limit = 1 ;
40+ $ this ->requestsRemaining = 1 ;
41+
3042 $ options = [
3143 'base_uri ' => sprintf (($ test ? Client::BASE_URI_TEST : Client::BASE_URI ), Client::API_VERSION ),
3244 'auth ' => [$ this ->username , $ this ->password ],
@@ -42,6 +54,22 @@ function __construct($username, $password, $test=false) {
4254 $ this ->guzzle = new \GuzzleHttp \Client ($ options );
4355 }
4456
57+ /**
58+ * @return int
59+ */
60+ public function getLimit ()
61+ {
62+ return $ this ->limit ;
63+ }
64+
65+ /**
66+ * @return int
67+ */
68+ public function getRequestsRemaining ()
69+ {
70+ return $ this ->requestsRemaining ;
71+ }
72+
4573 /**
4674 * @param string $method
4775 * @param array $data
@@ -51,18 +79,10 @@ function __construct($username, $password, $test=false) {
5179 public function get ($ method , $ data = []) {
5280 try {
5381 $ result = $ this ->guzzle ->get ($ method , ['query ' => $ data ]);
82+ $ this ->updateLimitAndRemaining ($ result ->getHeader ('X-RateLimit-Limit ' )[0 ], $ result ->getHeader ('X-RateLimit-Remaining ' )[0 ]);
5483 return json_decode ($ result ->getBody ());
5584 } catch (RequestException $ e ) {
56- $ data = json_decode ($ e ->getResponse ()->getBody ());
57- if ($ data == NULL ) {
58- throw new \Exception ($ e ->getResponse ()->getBody ());
59- } else {
60- if (isset ($ data ->message ) || isset ($ data ->errors )) {
61- throw new \Exception (json_encode ($ data ), $ e ->getCode ());
62- } else {
63- throw new \Exception ($ e ->getMessage (), $ e ->getCode ());
64- }
65- }
85+ throw XolphinRequestException::createFromRequestException ($ e );
6686 }
6787 }
6888
@@ -91,34 +111,35 @@ public function post($method, $data = []) {
91111 }
92112
93113 $ result = $ this ->guzzle ->post ($ method , ['multipart ' => $ mp ]);
114+ $ this ->updateLimitAndRemaining ($ result ->getHeader ('X-RateLimit-Limit ' )[0 ], $ result ->getHeader ('X-RateLimit-Remaining ' )[0 ]);
94115 return json_decode ($ result ->getBody ());
95116 } catch (RequestException $ e ) {
96- $ data = json_decode ($ e ->getResponse ()->getBody ());
97- if ($ data == NULL ) {
98- throw new \Exception ($ e ->getResponse ()->getBody ());
99- } else {
100- throw new \Exception (json_encode ($ data ), $ e ->getCode ());
101- }
117+ throw XolphinRequestException::createFromRequestException ($ e );
102118 }
103119 }
104120
121+ /**
122+ * @param $limit
123+ * @param $remaining
124+ */
125+ public function updateLimitAndRemaining ($ limit , $ remaining ) {
126+ $ this ->limit = $ limit ;
127+ $ this ->requestsRemaining = $ remaining ;
128+ }
129+
105130 /**
106131 * @param string $method
107132 * @param array $data
108- * @return \Psr\Http\Message\ StreamInterface
133+ * @return StreamInterface
109134 * @throws \Exception
110135 */
111136 public function download ($ method , $ data = []) {
112137 try {
113138 $ result = $ this ->guzzle ->get ($ method , ['query ' => $ data ]);
139+ $ this ->updateLimitAndRemaining ($ result ->getHeader ('X-RateLimit-Limit ' )[0 ], $ result ->getHeader ('X-RateLimit-Remaining ' )[0 ]);
114140 return $ result ->getBody ();
115141 } catch (RequestException $ e ) {
116- try {
117- $ data = json_decode ($ e ->getResponse ()->getBody ());
118- throw new \Exception ($ data ->message );
119- } catch (\Exception $ ex ) {
120- throw new \Exception ($ e ->getResponse ()->getBody ());
121- }
142+ throw XolphinRequestException::createFromRequestException ($ e );
122143 }
123144 }
124145
@@ -142,4 +163,11 @@ public function certificate() {
142163 public function support () {
143164 return new Support ($ this );
144165 }
166+
167+ /**
168+ * @return Invoice
169+ */
170+ public function invoice () {
171+ return new Invoice ($ this );
172+ }
145173}
0 commit comments