Skip to content

Commit 266cc23

Browse files
committed
refactor: use httpx instead of requests
1 parent d269f27 commit 266cc23

File tree

7 files changed

+25
-23
lines changed

7 files changed

+25
-23
lines changed

canaille/app/forms.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import math
44
import re
55

6-
import requests
6+
import httpx
77
import wtforms.validators
88
from flask import abort
99
from flask import current_app
@@ -103,7 +103,7 @@ def compromised_password_validator(form, field):
103103
)
104104

105105
try:
106-
response = requests.api.get(api_url, timeout=10)
106+
response = httpx.get(api_url, timeout=10)
107107
except Exception:
108108
if not request_is_partial():
109109
current_app.logger.exception(

canaille/oidc/oauth.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import datetime
22
import json
33

4-
import requests
4+
import httpx
55
from authlib.integrations.flask_oauth2 import AuthorizationServer
66
from authlib.integrations.flask_oauth2 import ResourceProtector
77
from authlib.jose import JsonWebKey
@@ -209,7 +209,7 @@ def get_client_jwks(client, kid=None):
209209

210210
@cache.cached(timeout=50, key_prefix=f"jwks_{client.client_id}")
211211
def get_jwks():
212-
return requests.get(client.jwks_uri).json()
212+
return httpx.get(client.jwks_uri).json()
213213

214214
if client.jwks_uri:
215215
raw_jwks = get_jwks()

pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ dependencies = [
3535
"flask-caching>=2.3.0",
3636
"flask-wtf >= 1.2.1",
3737
"pydantic-settings >= 2.0.3",
38-
"requests>=2.32.3",
38+
"httpx>=0.28.1",
3939
"wtforms >= 3.1.1",
4040
]
4141

@@ -161,7 +161,7 @@ demo = [
161161
"faker",
162162
"honcho",
163163
"slapd",
164-
"requests",
164+
"httpx",
165165
"watchdog >= 4.0.0",
166166
]
167167
release = [

tests/app/test_forms.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ def __init__(self, data):
338338
password_too_long_validator(None, Field("a" * 4097))
339339

340340

341-
@mock.patch("requests.api.get")
341+
@mock.patch("httpx.get")
342342
def test_compromised_password_validator(api_get, testclient):
343343
current_app.config["CANAILLE"]["ENABLE_PASSWORD_COMPROMISSION_CHECK"] = True
344344

@@ -370,7 +370,7 @@ def __init__(self, data):
370370
assert compromised_password_validator(None, Field("password")) is None
371371

372372

373-
@mock.patch("requests.api.get")
373+
@mock.patch("httpx.get")
374374
def test_compromised_password_validator_with_failure_of_api_request_without_form_validation(
375375
api_get, testclient, logged_user, caplog
376376
):

tests/core/test_profile_settings.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def with_different_values(password, length, message):
166166
)
167167

168168

169-
@mock.patch("requests.api.get")
169+
@mock.patch("httpx.get")
170170
def test_profile_settings_compromised_password(api_get, testclient, logged_user):
171171
current_app.config["CANAILLE"]["ENABLE_PASSWORD_COMPROMISSION_CHECK"] = True
172172
"""Tests if password is compromised."""
@@ -204,7 +204,7 @@ def with_different_values(password, message):
204204
with_different_values("i'm a little pea", 'data-percent="100"')
205205

206206

207-
@mock.patch("requests.api.get")
207+
@mock.patch("httpx.get")
208208
def test_profile_settings_compromised_password_request_api_failed_but_password_updated(
209209
api_get, testclient, logged_user, backend, caplog
210210
):
@@ -237,7 +237,7 @@ def test_profile_settings_compromised_password_request_api_failed_but_password_u
237237
assert backend.check_user_password(logged_user, "123456789")[0]
238238

239239

240-
@mock.patch("requests.api.get")
240+
@mock.patch("httpx.get")
241241
def test_compromised_password_validator_with_failure_of_api_request_and_success_mail_to_admin_from_settings_form(
242242
api_get, testclient, backend, user, logged_user, caplog, smtpd
243243
):
@@ -269,7 +269,7 @@ def test_compromised_password_validator_with_failure_of_api_request_and_success_
269269
assert len(smtpd.messages) == 1
270270

271271

272-
@mock.patch("requests.api.get")
272+
@mock.patch("httpx.get")
273273
def test_compromised_password_validator_with_failure_of_api_request_and_fail_to_send_mail_to_admin_from_settings_form(
274274
api_get, testclient, backend, user, logged_user, caplog, smtpd
275275
):
@@ -303,7 +303,7 @@ def test_compromised_password_validator_with_failure_of_api_request_and_fail_to_
303303
assert len(smtpd.messages) == 0
304304

305305

306-
@mock.patch("requests.api.get")
306+
@mock.patch("httpx.get")
307307
def test_compromised_password_validator_with_failure_of_api_request_without_smtp_or_without_admin_email_from_settings_form(
308308
api_get, testclient, backend, user, logged_user, caplog
309309
):

tests/core/test_registration.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def test_registration_mail_error(SMTP, testclient, backend, smtpd, foo_group):
187187
assert len(smtpd.messages) == 0
188188

189189

190-
@mock.patch("requests.api.get")
190+
@mock.patch("httpx.get")
191191
def test_registration_with_compromised_password(api_get, testclient, backend):
192192
"""Tests a nominal registration with compromised password."""
193193
current_app.config["CANAILLE"]["ENABLE_PASSWORD_COMPROMISSION_CHECK"] = True
@@ -216,7 +216,7 @@ class Response:
216216
assert user is None
217217

218218

219-
@mock.patch("requests.api.get")
219+
@mock.patch("httpx.get")
220220
def test_registration_with_compromised_password_request_api_failed_but_account_created(
221221
api_get, testclient, backend, caplog
222222
):
@@ -252,7 +252,7 @@ def test_registration_with_compromised_password_request_api_failed_but_account_c
252252
backend.delete(user)
253253

254254

255-
@mock.patch("requests.api.get")
255+
@mock.patch("httpx.get")
256256
def test_compromised_password_validator_with_failure_of_api_request_and_success_mail_to_admin_from_register_form(
257257
api_get, testclient, backend, caplog, smtpd
258258
):
@@ -293,7 +293,7 @@ def test_compromised_password_validator_with_failure_of_api_request_and_success_
293293
backend.delete(user)
294294

295295

296-
@mock.patch("requests.api.get")
296+
@mock.patch("httpx.get")
297297
def test_compromised_password_validator_with_failure_of_api_request_and_fail_to_send_mail_to_admin_from_register_form(
298298
api_get, testclient, backend, caplog
299299
):
@@ -336,7 +336,7 @@ def test_compromised_password_validator_with_failure_of_api_request_and_fail_to_
336336
backend.delete(user)
337337

338338

339-
@mock.patch("requests.api.get")
339+
@mock.patch("httpx.get")
340340
def test_compromised_password_validator_with_failure_of_api_request_without_smtp_from_register_form(
341341
api_get, testclient, backend, caplog
342342
):
@@ -373,7 +373,7 @@ def test_compromised_password_validator_with_failure_of_api_request_without_smtp
373373
backend.delete(user)
374374

375375

376-
@mock.patch("requests.api.get")
376+
@mock.patch("httpx.get")
377377
def test_compromised_password_validator_with_failure_of_api_request_without_admin_email_from_register_form(
378378
api_get, testclient, backend, caplog
379379
):

uv.lock

+6-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)