Skip to content

Commit bc80bb5

Browse files
rafaelmardojaimufeedali
authored andcommitted
refactor(providers/soup): Cleanup request API
- Thet get method doesn't need a `form` arg - Make the `json` arg more clear renaming it to `return_json`
1 parent cc2fb91 commit bc80bb5

File tree

7 files changed

+24
-30
lines changed

7 files changed

+24
-30
lines changed

dialect/providers/modules/bing.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def translate_url(self):
5353
return self.format_url("www.bing.com", "/ttranslatev3", params)
5454

5555
async def init_trans(self):
56-
response = await self.get(self.html_url, self._headers, check_common=False, json=False)
56+
response = await self.get(self.html_url, self._headers, check_common=False, return_json=False)
5757

5858
if response:
5959
try:
@@ -125,11 +125,13 @@ async def translate(self, request):
125125
detected=detected,
126126
pronunciation=TranslationPronunciation(None, pronunciation),
127127
)
128+
else:
129+
raise UnexpectedError("Unexpected translation response")
128130

129131
except Exception as exc:
130132
raise UnexpectedError from exc
131133

132-
def check_known_errors(self, _status, data):
134+
def check_known_errors(self, status, data):
133135
if not data:
134136
raise UnexpectedError("Response is empty!")
135137

dialect/providers/modules/deepl.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ def headers(self):
6565

6666
async def init_trans(self):
6767
# Get languages
68-
src_langs = await self.get(self.source_lang_url, headers=self.headers)
69-
dest_langs = await self.get(self.target_lang_url, headers=self.headers)
68+
src_langs = await self.get(self.source_lang_url, self.headers)
69+
dest_langs = await self.get(self.target_lang_url, self.headers)
7070

7171
if src_langs and dest_langs and isinstance(src_langs, list) and isinstance(dest_langs, list):
7272
for lang in src_langs:
@@ -80,7 +80,7 @@ async def validate_api_key(self, key):
8080
headers = {"Authorization": f"DeepL-Auth-Key {key}"}
8181

8282
try:
83-
await self.get(url, headers=headers)
83+
await self.get(url, headers)
8484
return True
8585
except (APIKeyInvalid, APIKeyRequired):
8686
return False
@@ -111,7 +111,7 @@ async def translate(self, request):
111111
raise UnexpectedError
112112

113113
async def api_char_usage(self):
114-
response = await self.get(self.usage_url, headers=self.headers)
114+
response = await self.get(self.usage_url, self.headers)
115115

116116
try:
117117
usage = response.get("character_count")

dialect/providers/modules/google.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ async def init_trans(self):
253253
langs_url = self.format_url(
254254
self._get_translate_host(".com"), "/translate_a/l", {"client": "t", "alpha": "true"}
255255
)
256-
response = await self.get(langs_url, self._headers, check_common=False)
256+
response = await self.get(langs_url, self._headers, False)
257257

258258
try:
259259
for code, name in response["tl"].items():
@@ -447,9 +447,3 @@ def __init__(self, text: str, candidates: list[str]):
447447

448448
def __str__(self):
449449
return self.text
450-
451-
def __dict__(self):
452-
return {
453-
"text": self.text,
454-
"candidates": self.candidates,
455-
}

dialect/providers/modules/libretrans.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ async def suggest(self, text, src, dest, suggestion):
143143
except: # noqa
144144
return False
145145

146-
def check_known_errors(self, _status, data):
146+
def check_known_errors(self, status, data):
147147
if not data:
148148
raise UnexpectedError("Response is empty!")
149149

dialect/providers/modules/lingva.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ async def speech(self, text, language):
121121
file.close()
122122
raise UnexpectedError from exc
123123

124-
def check_known_errors(self, _status, data):
124+
def check_known_errors(self, status, data):
125125
"""Raises a proper Exception if an error is found in the data."""
126126
if not data:
127127
raise UnexpectedError("Response is empty!")

