|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ziming\LaravelScrapingBee; |
| 4 | + |
| 5 | +use Illuminate\Http\Client\ConnectionException; |
| 6 | +use Illuminate\Http\Client\Response; |
| 7 | +use Illuminate\Support\Facades\Http; |
| 8 | +use Illuminate\Support\Traits\Conditionable; |
| 9 | + |
| 10 | +final class LaravelScrapingBeeWalmartProduct |
| 11 | +{ |
| 12 | + use Conditionable; |
| 13 | + |
| 14 | + private readonly string $baseUrl; |
| 15 | + private readonly string $apiKey; |
| 16 | + |
| 17 | + private array $params = []; |
| 18 | + |
| 19 | + public static function make(#[\SensitiveParameter] ?string $apiKey = null): self |
| 20 | + { |
| 21 | + return new self($apiKey); |
| 22 | + } |
| 23 | + |
| 24 | + public function __construct(#[\SensitiveParameter] ?string $apiKey = null) |
| 25 | + { |
| 26 | + // If somebody pass '' into the constructor, we should use '' as the api key |
| 27 | + // even if it doesn't make sense. |
| 28 | + // If $apiKey is null, then we use the 1 in the config file. |
| 29 | + $this->apiKey = $apiKey ?? config('scrapingbee.api_key'); |
| 30 | + |
| 31 | + $this->baseUrl = config( |
| 32 | + 'scrapingbee.walmart_product_base_url', |
| 33 | + 'https://app.scrapingbee.com/api/v1/walmart/product' |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @throws ConnectionException |
| 39 | + */ |
| 40 | + public function get(): Response |
| 41 | + { |
| 42 | + $this->params['api_key'] = $this->apiKey; |
| 43 | + $response = Http::get($this->baseUrl, $this->params); |
| 44 | + $this->reset(); |
| 45 | + |
| 46 | + return $response; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * https://www.scrapingbee.com/documentation/walmart/#light_request_WalmartAPIProduct |
| 51 | + */ |
| 52 | + public function lightRequest(bool $lightRequest = true): self |
| 53 | + { |
| 54 | + $this->params['light_request'] = $lightRequest; |
| 55 | + |
| 56 | + return $this; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * https://www.scrapingbee.com/documentation/walmart/#product_id |
| 61 | + */ |
| 62 | + public function productId(string $storeId): self |
| 63 | + { |
| 64 | + $this->params['product_id'] = $storeId; |
| 65 | + |
| 66 | + return $this; |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + /** |
| 71 | + * https://www.scrapingbee.com/documentation/walmart/#domain_WalmartAPIProduct |
| 72 | + */ |
| 73 | + public function domain(string $domain): self |
| 74 | + { |
| 75 | + $this->params['domain'] = $domain; |
| 76 | + |
| 77 | + return $this; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * https://www.scrapingbee.com/documentation/walmart/#delivery_zip_WalmartAPIProduct |
| 82 | + */ |
| 83 | + public function deliveryZip(string $deliveryZip): self |
| 84 | + { |
| 85 | + $this->params['delivery_zip'] = $deliveryZip; |
| 86 | + |
| 87 | + return $this; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * https://www.scrapingbee.com/documentation/walmart/#store_id_WalmartAPIProduct |
| 92 | + */ |
| 93 | + public function storeId(string $storeId): self |
| 94 | + { |
| 95 | + $this->params['store_id'] = $storeId; |
| 96 | + |
| 97 | + return $this; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * https://www.scrapingbee.com/documentation/google/#extra_params |
| 102 | + */ |
| 103 | + public function extraParams(array $extraParams): self |
| 104 | + { |
| 105 | + $this->params['extra_params'] = http_build_query($extraParams); |
| 106 | + |
| 107 | + return $this; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * https://www.scrapingbee.com/documentation/walmart/#add_html_WalmartAPIProduct |
| 112 | + */ |
| 113 | + public function addHtml(): self |
| 114 | + { |
| 115 | + $this->params['add_html'] = true; |
| 116 | + |
| 117 | + return $this; |
| 118 | + } |
| 119 | + |
| 120 | + /* |
| 121 | + * If the API hasn't caught up and you need to support a new ScrapingBee parameter, |
| 122 | + * you can set it using this method. |
| 123 | + */ |
| 124 | + public function setParam(string $key, mixed $value): self |
| 125 | + { |
| 126 | + $this->params[$key] = $value; |
| 127 | + |
| 128 | + return $this; |
| 129 | + } |
| 130 | + |
| 131 | + private function reset(): self |
| 132 | + { |
| 133 | + $this->params = []; |
| 134 | + |
| 135 | + return $this; |
| 136 | + } |
| 137 | +} |
0 commit comments