Skip to content

Commit

Permalink
v0.7.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Jul 10, 2024
1 parent fd23674 commit fbcf785
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="tesla_fleet_api",
version="0.7.0",
version="0.7.1",
author="Brett Adams",
author_email="hello@teslemetry.com",
description="Tesla Fleet API library for Python",
Expand Down
2 changes: 1 addition & 1 deletion tesla_fleet_api/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from enum import Enum
import logging

VERSION = "0.7.0"
VERSION = "0.7.1"
LOGGER = logging.getLogger(__package__)
SERVERS = {
"na": "https://fleet-api.prd.na.vn.cloud.tesla.com",
Expand Down
7 changes: 6 additions & 1 deletion tesla_fleet_api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,12 @@ async def raise_for_status(resp: aiohttp.ClientResponse) -> None:
elif resp.status == 424:
raise InvalidResponse(data)
elif resp.status == 429:
raise RateLimited(data)
raise RateLimited(
{
"reset": resp.headers.get("RateLimit-Reset"),
"after": resp.headers.get("Retry-After"),
}
)
elif resp.status == 451:
raise ResourceUnavailableForLegalReasons(data)
elif resp.status == 499:
Expand Down
17 changes: 16 additions & 1 deletion tesla_fleet_api/teslafleetapi.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Tesla Fleet API for Python."""

from json import dumps
from typing import Any
from typing import Any, Awaitable
import aiohttp

from .exceptions import raise_for_status, InvalidRegion, LibraryError, ResponseError
Expand All @@ -22,6 +22,7 @@ class TeslaFleetApi:
server: str | None = None
session: aiohttp.ClientSession
headers: dict[str, str]
refresh_hook: Awaitable | None

def __init__(
self,
Expand All @@ -34,11 +35,13 @@ def __init__(
partner_scope: bool = True,
user_scope: bool = True,
vehicle_scope: bool = True,
refresh_hook: Awaitable | None = None,
):
"""Initialize the Tesla Fleet API."""

self.session = session
self.access_token = access_token
self.refresh_hook = refresh_hook

if server is not None:
self.server = server
Expand Down Expand Up @@ -90,6 +93,10 @@ async def _request(
if method == Method.GET and json is not None:
raise ValueError("GET requests cannot have a body.")

# Call a pre-request hook if provided
if self.refresh_hook is not None:
await self.refresh_hook()

LOGGER.debug("Sending request to %s", path)

# Remove None values from params and json
Expand All @@ -114,8 +121,16 @@ async def _request(
LOGGER.debug("Response Status: %s", resp.status)
if "x-txid" in resp.headers:
LOGGER.debug("Response TXID: %s", resp.headers["x-txid"])
if "RateLimit-Reset" in resp.headers:
LOGGER.debug(
"Rate limit reset: %s", resp.headers.get("RateLimit-Reset")
)
if "Retry-After" in resp.headers:
LOGGER.debug("Retry after: %s", resp.headers.get("Retry-After"))

if not resp.ok:
await raise_for_status(resp)

if not resp.content_type.lower().startswith("application/json"):
LOGGER.debug("Response type is: %s", resp.content_type)
raise ResponseError(status=resp.status, data=await resp.text())
Expand Down

0 comments on commit fbcf785

Please sign in to comment.