|
| 1 | +from abstract_api.bases import BaseService |
| 2 | +from abstract_api.exceptions import ResponseParseError |
| 3 | + |
| 4 | +from .current_timezone_response import CurrentTimezoneResponse |
| 5 | +from .timezone_conversion_response import TimezoneConversionResponse |
| 6 | + |
| 7 | + |
| 8 | +class Timezone(BaseService): |
| 9 | + """AbstractAPI timezone service. |
| 10 | +
|
| 11 | + Used to find, convert, and manage time and timezone data across the world. |
| 12 | +
|
| 13 | + Attributes: |
| 14 | + _subdomain: timezone service subdomain. |
| 15 | + """ |
| 16 | + _subdomain: str = "timezone" |
| 17 | + |
| 18 | + @staticmethod |
| 19 | + def _location_as_param( |
| 20 | + location: str | list[float] | tuple[float, ...] |
| 21 | + ) -> str: |
| 22 | + """Converts location to a request query parameter value. |
| 23 | +
|
| 24 | + This method converts the location from list/tuple of floats to a |
| 25 | + string if the location given is a list/tuple. |
| 26 | + It does nothing if the given location is already a string. |
| 27 | +
|
| 28 | + Args: |
| 29 | + location: Location to be converted. |
| 30 | +
|
| 31 | + Returns: |
| 32 | + A string with location as query parameter value. |
| 33 | + """ |
| 34 | + if isinstance(location, list) or isinstance(location, tuple): |
| 35 | + location = ",".join(map(str, location)) |
| 36 | + |
| 37 | + return location |
| 38 | + |
| 39 | + def current( |
| 40 | + self, |
| 41 | + location: str | list[float] | tuple[float, ...] |
| 42 | + ) -> CurrentTimezoneResponse: |
| 43 | + """Finds the current time, date, and timezone of a given location. |
| 44 | +
|
| 45 | + Args: |
| 46 | + location: The location to determine the current time and |
| 47 | + timezone of. This parameter accepts the location as |
| 48 | + a string (e.g., Los Angeles, CA), |
| 49 | + a longitude and latitude (e.g., -31.4173391,-64.183319), |
| 50 | + or an IP address (e.g., 82.111.111.111). |
| 51 | +
|
| 52 | + Returns: |
| 53 | + CurrentTimezoneResponse representing API call response. |
| 54 | + """ |
| 55 | + response = self._service_request( |
| 56 | + action="current_time", |
| 57 | + location=self._location_as_param(location) |
| 58 | + ) |
| 59 | + |
| 60 | + try: |
| 61 | + current_timezone_response = CurrentTimezoneResponse( |
| 62 | + response=response |
| 63 | + ) |
| 64 | + except Exception as e: |
| 65 | + raise ResponseParseError( |
| 66 | + "Failed to parse response as CurrentTimezoneResponse" |
| 67 | + ) from e |
| 68 | + |
| 69 | + return current_timezone_response |
| 70 | + |
| 71 | + def convert( |
| 72 | + self, |
| 73 | + base_location: str | list[float] | tuple[float, ...], |
| 74 | + target_location: str | list[float] | tuple[float, ...], |
| 75 | + base_datetime: str | None = None |
| 76 | + ) -> TimezoneConversionResponse: |
| 77 | + """Converts datetime of base location to target location's datetime. |
| 78 | +
|
| 79 | + By default, it converts the current time, but the conversion can |
| 80 | + take place in either the past or future with a simple parameter. |
| 81 | +
|
| 82 | + Args: |
| 83 | + base_location: The location you use as a base to convert the |
| 84 | + datetime for. This parameter accepts the location as |
| 85 | + a string (e.g., Los Angeles, CA), |
| 86 | + a longitude and latitude (e.g., -31.4173391,-64.183319), |
| 87 | + or an IP address (e.g., 82.111.111.111). |
| 88 | + target_location: The location you want to get the datetime for. |
| 89 | + This parameter accepts the location as |
| 90 | + a string (e.g., Los Angeles, CA), |
| 91 | + a longitude and latitude (e.g., -31.4173391,-64.183319), |
| 92 | + or an IP address (e.g., 82.111.111.111). |
| 93 | + base_datetime: The datetime you’re converting. |
| 94 | +
|
| 95 | + Returns: |
| 96 | + TimezoneConversionResponse representing API call response. |
| 97 | + """ |
| 98 | + response = self._service_request( |
| 99 | + action="convert_time", |
| 100 | + base_location=self._location_as_param(base_location), |
| 101 | + target_location=self._location_as_param(target_location), |
| 102 | + base_datetime=base_datetime |
| 103 | + ) |
| 104 | + |
| 105 | + try: |
| 106 | + timezone_conversion_response = TimezoneConversionResponse( |
| 107 | + response=response |
| 108 | + ) |
| 109 | + except Exception as e: |
| 110 | + raise ResponseParseError( |
| 111 | + "Failed to parse response as TimezoneConversionResponse" |
| 112 | + ) from e |
| 113 | + |
| 114 | + return timezone_conversion_response |
0 commit comments