from typing import Union
from datetime import datetime
import pytz


def api_timestamp_to_datetime(api_dt: Union[str, dict]):
    """Convertes the datetime string returned by the API to python datetime object"""

    """
    Somehow this string is formatted with 7 digits for 'microsecond' resolution, so crop the last digit (and trailing Z)
    The cropped string will be written into api_dt_str_mod 
    """
    api_dt_str_mod = None

    if isinstance(api_dt, str):
        api_dt_str_mod = api_dt[:-2]
    elif isinstance(api_dt, dict):
        api_dt_str_mod = api_dt["dateTime"][:-2]
    else:
        raise

    dt = datetime.strptime(api_dt_str_mod, "%Y-%m-%dT%H:%M:%S.%f")
    dt = pytz.utc.localize(dt)

    return dt