Skip to content

Commit e4646a5

Browse files
committed
cleanup, lint, format
1 parent 77f8b95 commit e4646a5

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

adafruit_requests.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
from adafruit_connection_manager import get_connection_manager
4848

4949
if not sys.implementation.name == "circuitpython":
50-
from io import FileIO
5150
from types import TracebackType
5251
from typing import Any, Dict, Optional, Type
5352

@@ -345,6 +344,14 @@ def iter_content(self, chunk_size: int = 1, decode_unicode: bool = False) -> byt
345344
self.close()
346345

347346

347+
def _generate_boundary_str():
348+
hex_characters = "0123456789abcdef"
349+
_boundary = ""
350+
for _ in range(32):
351+
_boundary += random.choice(hex_characters)
352+
return _boundary
353+
354+
348355
class Session:
349356
"""HTTP session that shares sockets and ssl context."""
350357

@@ -396,13 +403,6 @@ def _send(socket: SocketType, data: bytes):
396403
def _send_as_bytes(self, socket: SocketType, data: str):
397404
return self._send(socket, bytes(data, "utf-8"))
398405

399-
def _generate_boundary_str(self):
400-
hex_characters = "0123456789abcdef"
401-
_boundary = ""
402-
for i in range(32):
403-
_boundary += random.choice(hex_characters)
404-
return _boundary
405-
406406
def _send_header(self, socket, header, value):
407407
if value is None:
408408
return
@@ -414,8 +414,7 @@ def _send_header(self, socket, header, value):
414414
self._send_as_bytes(socket, value)
415415
self._send(socket, b"\r\n")
416416

417-
# pylint: disable=too-many-arguments
418-
def _send_request(
417+
def _send_request( # pylint: disable=too-many-arguments
419418
self,
420419
socket: SocketType,
421420
host: str,
@@ -425,7 +424,7 @@ def _send_request(
425424
data: Any,
426425
json: Any,
427426
files: Optional[Dict[str, tuple]],
428-
):
427+
): # pylint: disable=too-many-branches,too-many-locals,too-many-statements
429428
# Check headers
430429
self._check_headers(headers)
431430

@@ -464,8 +463,9 @@ def _send_request(
464463
supplied_headers = {header.lower() for header in headers}
465464
boundary_str = None
466465

466+
# pylint: disable=too-many-nested-blocks
467467
if files is not None and isinstance(files, dict):
468-
boundary_str = self._generate_boundary_str()
468+
boundary_str = _generate_boundary_str()
469469
content_type_header = f"multipart/form-data; boundary={boundary_str}"
470470

471471
for fieldname in files.keys():
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import adafruit_connection_manager
5+
import wifi
6+
7+
import adafruit_requests
8+
9+
URL = "https://httpbin.org/post"
10+
11+
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
12+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
13+
requests = adafruit_requests.Session(pool, ssl_context)
14+
15+
with open("raspi_snip.png", "rb") as file_handle:
16+
files = {
17+
"file": (
18+
"raspi_snip.png",
19+
file_handle,
20+
"image/png",
21+
{"CustomHeader": "BlinkaRocks"},
22+
),
23+
"othervalue": (None, "HelloWorld"),
24+
}
25+
26+
with requests.post(URL, files=files) as resp:
27+
print(resp.content)

0 commit comments

Comments
 (0)