MCPcopy Create free account
hub / github.com/DeepL/deepl-python / request_with_backoff

Method request_with_backoff

deepl/http_client.py:95–142  ·  view source on GitHub ↗

Makes API request, retrying if necessary, and returns response. Return and exceptions are the same as function request().

(
        self,
        method: str,
        url: str,
        data: Optional[dict],
        json: Optional[dict],
        headers: dict,
        stream: bool = False,
        **kwargs,
    )

Source from the content-addressed store, hash-verified

93 self._session.close()
94
95 def request_with_backoff(
96 self,
97 method: str,
98 url: str,
99 data: Optional[dict],
100 json: Optional[dict],
101 headers: dict,
102 stream: bool = False,
103 **kwargs,
104 ) -> Tuple[int, Union[str, requests.Response]]:
105 """Makes API request, retrying if necessary, and returns response.
106
107 Return and exceptions are the same as function request()."""
108 backoff = _BackoffTimer()
109 request = self._prepare_request(
110 method, url, data, json, headers, **kwargs
111 )
112
113 while True:
114 response: Optional[Tuple[int, Union[str, requests.Response]]]
115 try:
116 response = self._internal_request(
117 request, stream=stream, timeout=backoff.get_timeout()
118 )
119 exception = None
120 except Exception as e:
121 response = None
122 exception = e
123
124 if not self._should_retry(
125 response, exception, backoff.get_num_retries()
126 ):
127 if response is not None:
128 return response
129 else:
130 raise exception # type: ignore[misc]
131
132 if exception is not None:
133 log_info(
134 f"Encountered a retryable-exception: {str(exception)}"
135 )
136
137 log_info(
138 f"Starting retry {backoff.get_num_retries() + 1} for request "
139 f"{method} {url} after sleeping for "
140 f"{backoff.get_time_until_deadline():.2f} seconds."
141 )
142 backoff.sleep_until_deadline()
143
144 def request(
145 self,

Callers 1

_api_callMethod · 0.80

Calls 9

_prepare_requestMethod · 0.95
_internal_requestMethod · 0.95
_should_retryMethod · 0.95
_BackoffTimerClass · 0.85
log_infoFunction · 0.85
get_timeoutMethod · 0.80
get_num_retriesMethod · 0.80
sleep_until_deadlineMethod · 0.80

Tested by

no test coverage detected