Makes a request to the API, and returns response as status code, content and JSON object.
(
self,
url: str,
*,
method: str = "POST",
data: Optional[dict] = None,
json: Optional[dict] = None,
stream: bool = False,
headers: Optional[dict] = None,
**kwargs,
)
| 112 | self.close() |
| 113 | |
| 114 | def _api_call( |
| 115 | self, |
| 116 | url: str, |
| 117 | *, |
| 118 | method: str = "POST", |
| 119 | data: Optional[dict] = None, |
| 120 | json: Optional[dict] = None, |
| 121 | stream: bool = False, |
| 122 | headers: Optional[dict] = None, |
| 123 | **kwargs, |
| 124 | ) -> Tuple[int, Union[str, requests.Response], Any]: |
| 125 | """ |
| 126 | Makes a request to the API, and returns response as status code, |
| 127 | content and JSON object. |
| 128 | """ |
| 129 | if data is not None and json is not None: |
| 130 | raise ValueError("cannot accept both json and data") |
| 131 | |
| 132 | if data is None: |
| 133 | data = {} |
| 134 | url = urllib.parse.urljoin(self._server_url, url) |
| 135 | |
| 136 | util.log_info("Request to DeepL API", method=method, url=url) |
| 137 | util.log_debug("Request details", data=data, json=json) |
| 138 | |
| 139 | if headers is None: |
| 140 | headers = dict() |
| 141 | headers.update( |
| 142 | {k: v for k, v in self.headers.items() if k not in headers} |
| 143 | ) |
| 144 | |
| 145 | status_code, content = self._client.request_with_backoff( |
| 146 | method, |
| 147 | url, |
| 148 | data=data, |
| 149 | json=json, |
| 150 | stream=stream, |
| 151 | headers=headers, |
| 152 | **kwargs, |
| 153 | ) |
| 154 | |
| 155 | json = None |
| 156 | if isinstance(content, str): |
| 157 | content_str = content |
| 158 | try: |
| 159 | json = json_module.loads(content) |
| 160 | except json_module.JSONDecodeError: |
| 161 | pass |
| 162 | else: |
| 163 | content_str = content.text |
| 164 | |
| 165 | util.log_info("DeepL API response", url=url, status_code=status_code) |
| 166 | util.log_debug("Response details", content=content_str) |
| 167 | |
| 168 | return status_code, content, json |
| 169 | |
| 170 | def _raise_for_status( |
| 171 | self, |
no test coverage detected