|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Ziming\LaravelScrapingBee; |
| 6 | + |
| 7 | +use Illuminate\Http\Client\ConnectionException; |
| 8 | +use Illuminate\Http\Client\Response; |
| 9 | +use Illuminate\Support\Facades\Http; |
| 10 | +use Illuminate\Support\Traits\Conditionable; |
| 11 | + |
| 12 | +final class LaravelScrapingBeeWalmartSearch |
| 13 | +{ |
| 14 | + use Conditionable; |
| 15 | + |
| 16 | + private readonly string $baseUrl; |
| 17 | + private readonly string $apiKey; |
| 18 | + |
| 19 | + private array $params = []; |
| 20 | + |
| 21 | + public static function make(#[\SensitiveParameter] ?string $apiKey = null): self |
| 22 | + { |
| 23 | + return new self($apiKey); |
| 24 | + } |
| 25 | + |
| 26 | + public function __construct(#[\SensitiveParameter] ?string $apiKey = null) |
| 27 | + { |
| 28 | + // If somebody pass '' into the constructor, we should use '' as the api key |
| 29 | + // even if it doesn't make sense. |
| 30 | + // If $apiKey is null, then we use the 1 in the config file. |
| 31 | + $this->apiKey = $apiKey ?? config('scrapingbee.api_key'); |
| 32 | + |
| 33 | + $this->baseUrl = config( |
| 34 | + 'scrapingbee.walmart_search_base_url', |
| 35 | + 'https://app.scrapingbee.com/api/v1/walmart/search' |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @throws ConnectionException |
| 41 | + */ |
| 42 | + public function get(): Response |
| 43 | + { |
| 44 | + $this->params['api_key'] = $this->apiKey; |
| 45 | + $response = Http::get($this->baseUrl, $this->params); |
| 46 | + $this->reset(); |
| 47 | + |
| 48 | + return $response; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * https://www.scrapingbee.com/documentation/walmart/#light_request_WalmartAPISearch |
| 53 | + */ |
| 54 | + public function lightRequest(bool $lightRequest = true): self |
| 55 | + { |
| 56 | + $this->params['light_request'] = $lightRequest; |
| 57 | + |
| 58 | + return $this; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * https://www.scrapingbee.com/documentation/walmart/#query |
| 63 | + */ |
| 64 | + public function query(string $query): self |
| 65 | + { |
| 66 | + $this->params['query'] = $query; |
| 67 | + |
| 68 | + return $this; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * https://www.scrapingbee.com/documentation/walmart/#page |
| 73 | + */ |
| 74 | + public function page(int $page): self |
| 75 | + { |
| 76 | + $this->params['page'] = $page; |
| 77 | + |
| 78 | + return $this; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * https://www.scrapingbee.com/documentation/walmart/#min_price |
| 83 | + */ |
| 84 | + public function minPrice(int $minPrice): self |
| 85 | + { |
| 86 | + $this->params['min_price'] = $minPrice; |
| 87 | + |
| 88 | + return $this; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * https://www.scrapingbee.com/documentation/walmart/#max_price |
| 93 | + */ |
| 94 | + public function maxPrice(int $maxPrice): self |
| 95 | + { |
| 96 | + $this->params['max_price'] = $maxPrice; |
| 97 | + |
| 98 | + return $this; |
| 99 | + } |
| 100 | + |
| 101 | + /* |
| 102 | + * https://www.scrapingbee.com/documentation/walmart/#sort_by |
| 103 | + */ |
| 104 | + public function sortBy(string $sortBy): self |
| 105 | + { |
| 106 | + $this->params['sort_by'] = $sortBy; |
| 107 | + |
| 108 | + return $this; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * https://www.scrapingbee.com/documentation/walmart/#device_WalmartAPISearch |
| 113 | + */ |
| 114 | + public function device(string $device): self |
| 115 | + { |
| 116 | + $this->params['device'] = $device; |
| 117 | + |
| 118 | + return $this; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * https://www.scrapingbee.com/documentation/walmart/#domain_WalmartAPISearch |
| 123 | + */ |
| 124 | + public function domain(string $domain): self |
| 125 | + { |
| 126 | + $this->params['domain'] = $domain; |
| 127 | + |
| 128 | + return $this; |
| 129 | + } |
| 130 | + |
| 131 | + /* |
| 132 | + * https://www.scrapingbee.com/documentation/walmart/#domain_WalmartAPISearch |
| 133 | + */ |
| 134 | + public function fulfillmentSpeed(string $fulfillmentSpeed): self |
| 135 | + { |
| 136 | + $this->params['fulfillment_speed'] = $fulfillmentSpeed; |
| 137 | + |
| 138 | + return $this; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * https://www.scrapingbee.com/documentation/walmart/#fulfillment_type |
| 143 | + */ |
| 144 | + public function fulfillmentType(string $fulfillmentType): self |
| 145 | + { |
| 146 | + $this->params['fulfillment_type'] = $fulfillmentType; |
| 147 | + |
| 148 | + return $this; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * https://www.scrapingbee.com/documentation/walmart/#delivery_zip |
| 153 | + */ |
| 154 | + public function deliveryZip(string $deliveryZip): self |
| 155 | + { |
| 156 | + $this->params['delivery_zip'] = $deliveryZip; |
| 157 | + |
| 158 | + return $this; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * https://www.scrapingbee.com/documentation/walmart/#store_id |
| 163 | + */ |
| 164 | + public function storeId(string $storeId): self |
| 165 | + { |
| 166 | + $this->params['store_id'] = $storeId; |
| 167 | + |
| 168 | + return $this; |
| 169 | + } |
| 170 | + |
| 171 | + public function addHtml(): self |
| 172 | + { |
| 173 | + $this->params['add_html'] = true; |
| 174 | + |
| 175 | + return $this; |
| 176 | + } |
| 177 | + |
| 178 | + |
| 179 | + /** |
| 180 | + * https://www.scrapingbee.com/documentation/google/#extra_params |
| 181 | + */ |
| 182 | + public function extraParams(array $extraParams): self |
| 183 | + { |
| 184 | + $this->params['extra_params'] = http_build_query($extraParams); |
| 185 | + |
| 186 | + return $this; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * https://www.scrapingbee.com/documentation/google/#add_html |
| 191 | + */ |
| 192 | + public function addHtml(): self |
| 193 | + { |
| 194 | + $this->params['add_html'] = true; |
| 195 | + |
| 196 | + return $this; |
| 197 | + } |
| 198 | + |
| 199 | + /* |
| 200 | + * If the API hasn't caught up and you need to support a new ScrapingBee parameter, |
| 201 | + * you can set it using this method. |
| 202 | + */ |
| 203 | + public function setParam(string $key, mixed $value): self |
| 204 | + { |
| 205 | + $this->params[$key] = $value; |
| 206 | + |
| 207 | + return $this; |
| 208 | + } |
| 209 | + |
| 210 | + private function reset(): self |
| 211 | + { |
| 212 | + $this->params = []; |
| 213 | + |
| 214 | + return $this; |
| 215 | + } |
| 216 | +} |
0 commit comments