|
| 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 LaravelScrapingBeeAmazonSearch |
| 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/amazon/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/amazon/#light_request_AmazonSearch |
| 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/amazon/#query_AmazonSearch |
| 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/amazon/#start_page |
| 73 | + */ |
| 74 | + public function startPage(int $page): self |
| 75 | + { |
| 76 | + $this->params['start_page'] = $page; |
| 77 | + |
| 78 | + return $this; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * https://www.scrapingbee.com/documentation/amazon/#pages |
| 83 | + */ |
| 84 | + public function pages(int $pages): self |
| 85 | + { |
| 86 | + $this->params['pages'] = $pages; |
| 87 | + |
| 88 | + return $this; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * https://www.scrapingbee.com/documentation/amazon/#sort_by |
| 93 | + */ |
| 94 | + public function sortBy(string $sortBy): self |
| 95 | + { |
| 96 | + $this->params['sort_by'] = $sortBy; |
| 97 | + |
| 98 | + return $this; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * https://www.scrapingbee.com/documentation/amazon/#device_AmazonSearch |
| 103 | + */ |
| 104 | + public function device(string $device): self |
| 105 | + { |
| 106 | + $this->params['device'] = $device; |
| 107 | + |
| 108 | + return $this; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * https://www.scrapingbee.com/documentation/amazon/#domain_AmazonSearch |
| 113 | + */ |
| 114 | + public function domain(string $domain): self |
| 115 | + { |
| 116 | + $this->params['domain'] = $domain; |
| 117 | + |
| 118 | + return $this; |
| 119 | + } |
| 120 | + |
| 121 | + /* |
| 122 | + * https://www.scrapingbee.com/documentation/amazon/#country_AmazonSearch |
| 123 | + */ |
| 124 | + public function country(string $isoCountryCode): self |
| 125 | + { |
| 126 | + $this->params['country'] = $isoCountryCode; |
| 127 | + |
| 128 | + return $this; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * https://www.scrapingbee.com/documentation/amazon/#zip_code_AmazonSearch |
| 133 | + */ |
| 134 | + public function zipCode(string $zipCode): self |
| 135 | + { |
| 136 | + $this->params['zip_code'] = $zipCode; |
| 137 | + |
| 138 | + return $this; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * https://www.scrapingbee.com/documentation/amazon/#language_AmazonSearch |
| 143 | + */ |
| 144 | + public function language(string $language): self |
| 145 | + { |
| 146 | + $this->params['language'] = $language; |
| 147 | + |
| 148 | + return $this; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * https://www.scrapingbee.com/documentation/amazon/#currency_AmazonSearch |
| 153 | + */ |
| 154 | + public function currency(string $currency): self |
| 155 | + { |
| 156 | + $this->params['currency'] = $currency; |
| 157 | + |
| 158 | + return $this; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * https://www.scrapingbee.com/documentation/amazon/#category_id |
| 163 | + */ |
| 164 | + public function categoryId(string $categoryId): self |
| 165 | + { |
| 166 | + $this->params['category_id'] = $categoryId; |
| 167 | + |
| 168 | + return $this; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * https://www.scrapingbee.com/documentation/amazon/#merchant_id |
| 173 | + */ |
| 174 | + public function merchantId(string $merchantId): self |
| 175 | + { |
| 176 | + $this->params['merchant_id'] = $merchantId; |
| 177 | + |
| 178 | + return $this; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * https://www.scrapingbee.com/documentation/amazon/#autoselect_variant |
| 183 | + */ |
| 184 | + public function autoSelectVariant(bool $autoSelectVariant = true): self |
| 185 | + { |
| 186 | + $this->params['autoselect_variant'] = $autoSelectVariant; |
| 187 | + |
| 188 | + return $this; |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * https://www.scrapingbee.com/documentation/amazon/#add_html_AmazonSearch |
| 193 | + */ |
| 194 | + public function addHtml(): self |
| 195 | + { |
| 196 | + $this->params['add_html'] = true; |
| 197 | + |
| 198 | + return $this; |
| 199 | + } |
| 200 | + |
| 201 | + /* |
| 202 | + * If the API hasn't caught up and you need to support a new ScrapingBee parameter, |
| 203 | + * you can set it using this method. |
| 204 | + */ |
| 205 | + public function setParam(string $key, mixed $value): self |
| 206 | + { |
| 207 | + $this->params[$key] = $value; |
| 208 | + |
| 209 | + return $this; |
| 210 | + } |
| 211 | + |
| 212 | + private function reset(): self |
| 213 | + { |
| 214 | + $this->params = []; |
| 215 | + |
| 216 | + return $this; |
| 217 | + } |
| 218 | +} |
0 commit comments