Skip to content

Commit 4a4872c

Browse files
author
Sebastian Molenda
committed
Rework invocation of requests
1 parent defccec commit 4a4872c

36 files changed

+390
-2572
lines changed

.pubnub.yml

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@ sdks:
6161
- x86
6262
- x86-64
6363
requires:
64-
- name: requests
65-
min-version: "2.4"
66-
location: https://pypi.org/project/requests/
67-
license: Apache Software License (Apache 2.0)
68-
license-url: https://github.com/psf/requests/blob/master/LICENSE
69-
is-required: Required
7064
- name: pycryptodomex
7165
min-version: "3.3"
7266
location: https://pypi.org/project/pycryptodomex/
@@ -79,11 +73,11 @@ sdks:
7973
license: MIT License (MIT)
8074
license-url: https://github.com/agronholm/cbor2/blob/master/LICENSE.txt
8175
is-required: Required
82-
- name: aiohttp
83-
min-version: "2.3.10"
84-
location: https://pypi.org/project/aiohttp/
85-
license: Apache Software License (Apache 2)
86-
license-url: https://github.com/aio-libs/aiohttp/blob/master/LICENSE.txt
76+
- name: httpx
77+
min-version: "0.28.0"
78+
location: https://pypi.org/project/httpx/
79+
license: BSD License (BSD-3-Clause)
80+
license-url: https://github.com/encode/httpx/blob/master/LICENSE.md
8781
is-required: Required
8882
-
8983
language: python
@@ -162,11 +156,11 @@ sdks:
162156
license-url: https://github.com/agronholm/cbor2/blob/master/LICENSE.txt
163157
is-required: Required
164158
-
165-
name: aiohttp
166-
min-version: "2.3.10"
167-
location: https://pypi.org/project/aiohttp/
168-
license: Apache Software License (Apache 2)
169-
license-url: https://github.com/aio-libs/aiohttp/blob/master/LICENSE.txt
159+
name: httpx
160+
min-version: "0.28.0"
161+
location: https://pypi.org/project/httpx/
162+
license: BSD License (BSD-3-Clause)
163+
license-url: https://github.com/encode/httpx/blob/master/LICENSE.md
170164
is-required: Required
171165
changelog:
172166
- date: 2024-11-19

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ You will need the publish and subscribe keys to authenticate your app. Get your
1818
## Configure PubNub
1919

