Makes the given request, with a number of retries if being rate limited. The request will be prepended with the `base_url` unless the request appears to be an absolute URL (i.e. starts with `http://` or `https://`). Parameters: request (str): the request to make
(self, request: str)
| 202 | self.read_timeout = read_timeout or DEFAULT_READ_TIMEOUT |
| 203 | |
| 204 | def get(self, request: str): |
| 205 | """Makes the given request, with a number of retries if being rate limited. The |
| 206 | request will be prepended with the `base_url` unless the request appears to be an |
| 207 | absolute URL (i.e. starts with `http://` or `https://`). |
| 208 | |
| 209 | Parameters: |
| 210 | request (str): the request to make against the base URL of this client. |
| 211 | |
| 212 | Returns: |
| 213 | response (requests.models.Response): the response from the server. |
| 214 | |
| 215 | Raises: |
| 216 | SystemExit: if there is no response from the server, or if the URL is invalid. |
| 217 | ResponseError: if the server does not respond with a non-429 status code within |
| 218 | the `MAX_RETRIES` attempts. |
| 219 | |
| 220 | """ |
| 221 | if urllib.parse.urlparse(request, allow_fragments=True).scheme: |
| 222 | self.last_request = request |
| 223 | else: |
| 224 | if request and not request.startswith("/"): |
| 225 | request = f"/{request}" |
| 226 | self.last_request = f"{self.base_url}{request}" |
| 227 | |
| 228 | status_code = None |
| 229 | retries = 0 |
| 230 | errors = [] |
| 231 | while retries < self.max_retries: |
| 232 | retries += 1 |
| 233 | try: |
| 234 | self.response = requests.get( |
| 235 | self.last_request, |
| 236 | headers=self.headers, |
| 237 | timeout=(self.timeout, self.read_timeout), |
| 238 | ) |
| 239 | |
| 240 | status_code = self.response.status_code |
| 241 | # If we hit a 429 Too Many Requests status, then try again in 1 second |
| 242 | if status_code != 429: |
| 243 | return self.response |
| 244 | |
| 245 | # If the connection times out, retry but cache the error |
| 246 | except requests.exceptions.ConnectionError as exc: |
| 247 | errors.append(str(exc)) |
| 248 | |
| 249 | # Read timeouts should prevent further retries |
| 250 | except requests.exceptions.ReadTimeout as exc: |
| 251 | raise ResponseError(str(exc)) from exc |
| 252 | |
| 253 | except requests.exceptions.MissingSchema: |
| 254 | sys.exit( |
| 255 | f"Unable to make request on {self.last_request}, did you mean http://{self.last_request}?" |
| 256 | ) |
| 257 | |
| 258 | # If the connection failed, or returned a 429, then wait 1 second before retrying |
| 259 | time.sleep(1) |
| 260 | |
| 261 | else: |