Skip to content

Commit

Permalink
Switch to requests library
Browse files Browse the repository at this point in the history
Better support for proxies...
  • Loading branch information
ahankinson committed Mar 20, 2024
1 parent 0fb6975 commit 22c5f63
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 9 deletions.
21 changes: 13 additions & 8 deletions import_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sys
import tomllib
import urllib.parse
import urllib.request
import requests
from typing import TypedDict, Optional

import ujson
Expand Down Expand Up @@ -96,15 +96,20 @@ def submit_hit(batch: tuple, cfg: dict) -> bool:
}

json_data = ujson.dumps(req_data)
req = urllib.request.Request(url=matomo_url, data=json_data.encode('utf-8'),
method="POST", headers={"Content-Type": "application/json"})

log.debug(f"Size of request body: %s KB", sys.getsizeof(json_data) * 0.001)

with urllib.request.urlopen(req) as response:
if response.status != 200:
log.error("Request failed: %s %s", response.status, response.reason)
return False
proxies = None
if p := cfg['matomo'].get('https_proxy', None):
proxies = {"https": p}

response = requests.post(url=matomo_url,
data=json_data.encode("utf-8"),
headers={"Content-Type": "application/json"},
proxies=proxies)

if response.status_code != 200:
log.error("Request failed: %s %s", response.status_code, response.reason)
return False

return True

Expand Down
161 changes: 160 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ readme = "README.md"
[tool.poetry.dependencies]
python = "^3.11"
ujson = "^5.9.0"
requests = "^2.31.0"

[tool.poetry.group.dev.dependencies]
ipython = "^8.22.2"
Expand Down

0 comments on commit 22c5f63

Please sign in to comment.