Skip to content

Commit

Permalink
usbmux: make fetch py37 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
sgemin1_ford committed Jul 22, 2024
1 parent 59bbb0e commit 8db4a99
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,6 @@ ENV/
.vscode
.DS_Store
xcuserdata/

# IDE
.idea/
19 changes: 14 additions & 5 deletions wda/usbmux/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
"""

import json
from http.client import HTTPConnection, HTTPSConnection
from http.client import HTTPConnection, HTTPSConnection, HTTPResponse
from urllib.parse import urlparse

from wda.usbmux.exceptions import HTTPError, MuxConnectError, MuxError
from wda.usbmux.pyusbmux import select_device

_DEFAULT_CHUNK_SIZE = 4096

def http_create(url: str) -> HTTPConnection:
u = urlparse(url)
Expand Down Expand Up @@ -42,7 +43,7 @@ def getcode(self) -> int:
return self.status_code


def fetch(url: str, method="GET", data=None, timeout=None) -> HTTPResponseWrapper:
def fetch(url: str, method="GET", data=None, timeout=None, chunk_size: int = _DEFAULT_CHUNK_SIZE) -> HTTPResponseWrapper:
"""
thread safe http request
Expand All @@ -61,10 +62,18 @@ def fetch(url: str, method="GET", data=None, timeout=None) -> HTTPResponseWrappe
else:
conn.request(method, urlpath, json.dumps(data), headers={"Content-Type": "application/json"})
response = conn.getresponse()
content = bytearray()
while chunk := response.read(4096):
content.extend(chunk)
content = _recv(response, chunk_size)
resp = HTTPResponseWrapper(content, response.status)
return resp
except Exception as e:
raise HTTPError(e)


def _recv(response:HTTPResponse, chunk_size: int = _DEFAULT_CHUNK_SIZE) -> bytearray:
content = bytearray()
while True:
chunk = response.read(chunk_size)
if len(chunk) == 0:
break
content.extend(chunk)
return content

0 comments on commit 8db4a99

Please sign in to comment.