Skip to content

Remove str concatenation #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2024
Merged
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
36 changes: 16 additions & 20 deletions adafruit_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,22 +369,22 @@ def _build_boundary_data(self, files: dict): # pylint: disable=too-many-locals
file_name = field_values[0]
file_handle = field_values[1]

boundary_data = f"--{boundary_string}\r\n"
boundary_data += f'Content-Disposition: form-data; name="{field_name}"'
boundary_objects.append(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add all strings to the list

f'--{boundary_string}\r\nContent-Disposition: form-data; name="{field_name}"'
)
if file_name is not None:
boundary_data += f'; filename="{file_name}"'
boundary_data += "\r\n"
boundary_objects.append(f'; filename="{file_name}"')
boundary_objects.append("\r\n")
if len(field_values) >= 3:
file_content_type = field_values[2]
boundary_data += f"Content-Type: {file_content_type}\r\n"
boundary_objects.append(f"Content-Type: {file_content_type}\r\n")
if len(field_values) >= 4:
file_headers = field_values[3]
for file_header_key, file_header_value in file_headers.items():
boundary_data += f"{file_header_key}: {file_header_value}\r\n"
boundary_data += "\r\n"

content_length += len(boundary_data)
boundary_objects.append(boundary_data)
boundary_objects.append(
f"{file_header_key}: {file_header_value}\r\n"
)
boundary_objects.append("\r\n")

if hasattr(file_handle, "read"):
is_binary = False
Expand All @@ -400,19 +400,15 @@ def _build_boundary_data(self, files: dict): # pylint: disable=too-many-locals
file_handle.seek(0, SEEK_END)
content_length += file_handle.tell()
file_handle.seek(0)
boundary_objects.append(file_handle)
boundary_data = ""
else:
boundary_data = file_handle

boundary_data += "\r\n"
content_length += len(boundary_data)
boundary_objects.append(boundary_data)
boundary_objects.append(file_handle)
boundary_objects.append("\r\n")

boundary_data = f"--{boundary_string}--\r\n"
boundary_objects.append(f"--{boundary_string}--\r\n")

content_length += len(boundary_data)
boundary_objects.append(boundary_data)
for boundary_object in boundary_objects:
if isinstance(boundary_object, str):
content_length += len(boundary_object)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and calculate the length at the end


return boundary_string, content_length, boundary_objects

Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ envlist = py311
description = run tests
deps =
pytest==7.4.3
requests
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed to run tox, since requests is now in optional

commands = pytest

[testenv:coverage]
description = run coverage
deps =
pytest==7.4.3
pytest-cov==4.1.0
requests
package = editable
commands =
coverage run --source=. --omit=tests/* --branch {posargs} -m pytest
Expand Down
Loading