The Water Data API client reads the API key exclusively from the API_USGS_PAT environment variable:
token = os.getenv("API_USGS_PAT")
if token:
headers["X-Api-Key"] = token
Applications that obtain credentials from configuration files, secret stores, or dependency injection must therefore modify os.environ at runtime. Because environment variables are process‑global, this can cause problems for concurrent applications, tests, and programs that need different credentials.
Proposed API
Could the public waterdata functions accept a configured requests.Session?
with requests.Session() as session:
session.headers["X-Api-Key"] = api_key
data, metadata = waterdata.get_continuous(
monitoring_location_id="USGS-...",
session=session,
)
An explicit convenience parameter would also be useful:
data, metadata = waterdata.get_continuous(
monitoring_location_id="USGS-...",
api_key=api_key,
)
The environment variable could remain as a backward‑compatible fallback.
The package’s internal _walk_pages function already accepts a requests.Session, but this capability is not exposed through get_ogc_data or the other public endpoint functions. When supporting sessions, requests may need to be prepared with session.prepare_request(request) so that session‑level headers apply to both the initial request and subsequent paginated requests.
A session‑based API would additionally enable:
- connection pooling
- custom retries
- proxies
- certificates
- test adapters
Would this approach fit the library’s intended API?
The Water Data API client reads the API key exclusively from the
API_USGS_PATenvironment variable:Applications that obtain credentials from configuration files, secret stores, or dependency injection must therefore modify
os.environat runtime. Because environment variables are process‑global, this can cause problems for concurrent applications, tests, and programs that need different credentials.Proposed API
Could the public
waterdatafunctions accept a configuredrequests.Session?An explicit convenience parameter would also be useful:
The environment variable could remain as a backward‑compatible fallback.
The package’s internal
_walk_pagesfunction already accepts arequests.Session, but this capability is not exposed throughget_ogc_dataor the other public endpoint functions. When supporting sessions, requests may need to be prepared withsession.prepare_request(request)so that session‑level headers apply to both the initial request and subsequent paginated requests.A session‑based API would additionally enable:
Would this approach fit the library’s intended API?