Building AI agents, atomically
Keep the FXMacroData key on the HTTPS origin, validate responses and dates
Robert Tidball184709f3f0c4
diff --git a/atomic-forge/tools/fxmacrodata/README.md b/atomic-forge/tools/fxmacrodata/README.mdindex c4c3598..8f89fd8 100644--- a/atomic-forge/tools/fxmacrodata/README.md+++ b/atomic-forge/tools/fxmacrodata/README.md@@ -1,9 +1,9 @@# FXMacroData Tool## Overview-Queries the [FXMacroData](https://fxmacrodata.com/?utm_source=github&utm_medium=referral&utm_campaign=atomic-agents&utm_content=readme) API for official-source macroeconomic data: release history with the official announcement timestamp of each print (CPI, GDP, unemployment, payrolls, policy rates, bond yields and more), the latest value of every indicator for a currency, upcoming release dates, the indicator catalogue, FX spot history, CFTC Commitments of Traders positioning, and commodity prices.+Queries the [FXMacroData](https://fxmacrodata.com) API for official-source macroeconomic data: release history with the official announcement timestamp of each print (CPI, GDP, unemployment, payrolls, policy rates, bond yields and more), the latest value of every indicator for a currency, upcoming release dates, the indicator catalogue, FX spot history, CFTC Commitments of Traders positioning, and commodity prices.-USD releases, the USD release calendar and the indicator catalogue work without an API key. Other currencies, FX rates, COT and commodities need a key from [fxmacrodata.com/subscribe](https://fxmacrodata.com/subscribe?utm_source=github&utm_medium=referral&utm_campaign=atomic-agents&utm_content=readme).+USD releases, the USD release calendar and the indicator catalogue work without an API key. Other currencies, FX rates, COT and commodities need a key from [fxmacrodata.com/subscribe](https://fxmacrodata.com/subscribe).## Prerequisites and Dependencies- Python 3.12 or later@@ -17,8 +17,8 @@ USD releases, the USD release calendar and the indicator catalogue work without2. Or copy the `tool/` folder directly into your project.## Configuration-- `api_key` (str): FXMacroData API key, sent in the `X-API-Key` header. Falls back to the `FXMACRODATA_API_KEY` environment variable. Leave empty for keyless USD access.-- `base_url` (str): API base URL (default `https://api.fxmacrodata.com/v1`).+- `api_key` (str): FXMacroData API key, sent in the `X-API-Key` header. Falls back to the `FXMACRODATA_API_KEY` environment variable; surrounding whitespace is ignored. Leave empty for keyless USD access. The key never appears in `url` or `error`.+- `base_url` (str): API base URL (default `https://api.fxmacrodata.com/v1`). Must be HTTPS; redirects are not followed, so the key is only ever sent to this origin.- `timeout` (float): HTTP timeout in seconds (default 30).## Input & Output Structure@@ -35,7 +35,7 @@ USD releases, the USD release calendar and the indicator catalogue work without- `currency` (str, optional): three-letter code such as `usd`, `eur`, `jpy`.- `indicator` (str, optional): indicator slug such as `inflation`, `policy_rate`, `gdp`.- `quote` (str, optional): quote currency for `forex`.-- `start_date`, `end_date` (str, optional): `YYYY-MM-DD`.+- `start_date`, `end_date` (str, optional): real dates as `YYYY-MM-DD`; `start_date` must not be after `end_date`.- `limit` (int, optional): rows per page, 1-100. History endpoints return 20 rows by default.- `offset` (int, optional): row offset for paging.@@ -45,7 +45,7 @@ USD releases, the USD release calendar and the indicator catalogue work without- `data`: the rows returned by the API, with the API's own field names. Announcement rows carry `date` (reference period), `val`, and `announcement_datetime` (Unix seconds of the official release). For `catalogue` this is a mapping of indicator slug to metadata.- `pagination` (dict, optional): `has_more`, `next_offset` and `total_count` for paged endpoints.- `metadata` (dict): the remaining top-level response fields, such as source, provenance and data quality.-- `error` (str, optional): set instead of raising when the input is incomplete or the request fails, so an agent can read the reason (for example a missing API key for a non-USD currency) and adjust.+- `error` (str, optional): set instead of raising when the input is invalid, the request fails, or the response is not the documented shape, so an agent can read the reason (for example a missing API key for a non-USD currency) and adjust.## Usage@@ -65,7 +65,7 @@ for row in upcoming.data[:5]:To page through a long history, repeat the call with `offset=output.pagination["next_offset"]` until `has_more` is false.-The full list of endpoints and response fields is in the [API reference](https://fxmacrodata.com/documentation/reference?utm_source=github&utm_medium=referral&utm_campaign=atomic-agents&utm_content=readme).+The full list of endpoints and response fields is in the [API reference](https://fxmacrodata.com/documentation/reference).## ContributingPRs welcome. See the main repo `CONTRIBUTING.md`.diff --git a/atomic-forge/tools/fxmacrodata/tests/test_fxmacrodata.py b/atomic-forge/tools/fxmacrodata/tests/test_fxmacrodata.pyindex 76760fb..0e0f725 100644--- a/atomic-forge/tools/fxmacrodata/tests/test_fxmacrodata.py+++ b/atomic-forge/tools/fxmacrodata/tests/test_fxmacrodata.py@@ -1,3 +1,4 @@+import jsonimport osimport sysfrom unittest.mock import MagicMock, patch@@ -12,6 +13,7 @@ from tool.fxmacrodata import ( # noqa: E402FXMacroDataToolConfig,FXMacroDataToolInputSchema,FXMacroDataToolOutputSchema,+ build_request,)@@ -45,6 +47,8 @@ CATALOGUE_PAYLOAD = {"inflation": {"name": "Inflation (CPI)", "unit": "%", "frequency": "Monthly"},}+SECRET = "fxmd-dummy-secret-0123456789"+def _response(status: int, payload, reason: str = "OK") -> MagicMock:response = MagicMock()@@ -54,6 +58,18 @@ def _response(status: int, payload, reason: str = "OK") -> MagicMock:return response+def _real_response(request: requests.PreparedRequest, status: int, body: str, headers=None) -> requests.Response:+ """A genuine requests.Response, so redirect handling and header checks run through Requests itself."""+ response = requests.Response()+ response.status_code = status+ response._content = body.encode()+ response.headers.update(headers or {})+ response.url = request.url+ response.request = request+ response.reason = "Found" if status == 302 else "OK"+ return response++@pytest.fixturedef tool(monkeypatch):monkeypatch.delenv("FXMACRODATA_API_KEY", raising=False)@@ -85,7 +101,7 @@ def tool(monkeypatch):],)def test_build_request(fields, path, query):- assert FXMacroDataTool.build_request(FXMacroDataToolInputSchema(**fields)) == (path, query)+ assert build_request(FXMacroDataToolInputSchema(**fields)) == (path, query)@pytest.mark.parametrize(@@ -108,6 +124,23 @@ def test_invalid_input_returns_error_without_request(tool, fields, message):assert output.data == []+ "dates, message",+ [+ ({"start_date": "2026-02-30"}, "'start_date' must be a real date"),+ ({"end_date": "2026-13-01"}, "'end_date' must be a real date"),+ ({"start_date": ""}, "'start_date' must be a real date"),+ ({"start_date": "20260101"}, "'start_date' must be a real date"),+ ({"start_date": "2026-03-01", "end_date": "2026-02-01"}, "'start_date' must not be after 'end_date'"),+ ],+)+def test_impossible_empty_and_reversed_dates_are_rejected(tool, dates, message):+ with patch("tool.fxmacrodata.requests.get") as mock_get:+ output = tool.run(FXMacroDataToolInputSchema(endpoint="forex", currency="eur", quote="usd", **dates))+ mock_get.assert_not_called()+ assert message in output.error++def test_announcements_split_rows_pagination_and_metadata(tool):with patch("tool.fxmacrodata.requests.get", return_value=_response(200, ANNOUNCEMENTS_PAYLOAD)) as mock_get:output = tool.run(FXMacroDataToolInputSchema(endpoint="announcements", currency="usd", indicator="inflation", limit=2))@@ -124,6 +157,7 @@ def test_announcements_split_rows_pagination_and_metadata(tool):assert args[0] == "https://api.fxmacrodata.com/v1/announcements/usd/inflation"assert kwargs["params"] == {"limit": "2"}assert kwargs["timeout"] == 30.0+ assert kwargs["allow_redirects"] is Falseassert "X-API-Key" not in kwargs["headers"]@@ -170,7 +204,6 @@ def test_http_error_surfaces_api_detail(tool):output = tool.run(FXMacroDataToolInputSchema(endpoint="forex", currency="eur", quote="usd"))assert output.error == "FXMacroData returned 401: This endpoint requires an Individual or Business API key."- assert output.url == "https://api.fxmacrodata.com/v1/forex/eur/usd"assert output.data == []@@ -182,10 +215,111 @@ def test_http_error_without_json_body(tool):assert output.error == "FXMacroData returned 502 Bad Gateway"-def test_network_error_is_reported(tool):- with patch("tool.fxmacrodata.requests.get", side_effect=requests.ConnectionError("connection refused")):+def test_non_https_base_url_is_refused():+ tool = FXMacroDataTool(FXMacroDataToolConfig(api_key=SECRET, base_url="http://api.fxmacrodata.com/v1"))+ with patch("tool.fxmacrodata.requests.get") as mock_get:+ output = tool.run(FXMacroDataToolInputSchema(endpoint="latest", currency="usd"))+ mock_get.assert_not_called()+ assert output.error == "The FXMacroData base URL must use HTTPS."+++# Regression: a 302 from the HTTPS API to an unrelated HTTP host must not carry X-API-Key there.+def test_redirect_to_other_http_host_does_not_forward_api_key():+ sent = []++ def send(adapter, request, **kwargs):+ sent.append((request.url, dict(request.headers)))+ if request.url.startswith("https://api.fxmacrodata.com/"):+ return _real_response(request, 302, "", {"Location": "http://collector.example/steal"})+ return _real_response(request, 200, json.dumps(CALENDAR_PAYLOAD))++ tool = FXMacroDataTool(FXMacroDataToolConfig(api_key=SECRET))+ with patch("requests.adapters.HTTPAdapter.send", send):+ output = tool.run(FXMacroDataToolInputSchema(endpoint="calendar", currency="usd"))++ assert [url for url, _ in sent] == ["https://api.fxmacrodata.com/v1/calendar/usd"]+ assert all(not url.startswith("http://collector.example") for url, _ in sent)+ assert output.error == "FXMacroData answered with a redirect (302); redirects are not followed."+ assert output.data == []+++# Regression: a key with leading whitespace raised InvalidHeader, whose text carried the full key.+def test_key_with_leading_whitespace_is_stripped_and_never_echoed():+ sent = []++ def send(adapter, request, **kwargs):+ sent.append(dict(request.headers))+ return _real_response(request, 200, json.dumps(CALENDAR_PAYLOAD))++ tool = FXMacroDataTool(FXMacroDataToolConfig(api_key=f" {SECRET}"))+ with patch("requests.adapters.HTTPAdapter.send", send):+ output = tool.run(FXMacroDataToolInputSchema(endpoint="calendar", currency="usd"))++ assert output.error is None+ assert sent[0]["X-API-Key"] == SECRET+++def test_key_that_cannot_be_a_header_is_reported_without_its_value():+ tool = FXMacroDataTool(FXMacroDataToolConfig(api_key=f"{SECRET}\nInjected: 1"))+ with patch("requests.adapters.HTTPAdapter.send") as send:+ output = tool.run(FXMacroDataToolInputSchema(endpoint="calendar", currency="usd"))++ send.assert_not_called()+ assert SECRET not in output.error+ assert "cannot be sent in an HTTP header" in output.error+++ "error",+ [requests.ConnectionError(f"refused for X-API-Key: {SECRET}"), requests.exceptions.InvalidHeader(f"bad {SECRET}")],+)+def test_request_exception_text_never_reaches_the_output(error):+ tool = FXMacroDataTool(FXMacroDataToolConfig(api_key=SECRET))+ with patch("requests.adapters.HTTPAdapter.send", side_effect=error):+ output = tool.run(FXMacroDataToolInputSchema(endpoint="latest", currency="usd"))++ assert SECRET not in output.error+ assert output.error == f"Could not reach FXMacroData ({type(error).__name__})."+++def test_upstream_detail_echoing_the_key_is_redacted():+ tool = FXMacroDataTool(FXMacroDataToolConfig(api_key=SECRET))+ with patch("tool.fxmacrodata.requests.get", return_value=_response(403, {"detail": f"key {SECRET} revoked"})):+ output = tool.run(FXMacroDataToolInputSchema(endpoint="latest", currency="usd"))+ assert output.error == "FXMacroData returned 403: key [redacted] revoked"+++# Regression: HTTP 200 bodies that are not the documented shape must come back as an error, not as data=[] or a+# ValidationError.+ "endpoint, payload, message",+ [+ ("latest", {"detail": "upstream failure"}, "unexpected response body: upstream failure"),+ ("latest", {"data": None}, "unexpected response body"),+ ("latest", {"data": "not rows"}, "unexpected response body"),+ ("latest", {"data": [1, 2, 3]}, "unexpected response body"),+ ("latest", {"data": [], "pagination": "page 1"}, "unexpected response body"),+ ("latest", ["not", "an", "object"], "unexpected response body"),+ ("catalogue", {"detail": "upstream failure"}, "unexpected response body: upstream failure"),+ ("catalogue", {"gdp": "GDP"}, "unexpected response body"),+ ("catalogue", [], "unexpected response body"),+ ],+)+def test_malformed_200_responses_return_an_error(tool, endpoint, payload, message):+ with patch("tool.fxmacrodata.requests.get", return_value=_response(200, payload)):+ output = tool.run(FXMacroDataToolInputSchema(endpoint=endpoint, currency="usd"))++ assert isinstance(output, FXMacroDataToolOutputSchema)+ assert message in output.error+ assert output.data == []+++def test_non_json_200_response_returns_an_error(tool):+ response = _response(200, None)+ response.json.side_effect = ValueError("Expecting value")+ with patch("tool.fxmacrodata.requests.get", return_value=response):output = tool.run(FXMacroDataToolInputSchema(endpoint="latest", currency="usd"))- assert "connection refused" in output.error+ assert output.error == "FXMacroData returned a response that is not JSON."@pytest.mark.asynciodiff --git a/atomic-forge/tools/fxmacrodata/tool/fxmacrodata.py b/atomic-forge/tools/fxmacrodata/tool/fxmacrodata.pyindex 56fcea6..8a1b4bd 100644--- a/atomic-forge/tools/fxmacrodata/tool/fxmacrodata.py+++ b/atomic-forge/tools/fxmacrodata/tool/fxmacrodata.py@@ -1,20 +1,43 @@import asyncioimport osimport re-from typing import Any, Dict, List, Literal, Optional, Tuple, Union+from datetime import date+from typing import Dict, List, Literal, Optional, Tuple, Union+from urllib.parse import urlsplitimport requests-from pydantic import Field+from pydantic import Field, JsonValuefrom atomic_agents import BaseIOSchema, BaseTool, BaseToolConfig-_CURRENCY_RE = re.compile(r"^[A-Za-z]{3}$")-_SLUG_RE = re.compile(r"^[A-Za-z0-9_]+$")-_DATE_RE = re.compile(r"^\d{4}-\d{2}-\d{2}$")+CURRENCY_PATTERN = re.compile(r"^[a-z]{3}$")+INDICATOR_PATTERN = re.compile(r"^[a-z0-9_]+$")+DATE_PATTERN = re.compile(r"^\d{4}-\d{2}-\d{2}$")+# Printable ASCII without spaces: anything else cannot travel in an HTTP header.+API_KEY_PATTERN = re.compile(r"^[\x21-\x7e]+$")-# Endpoints whose responses wrap rows in a "data" list next to metadata.-_ROW_ENDPOINTS = {"announcements", "latest", "calendar", "forex", "cot", "commodities"}+ENDPOINT_PATHS = {+ "announcements": "/announcements/{currency}/{indicator}",+ "latest": "/announcements/{currency}/latest",+ "calendar": "/calendar/{currency}",+ "catalogue": "/data_catalogue/{currency}",+ "forex": "/forex/{currency}/{quote}",+ "cot": "/cot/{currency}",+ "commodities": "/commodities/{indicator}",+}++FIELD_RULES = {+ "currency": (CURRENCY_PATTERN, "a three-letter currency code such as 'usd' or 'eur'"),+ "quote": (CURRENCY_PATTERN, "a three-letter currency code such as 'usd' or 'eur'"),+ "indicator": (INDICATOR_PATTERN, "an indicator slug such as 'inflation' or 'policy_rate'"),+}++JsonObject = Dict[str, JsonValue]+++class FXMacroDataError(Exception):+ """A request or response problem, worded so it is safe to hand back to an agent."""################@@ -64,7 +87,7 @@ class FXMacroDataToolOutputSchema(BaseIOSchema):endpoint: str = Field(..., description="The endpoint that was called.")url: str = Field(..., description="The request URL, without the API key.")- data: Union[List[Dict[str, Any]], Dict[str, Any]] = Field(+ data: Union[List[JsonObject], JsonObject] = Field(default_factory=list,description=("Rows returned by the API. Announcement rows carry 'date' (reference period), 'val', and"@@ -72,10 +95,10 @@ class FXMacroDataToolOutputSchema(BaseIOSchema):" of indicator slug to its metadata."),)- pagination: Optional[Dict[str, Any]] = Field(+ pagination: Optional[JsonObject] = Field(None, description="Paging info ('has_more', 'next_offset', 'total_count') when the endpoint pages.")- metadata: Dict[str, Any] = Field(+ metadata: JsonObject = Field(default_factory=dict,description="Remaining top-level response fields, such as source, units, provenance and data quality.",)@@ -92,10 +115,111 @@ class FXMacroDataToolConfig(BaseToolConfig):default="",description="FXMacroData API key. Falls back to the FXMACRODATA_API_KEY environment variable. Optional for USD data.",)- base_url: str = Field(default="https://api.fxmacrodata.com/v1", description="FXMacroData API base URL.")+ base_url: str = Field(default="https://api.fxmacrodata.com/v1", description="FXMacroData API base URL (HTTPS only).")timeout: float = Field(default=30.0, ge=1.0, le=120.0, description="HTTP request timeout in seconds.")+####################+# REQUEST BUILDING #+####################+def validated_field(params: FXMacroDataToolInputSchema, name: str) -> str:+ """Return a path field lower-cased, or raise if it is missing or malformed."""+ pattern, expected = FIELD_RULES[name]+ value = (getattr(params, name) or "").strip().lower()+ if not pattern.match(value):+ raise FXMacroDataError(f"'{name}' must be {expected}.")+ return value+++def parse_date(name: str, value: str) -> date:+ """Parse a YYYY-MM-DD string into a real calendar date."""+ try:+ if DATE_PATTERN.match(value):+ return date.fromisoformat(value)+ except ValueError:+ pass+ raise FXMacroDataError(f"'{name}' must be a real date formatted YYYY-MM-DD.")+++def optional_date(name: str, value: Optional[str]) -> Optional[date]:+ return None if value is None else parse_date(name, value)+++def check_order(start: Optional[date], end: Optional[date]) -> None:+ if start is not None and end is not None and start > end:+ raise FXMacroDataError("'start_date' must not be after 'end_date'.")+++def date_query(params: FXMacroDataToolInputSchema) -> Dict[str, str]:+ """Validate the date range and return it as query parameters."""+ dates = {name: optional_date(name, getattr(params, name)) for name in ("start_date", "end_date")}+ check_order(dates["start_date"], dates["end_date"])+ return {name: day.isoformat() for name, day in dates.items() if day is not None}+++def build_path(params: FXMacroDataToolInputSchema) -> str:+ template = ENDPOINT_PATHS[params.endpoint]+ names = re.findall(r"{(\w+)}", template)+ return template.format(**{name: validated_field(params, name) for name in names})+++def build_query(params: FXMacroDataToolInputSchema) -> Dict[str, str]:+ query = date_query(params)+ paging = {"limit": params.limit, "offset": params.offset}+ query.update({name: str(value) for name, value in paging.items() if value is not None})+ if params.endpoint == "calendar" and params.indicator is not None:+ query["indicator"] = validated_field(params, "indicator")+ return query+++def build_request(params: FXMacroDataToolInputSchema) -> Tuple[str, Dict[str, str]]:+ """Return the (path, query) pair for an input, validating the fields the endpoint needs."""+ return build_path(params), build_query(params)+++####################+# RESPONSE PARSING #+####################+def is_object_list(value: JsonValue) -> bool:+ return isinstance(value, list) and all(isinstance(item, dict) for item in value)+++def upstream_detail(payload: JsonValue) -> str:+ detail = payload.get("detail") if isinstance(payload, dict) else None+ return f": {detail}" if isinstance(detail, str) and detail else ""+++def unexpected_body(payload: JsonValue) -> FXMacroDataError:+ return FXMacroDataError(f"FXMacroData returned an unexpected response body{upstream_detail(payload)}")+++def response_object(payload: JsonValue) -> JsonObject:+ if not isinstance(payload, dict):+ raise unexpected_body(payload)+ return payload+++def metadata_fields(body: JsonObject) -> JsonObject:+ return {name: value for name, value in body.items() if name not in ("data", "pagination")}+++def parse_rows(payload: JsonValue) -> Tuple[List[JsonObject], Optional[JsonObject], JsonObject]:+ """Split a row endpoint's body into rows, pagination and the remaining metadata."""+ body = response_object(payload)+ rows, pagination = body.get("data"), body.get("pagination") or {}+ if not is_object_list(rows) or not isinstance(pagination, dict):+ raise unexpected_body(body)+ return rows, pagination or None, metadata_fields(body)+++def parse_catalogue(payload: JsonValue) -> JsonObject:+ """The catalogue is a mapping of indicator slug to an object describing it."""+ body = response_object(payload)+ if not body or not all(isinstance(entry, dict) for entry in body.values()):+ raise unexpected_body(body)+ return body++###################### MAIN TOOL & LOGIC ######################@@ -104,100 +228,64 @@ class FXMacroDataTool(BaseTool[FXMacroDataToolInputSchema, FXMacroDataToolOutputdef __init__(self, config: FXMacroDataToolConfig = FXMacroDataToolConfig()):super().__init__(config)- self.api_key = config.api_key or os.getenv("FXMACRODATA_API_KEY", "")- self.base_url = config.base_url.rstrip("/")+ self.api_key = (config.api_key or os.getenv("FXMACRODATA_API_KEY", "")).strip()+ self.base_url = config.base_url.strip().rstrip("/")self.timeout = config.timeout- @staticmethod- def _currency(value: Optional[str], name: str) -> str:- if not value or not _CURRENCY_RE.match(value.strip()):- raise ValueError(f"'{name}' must be a three-letter currency code such as 'usd' or 'eur'.")- return value.strip().lower()+ def redact(self, message: str) -> str:+ """Remove the API key from any text that leaves the tool."""+ return message.replace(self.api_key, "[redacted]") if self.api_key else message- @staticmethod- def _slug(value: Optional[str], name: str) -> str:- if not value or not _SLUG_RE.match(value.strip()):- raise ValueError(f"'{name}' must be an indicator slug such as 'inflation' or 'policy_rate'.")- return value.strip().lower()-- @classmethod- def build_request(cls, params: FXMacroDataToolInputSchema) -> Tuple[str, Dict[str, str]]:- """Return the (path, query) pair for an input, validating the fields the endpoint needs."""- endpoint = params.endpoint- if endpoint == "commodities":- path = f"/commodities/{cls._slug(params.indicator, 'indicator')}"- else:- currency = cls._currency(params.currency, "currency")- if endpoint == "announcements":- path = f"/announcements/{currency}/{cls._slug(params.indicator, 'indicator')}"- elif endpoint == "latest":- path = f"/announcements/{currency}/latest"- elif endpoint == "calendar":- path = f"/calendar/{currency}"- elif endpoint == "catalogue":- path = f"/data_catalogue/{currency}"- elif endpoint == "forex":- path = f"/forex/{currency}/{cls._currency(params.quote, 'quote')}"- else:- path = f"/cot/{currency}"-- query: Dict[str, str] = {}- for name in ("start_date", "end_date"):- value = getattr(params, name)- if value:- if not _DATE_RE.match(value):- raise ValueError(f"'{name}' must be formatted YYYY-MM-DD.")- query[name] = value- if params.limit is not None:- query["limit"] = str(params.limit)- if params.offset is not None:- query["offset"] = str(params.offset)- if endpoint == "calendar" and params.indicator:- query["indicator"] = cls._slug(params.indicator, "indicator")- return path, query+ def headers(self) -> Dict[str, str]:+ if self.api_key and not API_KEY_PATTERN.match(self.api_key):+ raise FXMacroDataError("The FXMacroData API key contains characters that cannot be sent in an HTTP header.")+ return {"Accept": "application/json", **({"X-API-Key": self.api_key} if self.api_key else {})}- @staticmethod- def _error_message(response: requests.Response) -> str:+ def request_url(self, path: str) -> str:+ if urlsplit(self.base_url).scheme != "https":+ raise FXMacroDataError("The FXMacroData base URL must use HTTPS.")+ return f"{self.base_url}{path}"++ def fetch(self, url: str, query: Dict[str, str]) -> JsonValue:+ """GET one URL. Redirects are never followed, so the key only goes to the configured HTTPS origin."""try:- body = response.json()- except ValueError:- body = None- detail = body.get("detail") if isinstance(body, dict) else None- if isinstance(detail, str) and detail:- return f"FXMacroData returned {response.status_code}: {detail}"- return f"FXMacroData returned {response.status_code} {response.reason}"-- def _get(self, path: str, query: Dict[str, str]) -> Union[Dict[str, Any], List[Any]]:- headers = {"Accept": "application/json"}- if self.api_key:- headers["X-API-Key"] = self.api_key- response = requests.get(f"{self.base_url}{path}", params=query, headers=headers, timeout=self.timeout)+ response = requests.get(url, params=query, headers=self.headers(), timeout=self.timeout, allow_redirects=False)+ except requests.RequestException as error:+ raise FXMacroDataError(f"Could not reach FXMacroData ({type(error).__name__}).") from Noneif response.status_code != 200:- raise RuntimeError(self._error_message(response))- return response.json()+ raise FXMacroDataError(self.status_message(response))+ try:+ return response.json()+ except ValueError:+ raise FXMacroDataError("FXMacroData returned a response that is not JSON.") from None- def run(self, params: FXMacroDataToolInputSchema) -> FXMacroDataToolOutputSchema:+ @staticmethod+ def status_message(response: requests.Response) -> str:+ if 300 <= response.status_code < 400:+ return f"FXMacroData answered with a redirect ({response.status_code}); redirects are not followed."try:- path, query = self.build_request(params)- except ValueError as exc:- return FXMacroDataToolOutputSchema(endpoint=params.endpoint, url="", error=str(exc))+ detail = upstream_detail(response.json())+ except ValueError:+ detail = ""+ return f"FXMacroData returned {response.status_code}{detail or ' ' + str(response.reason)}"++ def query_endpoint(self, params: FXMacroDataToolInputSchema) -> FXMacroDataToolOutputSchema:+ path, query = build_request(params)+ url = self.request_url(path)+ display_url = requests.Request("GET", url, params=query).prepare().url+ payload = self.fetch(url, query)+ if params.endpoint == "catalogue":+ return FXMacroDataToolOutputSchema(endpoint=params.endpoint, url=display_url, data=parse_catalogue(payload))+ rows, pagination, metadata = parse_rows(payload)+ return FXMacroDataToolOutputSchema(+ endpoint=params.endpoint, url=display_url, data=rows, pagination=pagination, metadata=metadata+ )- url = requests.Request("GET", f"{self.base_url}{path}", params=query).prepare().url+ def run(self, params: FXMacroDataToolInputSchema) -> FXMacroDataToolOutputSchema:try:- payload = self._get(path, query)- except (requests.RequestException, RuntimeError, ValueError) as exc:- return FXMacroDataToolOutputSchema(endpoint=params.endpoint, url=url, error=str(exc))-- if params.endpoint in _ROW_ENDPOINTS and isinstance(payload, dict):- metadata = {k: v for k, v in payload.items() if k not in ("data", "pagination")}- return FXMacroDataToolOutputSchema(- endpoint=params.endpoint,- url=url,- data=payload.get("data") or [],- pagination=payload.get("pagination"),- metadata=metadata,- )- return FXMacroDataToolOutputSchema(endpoint=params.endpoint, url=url, data=payload)+ return self.query_endpoint(params)+ except FXMacroDataError as error:+ return FXMacroDataToolOutputSchema(endpoint=params.endpoint, url="", error=self.redact(str(error)))async def run_async(self, params: FXMacroDataToolInputSchema) -> FXMacroDataToolOutputSchema:return await asyncio.to_thread(self.run, params)