— Allow to customize agent User-Agent header (fix #78)

This commit is contained in:
Luc Didry 2025-02-12 16:08:16 +01:00
parent 1b484da27a
commit db54dd2cdd
No known key found for this signature in database
GPG key ID: EA868E12D0257E3C
3 changed files with 18 additions and 5 deletions

View file

@ -2,6 +2,8 @@
## [Unreleased] ## [Unreleased]
- ✨ — Allow to customize agent User-Agent header (#78)
## 0.7.4 ## 0.7.4
Date: 2025-02-12 Date: 2025-02-12

View file

@ -37,11 +37,17 @@ def log_failure(retry_state):
class ArgosAgent: # pylint: disable-msg=too-many-instance-attributes class ArgosAgent: # pylint: disable-msg=too-many-instance-attributes
"""The Argos agent is responsible for running the checks and reporting the results.""" """The Argos agent is responsible for running the checks and reporting the results."""
def __init__(self, server: str, auth: str, max_tasks: int, wait_time: int): def __init__( # pylint: disable-msg=too-many-positional-arguments
self, server: str, auth: str, max_tasks: int, wait_time: int, user_agent: str
):
self.server = server self.server = server
self.max_tasks = max_tasks self.max_tasks = max_tasks
self.wait_time = wait_time self.wait_time = wait_time
self.auth = auth self.auth = auth
if user_agent == "":
self.ua = user_agent
else:
self.ua = f" - {user_agent}"
self._http_client: httpx.AsyncClient | None = None self._http_client: httpx.AsyncClient | None = None
self._http_client_v4: httpx.AsyncClient | None = None self._http_client_v4: httpx.AsyncClient | None = None
self._http_client_v6: httpx.AsyncClient | None = None self._http_client_v6: httpx.AsyncClient | None = None
@ -53,13 +59,13 @@ class ArgosAgent: # pylint: disable-msg=too-many-instance-attributes
async def run(self): async def run(self):
auth_header = { auth_header = {
"Authorization": f"Bearer {self.auth}", "Authorization": f"Bearer {self.auth}",
"User-Agent": f"Argos Panoptes agent {VERSION}", "User-Agent": f"Argos Panoptes agent {VERSION}{self.ua}",
} }
self._http_client = httpx.AsyncClient(headers=auth_header) self._http_client = httpx.AsyncClient(headers=auth_header)
ua_header = { ua_header = {
"User-Agent": f"Argos Panoptes {VERSION} " "User-Agent": f"Argos Panoptes {VERSION} "
"(about: https://argos-monitoring.framasoft.org/)", f"(about: https://argos-monitoring.framasoft.org/){self.ua}",
} }
self._http_client_v4 = httpx.AsyncClient( self._http_client_v4 = httpx.AsyncClient(
headers=ua_header, headers=ua_header,

View file

@ -92,7 +92,12 @@ def version():
default="INFO", default="INFO",
type=click.Choice(logging.LOG_LEVELS, case_sensitive=False), type=click.Choice(logging.LOG_LEVELS, case_sensitive=False),
) )
def agent(server_url, auth, max_tasks, wait_time, log_level): @click.option(
"--user-agent",
default="",
help="A custom string to append to the User-Agent header",
)
def agent(server_url, auth, max_tasks, wait_time, log_level, user_agent): # pylint: disable-msg=too-many-positional-arguments
"""Get and run tasks for the provided server. Will wait for new tasks. """Get and run tasks for the provided server. Will wait for new tasks.
Usage: argos agent https://argos.example.org "auth-token-here" Usage: argos agent https://argos.example.org "auth-token-here"
@ -108,7 +113,7 @@ def agent(server_url, auth, max_tasks, wait_time, log_level):
from argos.logging import logger from argos.logging import logger
logger.setLevel(log_level) logger.setLevel(log_level)
agent_ = ArgosAgent(server_url, auth, max_tasks, wait_time) agent_ = ArgosAgent(server_url, auth, max_tasks, wait_time, user_agent)
asyncio.run(agent_.run()) asyncio.run(agent_.run())