33namespace EasyPost \Http ;
44
55use EasyPost \Constant \Constants ;
6+ use EasyPost \EasyPostClient ;
67use EasyPost \EasypostObject ;
78use EasyPost \Exception \Error ;
89use GuzzleHttp \Client ;
910
1011class Requestor
1112{
12- /**
13- * Constructor.
14- *
15- * @param EasyPostClient $client
16- */
17- public function __construct ($ client )
18- {
19- $ this ->client = $ client ;
20- }
21-
2213 /**
2314 * Get the API URL.
2415 *
16+ * @param EasyPostClient $client
2517 * @param string $url
2618 * @param bool $beta
2719 * @return string
2820 */
29- private function absoluteUrl ($ url = '' , $ beta = false )
21+ private static function absoluteUrl ($ client , $ url = '' , $ beta = false )
3022 {
3123 if ($ beta ) {
3224 $ apiBase = Constants::API_BASE . '/ ' . Constants::BETA_API_VERSION ;
3325 } else {
34- $ apiBase = $ this -> client ->apiBase . '/ ' . Constants::API_VERSION ;
26+ $ apiBase = $ client ->getApiBase () . '/ ' . Constants::API_VERSION ;
3527 }
3628
3729 return "{$ apiBase }{$ url }" ;
@@ -120,37 +112,39 @@ public static function urlEncode($arr, $prefix = null)
120112 /**
121113 * Make a request to the EasyPost API.
122114 *
115+ * @param EasyPostClient $client
123116 * @param string $method
124117 * @param string $url
125118 * @param mixed $params
126119 * @param bool $beta
127120 * @return array
128121 * @throws \EasyPost\Exception\Error
129122 */
130- public function request ($ method , $ url , $ params = null , $ beta = false )
123+ public static function request ($ client , $ method , $ url , $ params = null , $ beta = false )
131124 {
132- list ($ responseBody , $ httpStatus ) = $ this -> requestRaw ($ method , $ url , $ params , $ beta );
133- $ httpBody = $ this -> interpretResponse ($ responseBody , $ httpStatus );
125+ list ($ responseBody , $ httpStatus ) = self :: requestRaw ($ client , $ method , $ url , $ params , $ beta );
126+ $ httpBody = self :: interpretResponse ($ responseBody , $ httpStatus );
134127
135128 return $ httpBody ;
136129 }
137130
138131 /**
139132 * Internal logic required to make a request to the EasyPost API.
140133 *
134+ * @param EasyPostClient $client
141135 * @param string $method
142136 * @param string $url
143137 * @param mixed $params
144138 * @param bool $beta
145139 * @return array
146140 * @throws \EasyPost\Exception\Error
147141 */
148- private function requestRaw ($ method , $ url , $ params , $ beta = false )
142+ private static function requestRaw ($ client , $ method , $ url , $ params , $ beta = false )
149143 {
150- $ absoluteUrl = $ this -> absoluteUrl ($ url , $ beta );
144+ $ absoluteUrl = self :: absoluteUrl ($ client , $ url , $ beta );
151145 $ requestOptions = [
152146 'http_errors ' => false , // we set this false here so we can do our own error handling
153- 'timeout ' => $ this -> client ->timeout ,
147+ 'timeout ' => $ client ->getTimeout () ,
154148 ];
155149 $ params = self ::encodeObjects ($ params );
156150 if (in_array (strtolower ($ method ), ['get ' , 'delete ' ])) {
@@ -166,7 +160,7 @@ private function requestRaw($method, $url, $params, $beta = false)
166160
167161 $ headers = [
168162 'Accept ' => 'application/json ' ,
169- 'Authorization ' => "Bearer {$ this -> client ->apiKey }" ,
163+ 'Authorization ' => "Bearer {$ client ->getApiKey () }" ,
170164 'Content-Type ' => 'application/json ' ,
171165 'User-Agent ' => 'EasyPost/v2 PhpClient/ ' . Constants::LIBRARY_VERSION . " PHP/ $ phpVersion OS/ $ osType OSVersion/ $ osVersion OSArch/ $ osArch " ,
172166 ];
@@ -176,7 +170,7 @@ private function requestRaw($method, $url, $params, $beta = false)
176170 try {
177171 $ response = $ guzzleClient ->request ($ method , $ absoluteUrl , $ requestOptions );
178172 } catch (\GuzzleHttp \Exception \ConnectException $ error ) {
179- $ message = " Unexpected error communicating with EasyPost. If this problem persists please let us know at { $ this -> supportEmail } . {$ error ->getMessage ()}" ;
173+ $ message = ' Unexpected error communicating with EasyPost. If this problem persists please let us know at ' . Constants:: SUPPORT_EMAIL . " . {$ error ->getMessage ()}" ;
180174 throw new Error ($ message , null , null );
181175 }
182176
@@ -199,7 +193,7 @@ private function requestRaw($method, $url, $params, $beta = false)
199193 * @return mixed
200194 * @throws \EasyPost\Exception\Error
201195 */
202- public function interpretResponse ($ httpBody , $ httpStatus )
196+ public static function interpretResponse ($ httpBody , $ httpStatus )
203197 {
204198 try {
205199 $ response = json_decode ($ httpBody , true );
@@ -208,7 +202,7 @@ public function interpretResponse($httpBody, $httpStatus)
208202 }
209203
210204 if ($ httpStatus < 200 || $ httpStatus >= 300 ) {
211- $ this -> handleApiError ($ httpBody , $ httpStatus , $ response );
205+ self :: handleApiError ($ httpBody , $ httpStatus , $ response );
212206 }
213207
214208 return $ response ;
@@ -222,7 +216,7 @@ public function interpretResponse($httpBody, $httpStatus)
222216 * @param array $response
223217 * @throws \EasyPost\Exception\Error
224218 */
225- public function handleApiError ($ httpBody , $ httpStatus , $ response )
219+ public static function handleApiError ($ httpBody , $ httpStatus , $ response )
226220 {
227221 if (!is_array ($ response ) || !isset ($ response ['error ' ])) {
228222 throw new Error ("Invalid response object from API: HTTP Status: ( {$ httpStatus }) {$ httpBody }) " , $ httpStatus , $ httpBody );
0 commit comments