2020
1. Integrate the Python SDK into your project using `pip`:
21-
21+
2222
```bash
2323
pip install pubnub
2424
```
@@ -83,9 +83,8 @@ pubnub.subscribe().channels('my_channel').execute()
8383
8484
## Documentation
8585
86-
* [Build your first realtime Python app with PubNub](https://www.pubnub.com/docs/platform/quickstarts/python)
87-
* [API reference for Python](https://www.pubnub.com/docs/python/pubnub-python-sdk)
88-
* [API reference for Python (asyncio)](https://www.pubnub.com/docs/python-aiohttp/pubnub-python-sdk)
86+
* [Build your first realtime Python app with PubNub](https://www.pubnub.com/docs/general/basics/set-up-your-account)
87+
* [API reference for Python](https://www.pubnub.com/docs/sdks/python)
8988
9089
## Support
9190

pubnub/crypto.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ def decrypt(self, key, file, use_random_iv=True):
104104
cipher = AES.new(bytes(secret[0:32], "utf-8"), self.mode, initialization_vector)
105105
result = unpad(cipher.decrypt(extracted_file), 16)
106106
except ValueError:
107+
if not self.fallback_mode: # No fallback mode so we return the original content
108+
return file
107109
cipher = AES.new(bytes(secret[0:32], "utf-8"), self.fallback_mode, initialization_vector)
108110
result = unpad(cipher.decrypt(extracted_file), 16)
109111

pubnub/endpoints/file_operations/download_file.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pubnub.request_handlers.requests_handler import RequestsRequestHandler
66
from pubnub.endpoints.file_operations.get_file_url import GetFileDownloadUrl
77
from warnings import warn
8+
from urllib.parse import urlparse, parse_qs
89

910

1011
class DownloadFileNative(FileOperationEndpoint):
@@ -69,7 +70,8 @@ def use_base_path(self):
6970
return False
7071

7172
def build_params_callback(self):
72-
return lambda a: {}
73+
params = parse_qs(urlparse(self._download_data.result.file_url).query)
74+
return lambda a: {key: str(params[key][0]) for key in params.keys()}
7375

7476
def name(self):
7577
return "Downloading file"

pubnub/endpoints/file_operations/send_file_asyncio.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,25 @@
44

55

66
class AsyncioSendFile(SendFileNative):
7-
def options(self):
8-
request_options = super(SendFileNative, self).options()
9-
request_options.data = request_options.files
10-
return request_options
11-
127
async def future(self):
13-
self._file_upload_envelope = await FetchFileUploadS3Data(self._pubnub).\
14-
channel(self._channel).\
15-
file_name(self._file_name).future()
8+
self._file_upload_envelope = await FetchFileUploadS3Data(self._pubnub) \
9+
.channel(self._channel) \
10+
.file_name(self._file_name).future()
1611

1712
response_envelope = await super(SendFileNative, self).future()
1813

19-
publish_file_response = await PublishFileMessage(self._pubnub).\
20-
channel(self._channel).\
21-
meta(self._meta).\
22-
message(self._message).\
23-
file_id(response_envelope.result.file_id).\
24-
file_name(response_envelope.result.name).\
25-
should_store(self._should_store).\
26-
ttl(self._ttl).\
27-
cipher_key(self._cipher_key).future()
14+
publish_file_response = await PublishFileMessage(self._pubnub) \
15+
.channel(self._channel) \
16+
.meta(self._meta) \
17+
.message(self._message) \
18+
.file_id(response_envelope.result.file_id) \
19+
.file_name(response_envelope.result.name) \
20+
.should_store(self._should_store) \
21+
.ttl(self._ttl) \
22+
.replicate(self._replicate) \
23+
.ptto(self._ptto) \
24+
.custom_message_type(self._custom_message_type) \
25+
.cipher_key(self._cipher_key).future()
2826

2927
response_envelope.result.timestamp = publish_file_response.result.timestamp
3028
return response_envelope

pubnub/pubnub_asyncio.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import urllib
88

99
from asyncio import Event, Queue, Semaphore
10-
from yarl import URL
1110
from httpx import AsyncHTTPTransport
1211
from pubnub.event_engine.containers import PresenceStateContainer
1312
from pubnub.event_engine.models import events, states
@@ -180,26 +179,32 @@ async def _request_helper(self, options_func, cancellation_event):
180179
else:
181180
url = utils.build_url(scheme="", origin="", path=options.path, params=options.query_string)
182181

183-
url = str(URL(url, encoded=True))
182+
full_url = httpx.URL(url, query=options.query_string.encode('utf-8'))
183+
184184
logger.debug("%s %s %s" % (options.method_string, url, options.data))
185185

186186
if options.request_headers:
187187
request_headers = {**self.headers, **options.request_headers}
188188
else:
189189
request_headers = self.headers
190190

191+
request_arguments = {
192+
'method': options.method_string,
193+
'headers': request_headers,
194+
'url': full_url,
195+
'follow_redirects': options.allow_redirects,
196+
'timeout': (options.connect_timeout, options.request_timeout),
197+
}
198+
if options.is_post() or options.is_patch():
199+
request_arguments['content'] = options.data
200+
request_arguments['files'] = options.files
201+
191202
try:
192203
if not self._session:
193204
await self.create_session()
194205
start_timestamp = time.time()
195206
response = await asyncio.wait_for(
196-
self._session.request(
197-
options.method_string,
198-
url,
199-
headers=request_headers,
200-
data=options.data if options.data else None,
201-
follow_redirects=options.allow_redirects
202-
),
207+
self._session.request(**request_arguments),
203208
options.request_timeout
204209
)
205210
except (asyncio.TimeoutError, asyncio.CancelledError):

pubnub/pubnub_core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def publish(self, channel: str = None, message: any = None, should_store: Option
225225

226226
def grant(self):
227227
""" Deprecated. Use grant_token instead """
228-
warn("This method will stop working on 31th December 2024. We recommend that you use grant_token() instead.",
228+
warn("Access management v2 is being deprecated. We recommend switching to grant_token().",
229229
DeprecationWarning, stacklevel=2)
230230
return Grant(self)
231231

@@ -240,7 +240,7 @@ def revoke_token(self, token: str) -> RevokeToken:
240240

241241
def audit(self):
242242
""" Deprecated """
243-
warn("This method will stop working on 31th December 2024.", DeprecationWarning, stacklevel=2)
243+
warn("Access management v2 is being deprecated.", DeprecationWarning, stacklevel=2)
244244
return Audit(self)
245245

246246
# Push Related methods

pubnub/request_handlers/requests_handler.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import logging
22
import threading
3-
import requests
43
import httpx
54
import json # noqa # pylint: disable=W0611
65
import urllib
@@ -237,14 +236,13 @@ def _invoke_request(self, p_options, e_options, base_origin):
237236
args = {
238237
"method": e_options.method_string,
239238
"headers": request_headers,
240-
"url": url,
241-
"params": e_options.query_string,
239+
"url": httpx.URL(url, query=e_options.query_string.encode("utf-8")),
242240
"timeout": (e_options.connect_timeout, e_options.request_timeout),
243241
"follow_redirects": e_options.allow_redirects
244242
}
245243

246244
if e_options.is_post() or e_options.is_patch():
247-
args["data"] = e_options.data
245+
args["content"] = e_options.data
248246
args["files"] = e_options.files
249247
logger.debug("%s %s %s" % (
250248
e_options.method_string,
@@ -265,32 +263,33 @@ def _invoke_request(self, p_options, e_options, base_origin):
265263
try:
266264
res = self.session.request(**args)
267265
logger.debug("GOT %s" % res.text)
268-
except requests.exceptions.ConnectionError as e:
266+
267+
except httpx.ConnectError as e:
269268
raise PubNubException(
270269
pn_error=PNERR_CONNECTION_ERROR,
271270
errormsg=str(e)
272271
)
273-
except requests.exceptions.HTTPError as e:
274-
raise PubNubException(
275-
pn_error=PNERR_HTTP_ERROR,
276-
errormsg=str(e)
277-
)
278-
except requests.exceptions.Timeout as e:
272+
except httpx.TimeoutException as e:
279273
raise PubNubException(
280274
pn_error=PNERR_CLIENT_TIMEOUT,
281275
errormsg=str(e)
282276
)
283-
except requests.exceptions.TooManyRedirects as e:
277+
except httpx.TooManyRedirects as e:
284278
raise PubNubException(
285279
pn_error=PNERR_TOO_MANY_REDIRECTS_ERROR,
286280
errormsg=str(e)
287281
)
282+
except httpx.HTTPStatusError as e:
283+
raise PubNubException(
284+
pn_error=PNERR_HTTP_ERROR,
285+
errormsg=str(e),
286+
status_code=e.response.status_code
287+
)
288288
except Exception as e:
289289
raise PubNubException(
290290
pn_error=PNERR_UNKNOWN_ERROR,
291291
errormsg=str(e)
292292
)
293-
294293
return res
295294

296295

setup.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
'Programming Language :: Python :: 3.8',
2222
'Programming Language :: Python :: 3.9',
2323
'Programming Language :: Python :: 3.10',
24+
'Programming Language :: Python :: 3.11',
25+
'Programming Language :: Python :: 3.12',
26+
'Programming Language :: Python :: 3.13',
2427
'Programming Language :: Python :: Implementation :: CPython',
2528
'License :: Other/Proprietary License',
2629
'Operating System :: OS Independent',
@@ -30,10 +33,8 @@
3033
python_requires='>=3.7',
3134
install_requires=[
3235
'pycryptodomex>=3.3',
33-
'requests>=2.4',
34-
'httpx>=0.18',
35-
'cbor2',
36-
'aiohttp'
36+
'httpx>=0.28',
37+
'cbor2>=5.6'
3738
],
3839
zip_safe=False,
3940
)

tests/helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ def pnconf_pam_copy():
196196
return deepcopy(pnconf_pam)
197197

198198

199-
def pnconf_pam_stub_copy():
200-
return deepcopy(pnconf_pam_stub)
199+
def pnconf_pam_stub_copy(**kwargs):
200+
return copy_and_update(pnconf_pam_stub, **kwargs)
201201

202202

203203
def pnconf_pam_acceptance_copy():

0 commit comments

Comments
 (0)