dialect/providers/modules/yandex.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async def init_trans(self):
5050
# Get Yandex Translate web HTML to parse languages
5151
# Using `/api/v1/tr.json/getLangs` doesn't provide all the languages that Yandex supports
5252
html_url = self.format_url("translate.yandex.com")
53-
response = await self.get(html_url, check_common=False, json=False)
53+
response = await self.get(html_url, check_common=False, return_json=False)
5454

5555
if response:
5656
try:

dialect/providers/soup.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ async def send_and_read_and_process(
115115
self,
116116
message: Soup.Message,
117117
check_common: bool = True,
118-
json: bool = True,
118+
return_json: bool = True,
119119
) -> Any:
120120
"""
121121
Helper mixing ``SoupProvider.send_and_read``, ``SoupProvider.send_and_read_json``
@@ -126,14 +126,14 @@ async def send_and_read_and_process(
126126
Args:
127127
message: Message to send.
128128
check_common: If response data should be checked for errors using check_known_errors.
129-
json: If data should be processed as JSON.
129+
return_json: If the response should be parsed as JSON.
130130
131131
Returns:
132132
The JSON deserialized to a python object or bytes if ``json`` is ``False``.
133133
"""
134134

135135
try:
136-
if json:
136+
if return_json:
137137
response = await self.send_and_read_json(message)
138138
else:
139139
response = await self.send_and_read(message)
@@ -153,7 +153,7 @@ async def request(
153153
headers: dict = {},
154154
form: bool = False,
155155
check_common: bool = True,
156-
json: bool = True,
156+
return_json: bool = True,
157157
) -> Any:
158158
"""
159159
Helper for regular HTTP request.
@@ -165,36 +165,34 @@ async def request(
165165
headers: HTTP headers of the message.
166166
form: If the data should be encoded as a form.
167167
check_common: If response data should be checked for errors using check_known_errors.
168-
json: If data should be processed as JSON.
168+
return_json: If the response should be parsed as JSON.
169169
170170
Returns:
171171
The JSON deserialized to a python object or bytes if ``json`` is ``False``.
172172
"""
173173
message = self.create_message(method, url, data, headers, form)
174-
return await self.send_and_read_and_process(message, check_common, json)
174+
return await self.send_and_read_and_process(message, check_common, return_json)
175175

176176
async def get(
177177
self,
178178
url: str,
179179
headers: dict = {},
180-
form: bool = False,
181180
check_common: bool = True,
182-
json: bool = True,
181+
return_json: bool = True,
183182
) -> Any:
184183
"""
185184
Helper for GET HTTP request.
186185
187186
Args:
188187
url: Url of the request.
189188
headers: HTTP headers of the message.
190-
form: If the data should be encoded as a form.
191189
check_common: If response data should be checked for errors using check_known_errors.
192-
json: If data should be processed as JSON.
190+
return_json: If the response should be parsed as JSON.
193191
194192
Returns:
195193
The JSON deserialized to a python object or bytes if ``json`` is ``False``.
196194
"""
197-
return await self.request("GET", url, headers=headers, form=form, check_common=check_common, json=json)
195+
return await self.request("GET", url, headers=headers, check_common=check_common, return_json=return_json)
198196

199197
async def post(
200198
self,
@@ -203,7 +201,7 @@ async def post(
203201
headers: dict = {},
204202
form: bool = False,
205203
check_common: bool = True,
206-
json: bool = True,
204+
return_json: bool = True,
207205
) -> Any:
208206
"""
209207
Helper for POST HTTP request.
@@ -214,9 +212,9 @@ async def post(
214212
headers: HTTP headers of the message.
215213
form: If the data should be encoded as a form.
216214
check_common: If response data should be checked for errors using check_known_errors.
217-
json: If data should be processed as JSON.
215+
return_json: If the response should be parsed as JSON.
218216
219217
Returns:
220218
The JSON deserialized to a python object or bytes if ``json`` is ``False``.
221219
"""
222-
return await self.request("POST", url, data, headers, form, check_common, json)
220+
return await self.request("POST", url, data, headers, form, check_common, return_json)

0 commit comments

Comments
 (0)