diff --git a/.shellcheckrc b/.shellcheckrc new file mode 100644 index 0000000..1ef75df --- /dev/null +++ b/.shellcheckrc @@ -0,0 +1,8 @@ +# shellcheck configuration +shell=bash +enable=all +severity=warning + +# SC2312: Ignore "consider invoking separately to avoid masking return values" +# (common pattern with command -v in this project) +disable=SC2312 diff --git a/healthcheck.py b/healthcheck.py index a4ecafa..e1b9f59 100644 --- a/healthcheck.py +++ b/healthcheck.py @@ -1,11 +1,14 @@ #!/usr/bin/env python3 +import json import os import sys -import urllib.request import urllib.error +import urllib.request from datetime import datetime, timezone from pathlib import Path +PAYLOAD = json.dumps({"name": "Ronaldo Freitas Dias"}).encode() + def load_url_from_config(): config_file = Path.home() / ".config" / "health-monitor" @@ -16,8 +19,11 @@ def load_url_from_config(): return None +HEADERS = {"User-Agent": "health-monitor/1.0", "Content-Type": "application/json"} + + def ping(url: str, timeout: int) -> int: - req = urllib.request.Request(url, method="GET", headers={"User-Agent": "health-monitor/1.0"}) + req = urllib.request.Request(url, data=PAYLOAD, method="GET", headers=HEADERS) with urllib.request.urlopen(req, timeout=timeout) as response: return response.getcode() @@ -25,7 +31,7 @@ def ping(url: str, timeout: int) -> int: def ping_fail(url: str, timeout: int) -> None: fail_url = url.rstrip("/") + "/fail" try: - req = urllib.request.Request(fail_url, method="GET", headers={"User-Agent": "health-monitor/1.0"}) + req = urllib.request.Request(fail_url, data=PAYLOAD, method="GET", headers=HEADERS) urllib.request.urlopen(req, timeout=timeout) except Exception: pass diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..bb1773c --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,20 @@ +[tool.ruff] +target-version = "py39" +line-length = 110 + +[tool.ruff.lint] +select = [ + "E", # pycodestyle errors + "W", # pycodestyle warnings + "F", # pyflakes + "I", # isort + "UP", # pyupgrade + "B", # flake8-bugbear + "SIM", # flake8-simplify +] +ignore = [ + "B904", # raise from exc — não obrigatório em scripts simples +] + +[tool.ruff.lint.isort] +known-first-party = ["healthcheck"]