Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions nbs/src/nixtla_client.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1000,20 +1000,28 @@
" return df, X_df, drop_id, freq\n",
"\n",
" def validate_api_key(self, log: bool = True) -> bool:\n",
" \"\"\"Returns True if your api_key is valid.\"\"\"\n",
" try:\n",
" with httpx.Client(**self._client_kwargs) as client:\n",
" validation = self._make_request_with_retries(\n",
" client, 'validate_token', {}\n",
" )\n",
" except:\n",
" validation = {}\n",
" if 'support' in validation and log:\n",
" logger.info(f'Happy Forecasting! :), {validation[\"support\"]}')\n",
" return (\n",
" validation.get('message', '') == 'success'\n",
" or 'Forecasting! :)' in validation.get('detail', '')\n",
" )\n",
" \"\"\"Check API key status.\n",
" \n",
" Parameters\n",
" ----------\n",
" log : bool (default=True)\n",
" Show the endpoint's response.\n",
" \n",
" Returns\n",
" -------\n",
" bool\n",
" Whether API key is valid.\"\"\"\n",
" if self._is_azure:\n",
" raise NotImplementedError(\n",
" 'validate_api_key is not implemented for Azure deployments, '\n",
" 'you can try using the forecasting methods directly.'\n",
" )\n",
" with httpx.Client(**self._client_kwargs) as client:\n",
" resp = client.get(\"/validate_api_key\")\n",
" body = resp.json()\n",
" if log:\n",
" logger.info(body[\"detail\"])\n",
" return resp.status_code == 200\n",
"\n",
" def usage(self) -> dict[str, dict[str, int]]:\n",
" \"\"\"Query consumed requests and limits\n",
Expand Down Expand Up @@ -2465,7 +2473,7 @@
"outputs": [],
"source": [
"#| hide\n",
"nixtla_client.validate_api_key()"
"assert nixtla_client.validate_api_key()"
]
},
{
Expand All @@ -2479,7 +2487,8 @@
"custom_client = NixtlaClient(\n",
" base_url=os.environ['NIXTLA_BASE_URL_CUSTOM'],\n",
" api_key=os.environ['NIXTLA_API_KEY_CUSTOM'],\n",
")"
")\n",
"assert custom_client.validate_api_key()"
]
},
{
Expand Down
35 changes: 22 additions & 13 deletions nixtla/nixtla_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,19 +928,28 @@ def _run_validations(
return df, X_df, drop_id, freq

def validate_api_key(self, log: bool = True) -> bool:
"""Returns True if your api_key is valid."""
try:
with httpx.Client(**self._client_kwargs) as client:
validation = self._make_request_with_retries(
client, "validate_token", {}
)
except:
validation = {}
if "support" in validation and log:
logger.info(f'Happy Forecasting! :), {validation["support"]}')
return validation.get(
"message", ""
) == "success" or "Forecasting! :)" in validation.get("detail", "")
"""Check API key status.

Parameters
----------
log : bool (default=True)
Show the endpoint's response.

Returns
-------
bool
Whether API key is valid."""
if self._is_azure:
raise NotImplementedError(
"validate_api_key is not implemented for Azure deployments, "
"you can try using the forecasting methods directly."
)
with httpx.Client(**self._client_kwargs) as client:
resp = client.get("/validate_api_key")
body = resp.json()
if log:
logger.info(body["detail"])
return resp.status_code == 200

def usage(self) -> dict[str, dict[str, int]]:
"""Query consumed requests and limits
Expand Down
Loading