Skip to content

Commit

Permalink
Sparsezoo stdout fix (#322)
Browse files Browse the repository at this point in the history
* Revert "Suppress analytics errors and messages (#318)"

This reverts commit 960cae2.

* Catch HttpError over exception

* Catch HttpError over exception when checking country

(cherry picked from commit 9b5a0bd)
  • Loading branch information
rahul-tuli committed May 31, 2023
1 parent 0a9ed9b commit b4e0214
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 87 deletions.
51 changes: 25 additions & 26 deletions src/sparsezoo/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

import machineid
import requests
from requests import HTTPError

from sparsezoo.utils.gdpr import is_gdpr_country
from sparsezoo.utils.suppress import suppress_stdout_stderr
from sparsezoo.version import version as sparsezoo_version


Expand Down Expand Up @@ -122,31 +122,30 @@ def send_event(
event_params = {}

def _send_request():
with suppress_stdout_stderr(suppress=not _DEBUG):
event_params.update(self._package_params)
event_params["package"] = self._package
event_params["version"] = self._version
payload = {
"client_id": self._client_id,
"events": [{"name": event_name, "params": event_params}],
}
headers = {
"Content-Type": "application/json",
}
data = json.dumps(payload)

try:
response = requests.post(self._url, headers=headers, data=data)
response.raise_for_status()
body = response.content
if _DEBUG:
print(body)
except Exception as err:
if _DEBUG:
print(err)

if raise_errors:
raise err
event_params.update(self._package_params)
event_params["package"] = self._package
event_params["version"] = self._version
payload = {
"client_id": self._client_id,
"events": [{"name": event_name, "params": event_params}],
}
headers = {
"Content-Type": "application/json",
}
data = json.dumps(payload)

try:
response = requests.post(self._url, headers=headers, data=data)
response.raise_for_status()
body = response.content
if _DEBUG:
print(body)
except HTTPError as http_error:
if _DEBUG:
print(http_error)

if raise_errors:
raise http_error

thread = threading.Thread(target=_send_request)
thread.start()
Expand Down
29 changes: 13 additions & 16 deletions src/sparsezoo/utils/gdpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

import geocoder
import requests

from sparsezoo.utils.suppress import suppress_stdout_stderr
from requests import HTTPError


__all__ = ["get_external_ip", "get_country_code", "is_gdpr_country"]
Expand Down Expand Up @@ -58,28 +57,26 @@ def get_external_ip() -> Optional[str]:
"""
:return: the external ip of the machine, None if unable to get
"""
with suppress_stdout_stderr():
try:
response = requests.get("https://ident.me")
external_ip = response.text.strip()
try:
response = requests.get("https://ident.me")
external_ip = response.text.strip()

return external_ip
except Exception:
return None
return external_ip
except Exception:
return None


def get_country_code() -> Optional[str]:
"""
:return: the country code of the machine, None if unable to get
"""
with suppress_stdout_stderr():
try:
ip = get_external_ip()
geo = geocoder.ip(ip)
try:
ip = get_external_ip()
geo = geocoder.ip(ip)

return geo.country
except Exception:
return None
return geo.country
except HTTPError:
return None


def is_gdpr_country() -> bool:
Expand Down
45 changes: 0 additions & 45 deletions src/sparsezoo/utils/suppress.py

This file was deleted.

0 comments on commit b4e0214

Please sign in to comment.