3434use Illuminate \Http \Client \Response ;
3535use Illuminate \Support \Facades \Http ;
3636use InvalidArgumentException ;
37- use Psr \Http \Message \ResponseInterface ;
3837use RuntimeException ;
3938use Throwable ;
4039
@@ -58,9 +57,8 @@ public function __construct(
5857 ?string $ username = null ,
5958 ?string $ password = null ,
6059 ?string $ authMethod = null ,
61- ?int $ defaultAllowedDispatchRetries = null ,
62- )
63- {
60+ ?int $ defaultAllowedDispatchRetries = null ,
61+ ) {
6462 $ this ->baseUrl = rtrim ($ baseUrl ?? config ('oxylabs-api.base_url ' , 'https://data.oxylabs.io/v1/ ' ), '/ ' );
6563 $ this ->username = $ username ?? config ('oxylabs-api.username ' ) ?? '' ;
6664 $ this ->password = $ password ?? config ('oxylabs-api.password ' ) ?? '' ;
@@ -72,10 +70,10 @@ protected function getAuthHeader(): array
7270 {
7371 return match ($ this ->authMethod ) {
7472 'basic ' => [
75- 'Authorization ' => 'Basic ' . base64_encode ($ this ->username . ': ' . $ this ->password ),
73+ 'Authorization ' => 'Basic ' . base64_encode ($ this ->username . ': ' . $ this ->password ),
7674 ],
7775 'bearer ' => [
78- 'Authorization ' => 'Bearer ' . $ this ->password ,
76+ 'Authorization ' => 'Bearer ' . $ this ->password ,
7977 ],
8078 default => throw new InvalidArgumentException ('Invalid authentication method ' )
8179 };
@@ -86,7 +84,6 @@ protected function getBaseRequest(): Factory|PendingRequest
8684 return Http::withHeaders ($ this ->getAuthHeader ());
8785 }
8886
89-
9087 /**
9188 * @throws RuntimeException
9289 */
@@ -95,28 +92,28 @@ public function makePostRequest(string $source, array $payload, ?int $allowedRet
9592 try {
9693 $ response = $ this ->makeRequest (
9794 'post ' ,
98- $ this ->baseUrl . '/queries ' ,
95+ $ this ->baseUrl . '/queries ' ,
9996 [
10097 'source ' => $ source ,
10198 ...$ payload ,
10299 ],
103100 $ allowedRetries ?? 0 ,
104101 );
105102
106- if (!$ response ->successful ()) {
107- throw new RuntimeException ('API request failed: ' . $ response ->getBody ()->getContents (), $ response ->getStatusCode ());
103+ if (! $ response ->successful ()) {
104+ throw new RuntimeException ('API request failed: ' . $ response ->getBody ()->getContents (), $ response ->getStatusCode ());
108105 }
109106
110107 return PushPullJob::from (json_decode ($ response ->getBody ()->getContents (), true ));
111108 } catch (Throwable $ e ) {
112- throw new RuntimeException ('API request failed: ' . $ e ->getMessage (), $ e ->getCode (), $ e );
109+ throw new RuntimeException ('API request failed: ' . $ e ->getMessage (), $ e ->getCode (), $ e );
113110 }
114111 }
115112
116113 /**
117114 * @throws GuzzleException|Throwable
118115 */
119- protected function makeRequest (string $ method , string $ uri , ?array $ payload = null , int $ retryCount = null ): Response
116+ protected function makeRequest (string $ method , string $ uri , ?array $ payload = null , ? int $ retryCount = null ): Response
120117 {
121118 $ request = new Request (
122119 method: $ method ,
@@ -125,7 +122,6 @@ protected function makeRequest(string $method, string $uri, ?array $payload = nu
125122 body: $ payload ? json_encode ($ payload ) : null ,
126123 );
127124
128-
129125 if (config ('oxylabs-api.request_logging_enabled ' , true )) {
130126 $ logger = OxylabsApiRequestLogger::makeFromGuzzle ($ request );
131127 $ logger ->save ();
@@ -155,23 +151,22 @@ protected function makeRequest(string $method, string $uri, ?array $payload = nu
155151 * @throws RuntimeException
156152 */
157153 public function getResult (
158- string $ job_id ,
154+ string $ job_id ,
159155 ?string $ type = null ,
160- ): ?array
161- {
156+ ): ?array {
162157 try {
163158 $ response = $ this ->makeRequest (
164159 'get ' ,
165- $ this ->baseUrl . "/queries/ $ job_id/results " . ($ type ? "?type= $ type " : '' ),
160+ $ this ->baseUrl . "/queries/ $ job_id/results " . ($ type ? "?type= $ type " : '' ),
166161 null ,
167162 3
168163 );
169164 } catch (Throwable $ e ) {
170- throw new RuntimeException ('API request failed: ' . $ e ->getMessage (), $ e ->getCode (), $ e );
165+ throw new RuntimeException ('API request failed: ' . $ e ->getMessage (), $ e ->getCode (), $ e );
171166 }
172167
173- if (!$ response ->successful ()) {
174- throw new RuntimeException ('API request failed: ' . $ response ->getBody ()->getContents (), $ response ->getStatusCode ());
168+ if (! $ response ->successful ()) {
169+ throw new RuntimeException ('API request failed: ' . $ response ->getBody ()->getContents (), $ response ->getStatusCode ());
175170 }
176171
177172 return json_decode ($ response ->getBody ()->getContents (), true );
@@ -185,12 +180,12 @@ public function getPushPullJob(string $job_id): PushPullJob
185180 {
186181 $ response = $ this ->makeRequest (
187182 'get ' ,
188- $ this ->baseUrl . "/queries/ $ job_id " ,
183+ $ this ->baseUrl . "/queries/ $ job_id " ,
189184 retryCount: 3 ,
190185 );
191186
192- if (!$ response ->successful ()) {
193- throw new RuntimeException ('API request failed: ' . $ response ->getBody ()->getContents (), $ response ->getStatusCode ());
187+ if (! $ response ->successful ()) {
188+ throw new RuntimeException ('API request failed: ' . $ response ->getBody ()->getContents (), $ response ->getStatusCode ());
194189 }
195190
196191 return PushPullJob::from (json_decode ($ response ->getBody ()->getContents (), true ));
@@ -202,19 +197,18 @@ public function getPushPullJob(string $job_id): PushPullJob
202197 * @throws Throwable
203198 */
204199 public function getPushPullResults (
205- string $ job_id ,
206- bool $ check_status = false ,
207- int $ status_check_limit = 5 ,
208- int $ status_wait_seconds = 3 ,
200+ string $ job_id ,
201+ bool $ check_status = false ,
202+ int $ status_check_limit = 5 ,
203+ int $ status_wait_seconds = 3 ,
209204 ?string $ type = null ,
210- ): ?array
211- {
205+ ): ?array {
212206 if ($ check_status ) {
213207 retry (
214208 $ status_check_limit ,
215209 function () use ($ job_id ): PushPullJob {
216210 $ job = $ this ->getPushPullJob ($ job_id );
217- if (!$ job ->isDone ()) {
211+ if (! $ job ->isDone ()) {
218212 throw new Exception ("Job $ job_id not completed " );
219213 }
220214
@@ -231,13 +225,12 @@ function () use ($job_id): PushPullJob {
231225 * @throws ConnectionException|Throwable
232226 */
233227 public function getAmazonProductResult (
234- string $ job_id ,
235- bool $ check_status = false ,
236- int $ status_check_limit = 5 ,
237- int $ status_wait_seconds = 3 ,
228+ string $ job_id ,
229+ bool $ check_status = false ,
230+ int $ status_check_limit = 5 ,
231+ int $ status_wait_seconds = 3 ,
238232 ?string $ type = 'parsed ' ,
239- ): AmazonProductResponse
240- {
233+ ): AmazonProductResponse {
241234 $ response = $ this ->getPushPullResults ($ job_id , $ check_status , $ status_check_limit , $ status_wait_seconds , $ type );
242235
243236 return AmazonProductResponse::from ($ response );
@@ -247,13 +240,12 @@ public function getAmazonProductResult(
247240 * @throws ConnectionException|RuntimeException|Throwable
248241 */
249242 public function getAmazonResult (
250- string $ job_id ,
251- bool $ check_status = false ,
252- int $ status_check_limit = 5 ,
253- int $ status_wait_seconds = 3 ,
243+ string $ job_id ,
244+ bool $ check_status = false ,
245+ int $ status_check_limit = 5 ,
246+ int $ status_wait_seconds = 3 ,
254247 ?string $ type = 'parsed ' ,
255- ): AmazonResponse
256- {
248+ ): AmazonResponse {
257249 $ response = $ this ->getPushPullResults ($ job_id , $ check_status , $ status_check_limit , $ status_wait_seconds , $ type );
258250
259251 return AmazonResponse::from ($ response );
@@ -263,13 +255,12 @@ public function getAmazonResult(
263255 * @throws ConnectionException|Throwable
264256 */
265257 public function getUniversalResult (
266- string $ job_id ,
267- bool $ check_status = false ,
268- int $ status_check_limit = 5 ,
269- int $ status_wait_seconds = 3 ,
258+ string $ job_id ,
259+ bool $ check_status = false ,
260+ int $ status_check_limit = 5 ,
261+ int $ status_wait_seconds = 3 ,
270262 ?string $ type = 'raw ' ,
271- ): UniversalResponse
272- {
263+ ): UniversalResponse {
273264 $ response = $ this ->getPushPullResults ($ job_id , $ check_status , $ status_check_limit , $ status_wait_seconds , $ type );
274265
275266 return UniversalResponse::from ($ response );
@@ -279,13 +270,12 @@ public function getUniversalResult(
279270 * @throws ConnectionException|Throwable
280271 */
281272 public function getAmazonPricingResult (
282- string $ job_id ,
283- bool $ check_status = false ,
284- int $ status_check_limit = 5 ,
285- int $ status_wait_seconds = 3 ,
273+ string $ job_id ,
274+ bool $ check_status = false ,
275+ int $ status_check_limit = 5 ,
276+ int $ status_wait_seconds = 3 ,
286277 ?string $ type = 'parsed ' ,
287- ): AmazonPricingResponse
288- {
278+ ): AmazonPricingResponse {
289279 $ response = $ this ->getPushPullResults ($ job_id , $ check_status , $ status_check_limit , $ status_wait_seconds , $ type );
290280
291281 return AmazonPricingResponse::from ($ response );
@@ -296,13 +286,12 @@ public function getAmazonPricingResult(
296286 * @throws Throwable
297287 */
298288 public function getAmazonSellerResult (
299- string $ job_id ,
300- bool $ check_status = false ,
301- int $ status_check_limit = 5 ,
302- int $ status_wait_seconds = 3 ,
289+ string $ job_id ,
290+ bool $ check_status = false ,
291+ int $ status_check_limit = 5 ,
292+ int $ status_wait_seconds = 3 ,
303293 ?string $ type = 'parsed ' ,
304- ): AmazonSellerResponse
305- {
294+ ): AmazonSellerResponse {
306295 $ response = $ this ->getPushPullResults ($ job_id , $ check_status , $ status_check_limit , $ status_wait_seconds , $ type );
307296
308297 return AmazonSellerResponse::from ($ response );
@@ -315,10 +304,10 @@ public function getAmazonSellerResult(
315304 public function makeBatchRequest (BatchRequest $ payload ): PushPullBatchJobResponse
316305 {
317306 $ response = $ this ->getBaseRequest ()
318- ->post ($ this ->baseUrl . '/queries/batch ' , $ payload ->toArray ());
307+ ->post ($ this ->baseUrl . '/queries/batch ' , $ payload ->toArray ());
319308
320- if (!$ response ->successful ()) {
321- throw new RuntimeException ('API request failed: ' . $ response ->body ());
309+ if (! $ response ->successful ()) {
310+ throw new RuntimeException ('API request failed: ' . $ response ->body ());
322311 }
323312
324313 return PushPullBatchJobResponse::from ($ response ->json ());
@@ -393,13 +382,12 @@ public function googleShoppingProduct(GoogleShoppingProductRequest $request, ?in
393382 * @throws Throwable
394383 */
395384 public function getGoogleShoppingProductResult (
396- string $ job_id ,
397- bool $ check_status = false ,
398- int $ status_check_limit = 5 ,
399- int $ status_wait_seconds = 3 ,
385+ string $ job_id ,
386+ bool $ check_status = false ,
387+ int $ status_check_limit = 5 ,
388+ int $ status_wait_seconds = 3 ,
400389 ?string $ type = 'parsed ' ,
401- ): GoogleShoppingProductResponse
402- {
390+ ): GoogleShoppingProductResponse {
403391 $ response = $ this ->getPushPullResults ($ job_id , $ check_status , $ status_check_limit , $ status_wait_seconds , $ type );
404392
405393 return GoogleShoppingProductResponse::from ($ response );
@@ -417,13 +405,12 @@ public function googleShoppingPricing(GoogleShoppingPricingRequest $request, ?in
417405 * @throws ConnectionException|Throwable
418406 */
419407 public function getGoogleShoppingPricingResult (
420- string $ job_id ,
421- bool $ check_status = false ,
422- int $ status_check_limit = 5 ,
423- int $ status_wait_seconds = 3 ,
408+ string $ job_id ,
409+ bool $ check_status = false ,
410+ int $ status_check_limit = 5 ,
411+ int $ status_wait_seconds = 3 ,
424412 ?string $ type = 'parsed ' ,
425- ): GoogleShoppingPricingResponse
426- {
413+ ): GoogleShoppingPricingResponse {
427414 $ response = $ this ->getPushPullResults ($ job_id , $ check_status , $ status_check_limit , $ status_wait_seconds , $ type );
428415
429416 return GoogleShoppingPricingResponse::from ($ response );
@@ -442,13 +429,12 @@ public function walmartProduct(WalmartProductRequest $request, ?int $allowedRetr
442429 * @throws ConnectionException
443430 */
444431 public function getWalmartProductResult (
445- string $ job_id ,
446- bool $ check_status = false ,
447- int $ status_check_limit = 5 ,
448- int $ status_wait_seconds = 3 ,
432+ string $ job_id ,
433+ bool $ check_status = false ,
434+ int $ status_check_limit = 5 ,
435+ int $ status_wait_seconds = 3 ,
449436 ?string $ type = 'parsed ' ,
450- ): WalmartProductResponse
451- {
437+ ): WalmartProductResponse {
452438 $ response = $ this ->getPushPullResults ($ job_id , $ check_status , $ status_check_limit , $ status_wait_seconds , $ type );
453439
454440 return WalmartProductResponse::from ($ response );
0 commit comments