MCPcopy Create free account
hub / github.com/Materials-Consortia/optimade-python-tools / get

Method get

optimade/validator/utils.py:204–266  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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:

Callers 15

insert_from_jsonlFunction · 0.45
get_providersFunction · 0.45
get_child_database_linksFunction · 0.45
propertyMethod · 0.45
handle_not_andMethod · 0.45
__init__Method · 0.45
convertMethod · 0.45
from_urlMethod · 0.45
get_pdbx_mmcifFunction · 0.45
get_pdbFunction · 0.45

Calls 1

ResponseErrorClass · 0.85

Tested by 15

test_subappsFunction · 0.36
test_client_endpointsFunction · 0.36
test_versioned_base_urlsFunction · 0.36
innerFunction · 0.36
test_int_ascFunction · 0.36
test_int_descFunction · 0.36
test_datetime_ascFunction · 0.36
test_datetime_descFunction · 0.36