Skip to content

Commit b79a2da

Browse files
committed
feat: change http client to Niquests
1 parent c020d05 commit b79a2da

23 files changed

+1255
-1015
lines changed

.github/workflows/check.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ jobs:
2020
matrix:
2121
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12', '3.13']
2222
steps:
23-
- uses: actions/checkout@v2
23+
- uses: actions/checkout@v4
2424
with:
2525
submodules: 'recursive'
2626
- name: Set up Python ${{ matrix.python-version }}
27-
uses: actions/setup-python@v2
27+
uses: actions/setup-python@v5
2828
with:
2929
python-version: ${{ matrix.python-version }}
3030
- name: Setup poetry
31-
uses: abatilo/actions-poetry@v2.0.0
31+
uses: abatilo/actions-poetry@v4
3232
with:
33-
poetry-version: 1.3.2
33+
poetry-version: 2.1.2
3434
- name: Install dependencies
3535
run: poetry install -E crypto
3636
- name: Generate rest sync code and tests

.github/workflows/lint.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ jobs:
1010
lint:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v2
13+
- uses: actions/checkout@v4
1414
with:
1515
submodules: 'recursive'
1616
- name: Set up Python ${{ matrix.python-version }}
17-
uses: actions/setup-python@v2
17+
uses: actions/setup-python@v5
1818
with:
1919
python-version: '3.8'
2020
- name: Setup poetry
21-
uses: abatilo/actions-poetry@v2.0.0
21+
uses: abatilo/actions-poetry@v4
2222
with:
23-
poetry-version: 1.3.2
23+
poetry-version: 2.1.2
2424
- name: Install dependencies
2525
run: poetry install -E crypto
2626
- name: Lint with flake8

ably/http/http.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import json
55
from urllib.parse import urljoin
66

7-
import httpx
7+
import niquests
88
import msgpack
99

1010
from ably.rest.auth import Auth
@@ -86,10 +86,10 @@ def version(self):
8686

8787
class Response:
8888
"""
89-
Composition for httpx.Response with delegation
89+
Composition for niquests.Response with delegation
9090
"""
9191

92-
def __init__(self, response):
92+
def __init__(self, response: niquests.Response):
9393
self.__response = response
9494

9595
def to_native(self):
@@ -129,10 +129,10 @@ def __init__(self, ably, options):
129129
# Cached fallback host (RSC15f)
130130
self.__host = None
131131
self.__host_expires = None
132-
self.__client = httpx.AsyncClient(http2=True)
132+
self.__client = niquests.AsyncSession()
133133

134134
async def close(self):
135-
await self.__client.aclose()
135+
await self.__client.close()
136136

137137
def dump_body(self, body):
138138
if self.options.use_binary_protocol:
@@ -196,18 +196,24 @@ def should_stop_retrying():
196196
base_url = "%s://%s:%d" % (self.preferred_scheme,
197197
host,
198198
self.preferred_port)
199+
200+
# remove redundant port in base_url
201+
if self.preferred_scheme == "https" and self.preferred_port == 443:
202+
base_url = base_url.replace(":443", "")
203+
elif self.preferred_scheme == "http" and self.preferred_port == 80:
204+
base_url = base_url.replace(":80", "")
205+
199206
url = urljoin(base_url, path)
200207

201-
request = self.__client.build_request(
202-
method=method,
203-
url=url,
204-
content=body,
205-
params=params,
206-
headers=all_headers,
207-
timeout=timeout,
208-
)
209208
try:
210-
response = await self.__client.send(request)
209+
response = await self.__client.request(
210+
method=method,
211+
url=url,
212+
data=body,
213+
params=params,
214+
headers=all_headers,
215+
timeout=timeout,
216+
)
211217
except Exception as e:
212218
if should_stop_retrying():
213219
raise e

ably/realtime/connectionmanager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22
import logging
33
import asyncio
4-
import httpx
4+
import niquests
55
from ably.transport.websockettransport import WebSocketTransport, ProtocolMessageAction
66
from ably.transport.defaults import Defaults
77
from ably.types.connectionerrors import ConnectionErrors
@@ -52,10 +52,10 @@ def enact_state_change(self, state: ConnectionState, reason: Optional[AblyExcept
5252

5353
def check_connection(self) -> bool:
5454
try:
55-
response = httpx.get(self.options.connectivity_check_url)
55+
response = niquests.get(self.options.connectivity_check_url)
5656
return 200 <= response.status_code < 300 and \
5757
(self.options.connectivity_check_url != Defaults.connectivity_check_url or "yes" in response.text)
58-
except httpx.HTTPError:
58+
except niquests.HTTPError:
5959
return False
6060

6161
def get_state_error(self) -> AblyException:

ably/rest/auth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import time
66
from typing import Optional, TYPE_CHECKING, Union
77
import uuid
8-
import httpx
8+
import niquests
99

1010
from ably.types.options import Options
1111
if TYPE_CHECKING:
@@ -400,7 +400,7 @@ async def token_request_from_auth_url(self, method: str, url: str, token_params,
400400
body = dict(auth_params, **token_params)
401401

402402
from ably.http.http import Response
403-
async with httpx.AsyncClient(http2=True) as client:
403+
async with niquests.AsyncSession() as client:
404404
resp = await client.request(method=method, url=url, headers=headers, params=params, data=body)
405405
response = Response(resp)
406406

ably/scripts/unasync.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ def find_files(dir_path, file_name_regex):
237237
def run():
238238
# Source files ==========================================
239239

240-
_TOKEN_REPLACE["AsyncClient"] = "Client"
241-
_TOKEN_REPLACE["aclose"] = "close"
240+
_TOKEN_REPLACE["AsyncSession"] = "Session"
241+
_TOKEN_REPLACE["close"] = "close"
242242

243243
_IMPORTS_REPLACE["ably"] = "ably.sync"
244244

@@ -271,7 +271,7 @@ def run():
271271
_STRING_REPLACE['ably.rest.auth.Auth.request_token'] = 'ably.sync.rest.auth.AuthSync.request_token'
272272
_STRING_REPLACE['ably.rest.auth.TokenRequest'] = 'ably.sync.rest.auth.TokenRequest'
273273
_STRING_REPLACE['ably.rest.rest.Http.post'] = 'ably.sync.rest.rest.HttpSync.post'
274-
_STRING_REPLACE['httpx.AsyncClient.send'] = 'httpx.Client.send'
274+
_STRING_REPLACE['niquests.AsyncSession.send'] = 'niquests.Session.send'
275275
_STRING_REPLACE['ably.util.exceptions.AblyException.raise_for_response'] = \
276276
'ably.sync.util.exceptions.AblyException.raise_for_response'
277277
_STRING_REPLACE['ably.rest.rest.AblyRest.time'] = 'ably.sync.rest.rest.AblyRestSync.time'

ably/util/crypto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def get_default_params(params=None):
153153
if not key:
154154
raise ValueError("Crypto.get_default_params: a key is required")
155155

156-
if type(key) == str:
156+
if isinstance(key, str):
157157
key = base64.b64decode(key)
158158

159159
cipher_params = CipherParams(algorithm=algorithm, secret_key=key, iv=iv, mode=mode)

0 commit comments

Comments
 (0)