Skip to content

Instantly share code, notes, and snippets.

@mtik00
Last active June 26, 2023 19:16
Show Gist options
  • Save mtik00/891121a68e1eddc98380a4e846b41949 to your computer and use it in GitHub Desktop.
Save mtik00/891121a68e1eddc98380a4e846b41949 to your computer and use it in GitHub Desktop.
stdlib URL request function
import json
import urllib.parse
import urllib.request
def request(
url: str,
method: str | None = None,
data: dict | None = None,
query_params: dict | None = None,
raise_on_error: bool = True,
) -> str | dict | list:
headers = {
"Content-Type": "application/json",
"Authorization": f"Basic abcdef==",
}
encoded_data: bytes | None = None
if data:
encoded_data = json.dumps(data).encode("utf-8")
if query_params:
encoded_params = urllib.parse.urlencode(query_params)
url = f"{url}?{encoded_params}"
req = urllib.request.Request(url, data=encoded_data, headers=headers, method=method)
page = None
try:
with urllib.request.urlopen(req) as response:
page = response.read()
except urllib.error.HTTPError as e:
if raise_on_error:
raise
print("WARNING: status code", e.status)
print("...", e.url)
print("...", str(e))
return ""
try:
result = json.loads(page)
except Exception:
result = page
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment