Skip to content

Commit

Permalink
Merge pull request #12 from kaymal/add-key-header
Browse files Browse the repository at this point in the history
Remove api-key from query params and add to request headers.
  • Loading branch information
kaymal authored Apr 14, 2024
2 parents 2088292 + 3181c9c commit f1323c3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
6 changes: 5 additions & 1 deletion tcmb/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ def check_api_key(api_key: str | None = None) -> bool:
if api_key is None:
raise ApiKeyError("No API key provided.")

headers = {"key": api_key}

res = requests.get(
f"https://evds2.tcmb.gov.tr/service/evds/categories/key={api_key}"
"https://evds2.tcmb.gov.tr/service/evds/categories/type=json",
headers=headers,
timeout=30,
)
# check if authenticated
check_status(res)
Expand Down
34 changes: 22 additions & 12 deletions tcmb/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def read(
API key of the user. If None, environment variable with
the name "TCMB_API_KEY" will be used to as the api_key. If
the api_key argument is not passed or it is not exported to the
environment, ApiKeyError is raised.
environment, ApiKeyError is raised. EVDS Web Service accepts API Key
as a request header in the format {"key": api_key}.
**kwargs:
Keyword arguments passed to the request.
Expand Down Expand Up @@ -139,13 +140,18 @@ def read(
"endDate": end or date.today().strftime("%d-%m-%Y"),
"type": "json", # csv, xml, json
"aggregationTypes": agg,
"key": api_key,
"formulas": formulas,
"frequency": freq,
"decimalSeperator": seperator,
**kwargs,
}

# add api_key to request header
if headers is None:
headers = {"key": api_key}
elif "key" not in headers:
headers["key"] = api_key

res = fetch.get_response(params=params, headers=headers)

# convert response JSON to DataFrame
Expand Down Expand Up @@ -303,7 +309,7 @@ def read(
# it can be accessed using attrs: e.g. "df.attrs"
if metadata:
attrs: dict[str, dict] = {}

# TODO: update for the case of list of series
for series_item in series.split("-"):
attrs[series_item] = self.get_series_metadata(series_item)

Expand All @@ -313,12 +319,12 @@ def read(

def get_categories_metadata(self):
"""Get the list of the metadata of all categories."""
params = {
"type": "json",
"key": self.api_key,
}
params = {"type": "json"}

# add api_key to request header
headers = {"key": self.api_key}

res = self._get_response(params=params, endpoint="categories")
res = self._get_response(params=params, endpoint="categories", headers=headers)

return res.json()

Expand Down Expand Up @@ -356,10 +362,12 @@ def get_datagroups_metadata(
"mode": str(mode),
"code": code,
"type": "json",
"key": self.api_key,
}

res = self._get_response(params=params, endpoint="datagroups")
# add api_key to request header
headers = {"key": self.api_key}

res = self._get_response(params=params, endpoint="datagroups", headers=headers)

json_data = res.json()

Expand Down Expand Up @@ -405,10 +413,12 @@ def get_series_metadata(
params = {
"code": series or datagroup,
"type": "json",
"key": self.api_key,
}

res = self._get_response(params=params, endpoint="serieList")
# add api_key to request header
headers = {"key": self.api_key}

res = self._get_response(params=params, endpoint="serieList", headers=headers)

return res.json()

Expand Down

0 comments on commit f1323c3

Please sign in to comment.