compiler-explorer / compiler-explorer/infra
ce user agent possible refactor
- Dominant language
- Python
- Stars
- 434
- Forks
- 429
- Avg merge
- 4d 21h
- Merged PRs (30d)
- 38
Description
(I'm Claude, posting on Patrick's behalf.)
Not needed for this PR, but a possible future refactor: this PR's `_ce_user_agent()` overlaps with the hand-rolled UA strings in `bin/lib/rust_crates.py`, which already build the same `product (comment)` shape for crates.io:
- `get_builder_user_agent_id()` -> `"Compiler Explorer Library Builder (github.com/compiler-explorer/infra)"`
- `get_manual_user_agent_id()` -> `"Compiler Explorer (github.com/compiler-explorer/infra)"`
Both places construct `product (comment)`; the only real difference is the comment (this PR uses `Compiler Explorer`, Rust uses the repo URL, which is what crates.io's crawler policy actually asks for -- a contact URL).
Since `installation_context.py` is a heavyweight module and `rust_crates.py` is intentionally tiny (just `json`/`urllib`), the clean combine would be a small shared `bin/lib/user_agent.py` that both import, rather than having `rust_crates` pull in `installation_context`:
```python
from __future__ import annotations
import requests
CE_USER_AGENT_URL = "https://github.com/compiler-explorer/infra"
def ce_user_agent(product: str = "") -> str:
"""Build a CE User-Agent as `product (+url)`."""
product = product.strip() or f"python-requests/{requests.__version__}"
return f"{product} (+{CE_USER_AGENT_URL})"
```
Then `installation_context` drops its local helper and calls `ce_user_agent(agent)`, and `rust_crates` becomes `ce_user_agent("Compiler Explorer Library Builder")` / `ce_user_agent("Compiler Explorer")`. Net effect: the fetch default keeps its `python-requests/{version}` prefix (preserving today's behavior) while every CE request also carries the contact URL.
Again, no need to do this here -- just flagging it so we don't grow a third UA convention later.
_Originally posted by @partouf in https://github.com/compiler-explorer/infra/issues/2237#issuecomment-5013345528_
Contributor guide
Research direction
Read the `_ce_user_agent()` helper in `installation_context.py` and `get_builder_user_agent_id()` / `get_manual_user_agent_id()` in `bin/lib/rust_crates.py`, then compare their current imports and User-Agent behavior. The refactor is done when both use the shared `bin/lib/user_agent.py` helper, the fetch default preserves its `python-requests/{version}` prefix, and CE requests include the contact URL.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Refactor
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 68/100