Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions tests/unit/http/test_async_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,23 @@ async def test_request_called_with_basic_auth(self):
)

self.session_mock.request.assert_called()
auth = self.session_mock.request.call_args.kwargs["auth"]
self.assertIsNotNone(auth)
self.assertEqual(auth.login, "account_sid")
self.assertEqual(auth.password, "auth_token")
kwargs = self.session_mock.request.call_args.kwargs
try:
from aiohttp import encode_basic_auth
except ImportError:
encode_basic_auth = None

if encode_basic_auth is not None:
self.assertEqual(
kwargs["headers"]["Authorization"],
encode_basic_auth("account_sid", "auth_token"),
)
self.assertIsNone(kwargs["auth"])
else:
auth = kwargs["auth"]
self.assertIsNotNone(auth)
self.assertEqual(auth.login, "account_sid")
self.assertEqual(auth.password, "auth_token")

async def test_invalid_request_timeout_raises_exception(self):
with self.assertRaises(ValueError):
Expand Down
22 changes: 19 additions & 3 deletions twilio/http/async_http_client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import logging
from typing import Dict, Optional, Tuple

from aiohttp import BasicAuth, ClientSession
from aiohttp import ClientSession
from aiohttp_retry import ExponentialRetry, RetryClient

try:
from aiohttp import encode_basic_auth
except ImportError:
encode_basic_auth = None # type: ignore[misc, assignment]

if encode_basic_auth is None:
from aiohttp import BasicAuth

from twilio.http import AsyncHttpClient
from twilio.http.request import Request as TwilioRequest
from twilio.http.response import Response
Expand Down Expand Up @@ -79,16 +87,24 @@ async def request(
if timeout is not None and timeout <= 0:
raise ValueError(timeout)

request_headers = dict(headers) if headers is not None else None
basic_auth = None
if auth is not None:
basic_auth = BasicAuth(login=auth[0], password=auth[1])
if encode_basic_auth is not None:
if request_headers is None:
request_headers = {}
request_headers["Authorization"] = encode_basic_auth(
auth[0], auth[1]
)
else:
basic_auth = BasicAuth(login=auth[0], password=auth[1])

kwargs = {
"method": method.upper(),
"url": url,
"params": params,
"data": data,
"headers": headers,
"headers": request_headers,
"auth": basic_auth,
"timeout": timeout,
"allow_redirects": allow_redirects,
Expand Down