Skip to content

Commit

Permalink
Merge pull request #58 from JohnPaton/request-token
Browse files Browse the repository at this point in the history
request URLs with custom header
  • Loading branch information
avaldebe authored Sep 3, 2024
2 parents 6043d71 + 98b5e6f commit 95a1378
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion airbase/airbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
fetch_to_file,
fetch_unique_lines,
)
from .resources import CURRENT_YEAR, METADATA_URL
from .resources import CURRENT_YEAR, LIST_URL_HEADERS, METADATA_URL
from .summary import DB
from .util import link_list_url, string_safe_list

Expand Down Expand Up @@ -323,6 +323,7 @@ def _get_csv_links(self, force: bool = False) -> None:
self._download_links,
progress=self.verbose,
encoding="utf-8-sig",
headers=LIST_URL_HEADERS,
)

# list of links (no duplicates)
Expand Down
8 changes: 6 additions & 2 deletions airbase/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def fetcher(
progress: bool = DEFAULT.progress,
raise_for_status: bool = DEFAULT.raise_for_status,
max_concurrent: int = DEFAULT.max_concurrent,
headers: dict[str, str] | None = None,
) -> AsyncIterator[str]: # pragma: no cover
...

Expand All @@ -89,6 +90,7 @@ def fetcher(
progress: bool = DEFAULT.progress,
raise_for_status: bool = DEFAULT.raise_for_status,
max_concurrent: int = DEFAULT.max_concurrent,
headers: dict[str, str] | None = None,
) -> AsyncIterator[Path]: # pragma: no cover
...

Expand All @@ -100,6 +102,7 @@ async def fetcher(
progress: bool = DEFAULT.progress,
raise_for_status: bool = DEFAULT.raise_for_status,
max_concurrent: int = DEFAULT.max_concurrent,
headers: dict[str, str] | None = None,
) -> AsyncIterator[str | Path]:
"""Request multiple urls and write request text into individual paths
it a `dict[url, path]` is provided, or return the decoded text from each request
Expand All @@ -115,8 +118,7 @@ async def fetcher(
:return: url text or path to downloaded text, one by one as the requests are completed
"""

async with aiohttp.ClientSession() as session:
async with aiohttp.ClientSession(headers=headers) as session:
semaphore = asyncio.Semaphore(max_concurrent)

async def fetch(url: str) -> str:
Expand Down Expand Up @@ -162,6 +164,7 @@ def fetch_unique_lines(
progress: bool = DEFAULT.progress,
raise_for_status: bool = DEFAULT.raise_for_status,
max_concurrent: int = DEFAULT.max_concurrent,
headers: dict[str, str] | None = None,
) -> set[str]:
"""Request a list of url and return only the unique lines among all the responses
Expand All @@ -184,6 +187,7 @@ async def fetch() -> set[str]:
progress=progress,
raise_for_status=raise_for_status,
max_concurrent=max_concurrent,
headers=headers,
):
lines.update(text.splitlines())
return lines
Expand Down
7 changes: 6 additions & 1 deletion airbase/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
E1A_SUMMARY_URL = "http://discomap.eea.europa.eu/map/fme/E1a/summaryE1a.js"

LINK_LIST_URL_TEMPLATE = (
"http://fme.discomap.eea.europa.eu/fmedatastreaming/"
"https://fme.discomap.eea.europa.eu/fmedatastreaming/"
"AirQualityDownload/AQData_Extract.fmw"
"?CountryCode={country}"
"&CityName="
Expand All @@ -18,6 +18,11 @@
"&Output=TEXT"
"&UpdateDate={update_date}"
)
FME_TOKEN = "8f3a54b3e7054080813237004b35694fbff43580"
LIST_URL_HEADERS = {
"Authorization": f"fmetoken token={FME_TOKEN}",
"Content-Type": "application/json",
}

METADATA_URL = (
"http://discomap.eea.europa.eu/map/fme/metadata/PanEuropean_metadata.csv"
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def csv_links_response(response: aioresponses):
"""mock response from station data links url"""
response.get(
re.compile(
r"http://fme\.discomap\.eea\.europa\.eu/fmedatastreaming/"
r"https://fme\.discomap\.eea\.europa\.eu/fmedatastreaming/"
r"AirQualityDownload/AQData_Extract\.fmw.*"
),
body=resources.CSV_LINKS_RESPONSE_TEXT,
Expand Down

0 comments on commit 95a1378

Please sign in to comment.