Skip to content

Commit 4e97f0e

Browse files
authored
TimeZone SDK implementation (#36255)
1 parent d2112e4 commit 4e97f0e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+5423
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Release History
2+
3+
## 1.0.0b1 (Unreleased)
4+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
recursive-include tests *.py *.yaml
2+
recursive-include samples *.py *.md
3+
include *.md
4+
include LICENSE
5+
include azure/__init__.py
6+
include azure/maps/__init__.py
7+
include azure/maps/timezone/py.typed
Lines changed: 366 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,366 @@
1+
# Azure Maps Timezone Package client library for Python
2+
3+
This package contains a Python SDK for Azure Maps Services for Timezone.
4+
Read more about Azure Maps Services [here](https://docs.microsoft.com/azure/azure-maps/)
5+
6+
[Source code](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/maps/azure-maps-timezone) | [API reference documentation](https://docs.microsoft.com/rest/api/maps/timezone) | [Product documentation](https://docs.microsoft.com/azure/azure-maps/)
7+
8+
## _Disclaimer_
9+
10+
_Azure SDK Python packages support for Python 2.7 has ended 01 January 2022. For more information and questions, please refer to <https://github.com/Azure/azure-sdk-for-python/issues/20691>_
11+
12+
## Getting started
13+
14+
### Prerequisites
15+
16+
- Python 3.7 or later is required to use this package.
17+
- An [Azure subscription][azure_subscription] and an [Azure Maps account](https://docs.microsoft.com/azure/azure-maps/how-to-manage-account-keys).
18+
- A deployed Maps Services resource. You can create the resource via [Azure Portal][azure_portal] or [Azure CLI][azure_cli].
19+
20+
If you use Azure CLI, replace `<resource-group-name>` and `<account-name>` of your choice, and select a proper [pricing tier](https://docs.microsoft.com/azure/azure-maps/choose-pricing-tier) based on your needs via the `<sku-name>` parameter. Please refer to [this page](https://docs.microsoft.com/cli/azure/maps/account?view=azure-cli-latest#az_maps_account_create) for more details.
21+
22+
```bash
23+
az maps account create --resource-group <resource-group-name> --account-name <account-name> --sku <sku-name>
24+
```
25+
26+
### Install the package
27+
28+
Install the Azure Maps Service Timezone SDK.
29+
30+
```bash
31+
pip install azure-maps-timezone
32+
```
33+
34+
### Create and Authenticate the TimezoneClient
35+
36+
To create a client object to access the Azure Maps Timezone API, you will need a **credential** object. Azure Maps Timezone client also support two ways to authenticate.
37+
38+
#### 1. Authenticate with a Subscription Key Credential
39+
40+
You can authenticate with your Azure Maps Subscription Key.
41+
Once the Azure Maps Subscription Key is created, set the value of the key as environment variable: `AZURE_SUBSCRIPTION_KEY`.
42+
Then pass an `AZURE_SUBSCRIPTION_KEY` as the `credential` parameter into an instance of [AzureKeyCredential][azure-key-credential].
43+
44+
```python
45+
import os
46+
47+
from azure.core.credentials import AzureKeyCredential
48+
from azure.maps.timezone import TimezoneClient
49+
50+
credential = AzureKeyCredential(os.environ.get("AZURE_SUBSCRIPTION_KEY"))
51+
52+
timezone_client = TimezoneClient(
53+
credential=credential,
54+
)
55+
```
56+
57+
#### 2. Authenticate with an Microsoft Entra ID credential
58+
59+
You can authenticate with [Microsoft Entra ID credential][maps_authentication_ms_entra_id] using the [Azure Identity library][azure_identity].
60+
Authentication by using Microsoft Entra ID requires some initial setup:
61+
62+
- Install [azure-identity][azure-key-credential]
63+
- Register a [new Microsoft Entra ID application][register_ms_entra_id_app]
64+
- Grant access to Azure Maps by assigning the suitable role to your service principal. Please refer to the [Manage authentication page][manage_ms_entra_id_auth_page].
65+
66+
After setup, you can choose which type of [credential][azure-key-credential] from `azure.identity` to use.
67+
As an example, [DefaultAzureCredential][default_azure_credential]
68+
can be used to authenticate the client:
69+
70+
Next, set the values of the client ID, tenant ID, and client secret of the Microsoft Entra ID application as environment variables:
71+
`AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`
72+
73+
You will also need to specify the Azure Maps resource you intend to use by specifying the `clientId` in the client options. The Azure Maps resource client id can be found in the Authentication sections in the Azure Maps resource. Please refer to the [documentation][how_to_manage_authentication] on how to find it.
74+
75+
```python
76+
import os
77+
from azure.maps.timezone import TimezoneClient
78+
from azure.identity import DefaultAzureCredential
79+
80+
credential = DefaultAzureCredential()
81+
timezone_client = TimezoneClient(credential=credential)
82+
```
83+
84+
## Key concepts
85+
86+
The Azure Maps Timezone client library for Python allows you to interact with each of the components through the use of a dedicated client object.
87+
88+
### Sync Clients
89+
90+
`TimezoneClient` is the primary client for developers using the Azure Maps Timezone client library for Python.
91+
Once you initialized a `TimezoneClient` class, you can explore the methods on this client object to understand the different features of the Azure Maps Timezone service that you can access.
92+
93+
### Async Clients
94+
95+
This library includes a complete async API supported on Python 3.5+. To use it, you must first install an async transport, such as [aiohttp](https://pypi.org/project/aiohttp/).
96+
See [azure-core documentation](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-core/CLIENT_LIBRARY_DEVELOPER.md#transport) for more information.
97+
98+
Async clients and credentials should be closed when they're no longer needed. These objects are async context managers and define async `close` methods.
99+
100+
## Examples
101+
102+
The following sections provide several code snippets covering some of the most common Azure Maps Timezone tasks, including:
103+
104+
- [Get timezone by id](#get-timezone-by-id)
105+
- [Get timezone by coordinates](#get-timezone-by-coordinates)
106+
- [Get iana version](#get-iana-version)
107+
- [Get iana timezone ids](#get-iana-timezone-ids)
108+
- [Get windows timezone ids](#get-windows-timezone-ids)
109+
- [Convert windows timezone to iana](#convert-windows-timezone-to-iana)
110+
111+
### Get timezone by id
112+
113+
This API returns current, historical, and future time zone information for the specified IANA time zone ID.
114+
115+
```python
116+
import os
117+
118+
from azure.core.exceptions import HttpResponseError
119+
120+
subscription_key = os.getenv("AZURE_SUBSCRIPTION_KEY", "your subscription key")
121+
122+
def get_timezone_by_id():
123+
from azure.core.credentials import AzureKeyCredential
124+
from azure.maps.timezone import TimezoneClient
125+
126+
timezone_client = TimezoneClient(credential=AzureKeyCredential(subscription_key))
127+
try:
128+
result = timezone_client.get_timezone_by_id(timezone_id="sr-Latn-RS")
129+
print(result)
130+
except HttpResponseError as exception:
131+
if exception.error is not None:
132+
print(f"Error Code: {exception.error.code}")
133+
print(f"Message: {exception.error.message}")
134+
135+
if __name__ == '__main__':
136+
get_timezone_by_id()
137+
```
138+
139+
### Get timezone by coordinates
140+
141+
This API returns current, historical, and future time zone information for a specified latitude-longitude pair.
142+
143+
```python
144+
import os
145+
146+
from azure.core.exceptions import HttpResponseError
147+
148+
subscription_key = os.getenv("AZURE_SUBSCRIPTION_KEY", "your subscription key")
149+
150+
def get_timezone_by_coordinates():
151+
from azure.core.credentials import AzureKeyCredential
152+
from azure.maps.timezone import TimezoneClient
153+
154+
timezone_client = TimezoneClient(credential=AzureKeyCredential(subscription_key))
155+
try:
156+
result = timezone_client.get_timezone_by_coordinates(coordinates=[25.0338053, 121.5640089])
157+
print(result)
158+
except HttpResponseError as exception:
159+
if exception.error is not None:
160+
print(f"Error Code: {exception.error.code}")
161+
print(f"Message: {exception.error.message}")
162+
163+
if __name__ == '__main__':
164+
get_timezone_by_coordinates()
165+
```
166+
167+
### Get iana version
168+
169+
This API returns the current IANA version number as Metadata.
170+
171+
```python
172+
import os
173+
174+
from azure.core.exceptions import HttpResponseError
175+
176+
subscription_key = os.getenv("AZURE_SUBSCRIPTION_KEY", "your subscription key")
177+
178+
def get_iana_version():
179+
from azure.core.credentials import AzureKeyCredential
180+
from azure.maps.timezone import TimezoneClient
181+
182+
timezone_client = TimezoneClient(credential=AzureKeyCredential(subscription_key))
183+
try:
184+
result = timezone_client.get_iana_version()
185+
print(result)
186+
except HttpResponseError as exception:
187+
if exception.error is not None:
188+
print(f"Error Code: {exception.error.code}")
189+
print(f"Message: {exception.error.message}")
190+
191+
if __name__ == '__main__':
192+
get_iana_version()
193+
```
194+
195+
### Get iana timezone ids
196+
197+
This API returns a full list of IANA time zone IDs. Updates to the IANA service will be reflected in the system within one day.
198+
199+
```python
200+
import os
201+
202+
from azure.core.exceptions import HttpResponseError
203+
204+
subscription_key = os.getenv("AZURE_SUBSCRIPTION_KEY", "your subscription key")
205+
206+
def get_iana_timezone_ids():
207+
from azure.core.credentials import AzureKeyCredential
208+
from azure.maps.timezone import TimezoneClient
209+
210+
timezone_client = TimezoneClient(credential=AzureKeyCredential(subscription_key))
211+
try:
212+
result = timezone_client.get_iana_timezone_ids()
213+
print(result)
214+
except HttpResponseError as exception:
215+
if exception.error is not None:
216+
print(f"Error Code: {exception.error.code}")
217+
print(f"Message: {exception.error.message}")
218+
219+
if __name__ == '__main__':
220+
get_iana_timezone_ids()
221+
```
222+
223+
### Get windows timezone ids
224+
225+
This API returns a full list of Windows time zone IDs.
226+
227+
```python
228+
import os
229+
230+
from azure.core.exceptions import HttpResponseError
231+
232+
subscription_key = os.getenv("AZURE_SUBSCRIPTION_KEY", "your subscription key")
233+
234+
def get_windows_timezone_ids():
235+
from azure.core.credentials import AzureKeyCredential
236+
from azure.maps.timezone import TimezoneClient
237+
238+
timezone_client = TimezoneClient(credential=AzureKeyCredential(subscription_key))
239+
try:
240+
result = timezone_client.get_windows_timezone_ids()
241+
print(result)
242+
except HttpResponseError as exception:
243+
if exception.error is not None:
244+
print(f"Error Code: {exception.error.code}")
245+
print(f"Message: {exception.error.message}")
246+
247+
if __name__ == '__main__':
248+
get_windows_timezone_ids()
249+
```
250+
### Convert windows timezone to iana
251+
252+
This API returns a corresponding IANA ID, given a valid Windows Time Zone ID.
253+
254+
```python
255+
import os
256+
257+
from azure.core.exceptions import HttpResponseError
258+
259+
subscription_key = os.getenv("AZURE_SUBSCRIPTION_KEY", "your subscription key")
260+
261+
def convert_windows_timezone_to_iana():
262+
from azure.core.credentials import AzureKeyCredential
263+
from azure.maps.timezone import TimezoneClient
264+
265+
timezone_client = TimezoneClient(credential=AzureKeyCredential(subscription_key))
266+
try:
267+
result = timezone_client.convert_windows_timezone_to_iana(windows_timezone_id="Pacific Standard Time")
268+
print(result)
269+
except HttpResponseError as exception:
270+
if exception.error is not None:
271+
print(f"Error Code: {exception.error.code}")
272+
print(f"Message: {exception.error.message}")
273+
274+
if __name__ == '__main__':
275+
convert_windows_timezone_to_iana()
276+
```
277+
278+
## Troubleshooting
279+
280+
### General
281+
282+
Maps Timezone clients raise exceptions defined in [Azure Core](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-core/README.md).
283+
284+
This list can be used for reference to catch thrown exceptions. To get the specific error code of the exception, use the `error_code` attribute, i.e, `exception.error_code`.
285+
286+
### Logging
287+
288+
This library uses the standard [logging](https://docs.python.org/3/library/logging.html) library for logging.
289+
Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO level.
290+
291+
Detailed DEBUG level logging, including request/response bodies and unredacted headers, can be enabled on a client with the `logging_enable` argument:
292+
293+
```python
294+
import sys
295+
import logging
296+
297+
# Create a logger for the 'azure.maps.timezone' SDK
298+
logger = logging.getLogger('azure.maps.timezone')
299+
logger.setLevel(logging.DEBUG)
300+
301+
# Configure a console output
302+
handler = logging.StreamHandler(stream=sys.stdout)
303+
logger.addHandler(handler)
304+
305+
```
306+
307+
Similarly, `logging_enable` can enable detailed logging for a single operation,
308+
even when it isn't enabled for the client:
309+
310+
```python
311+
service_client.get_service_stats(logging_enable=True)
312+
```
313+
314+
### Additional
315+
316+
Still running into issues? If you encounter any bugs or have suggestions, please file an issue in the [Issues](<https://github.com/Azure/azure-sdk-for-python/issues>) section of the project.
317+
318+
## Next steps
319+
320+
### More sample code
321+
322+
Get started with our [Maps Timezone samples](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/maps/azure-maps-timezone/samples) ([Async Version samples](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/maps/azure-maps-timezone/samples/async_samples)).
323+
324+
Several Azure Maps Timezone Python SDK samples are available to you in the SDK's GitHub repository. These samples provide example code for additional scenarios commonly encountered while working with Maps Timezone
325+
326+
```bash
327+
set AZURE_SUBSCRIPTION_KEY="<RealSubscriptionKey>"
328+
329+
pip install azure-maps-timezone --pre
330+
331+
python samples/get_timezone_by_id.py
332+
python sample/get_timezone_by_coordinates.py
333+
python samples/get_iana_version.py
334+
python samples/get_iana_timezone_ids.py
335+
python samples/get_windows_timezone_ids.py
336+
python samples/convert_windows_timezone_to_iana.py
337+
```
338+
339+
> Notes: `--pre` flag can be optionally added, it is to include pre-release and development versions for `pip install`. By default, `pip` only finds stable versions.
340+
341+
Further detail please refer to [Samples Introduction](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/maps/azure-maps-timezone/samples/README.md)
342+
343+
### Additional documentation
344+
345+
For more extensive documentation on Azure Maps Timezone, see the [Azure Maps Timezone documentation](https://docs.microsoft.com/rest/api/maps/timezone) on docs.microsoft.com.
346+
347+
## Contributing
348+
349+
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit <https://cla.microsoft.com>.
350+
351+
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
352+
353+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
354+
355+
<!-- LINKS -->
356+
[azure_subscription]: https://azure.microsoft.com/free/
357+
[azure_identity]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/identity/azure-identity
358+
[azure_portal]: https://portal.azure.com
359+
[azure_cli]: https://docs.microsoft.com/cli/azure
360+
[azure-key-credential]: https://aka.ms/azsdk/python/core/azurekeycredential
361+
[default_azure_credential]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#defaultazurecredential
362+
[register_ms_entra_id_app]: https://docs.microsoft.com/powershell/module/Az.Resources/New-AzADApplication?view=azps-8.0.0
363+
[maps_authentication_ms_entra_id]: https://docs.microsoft.com/azure/azure-maps/how-to-manage-authentication
364+
[create_new_application_registration]: https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/applicationsListBlade/quickStartType/AspNetWebAppQuickstartPage/sourceType/docs
365+
[manage_ms_entra_id_auth_page]: https://docs.microsoft.com/azure/azure-maps/how-to-manage-authentication
366+
[how_to_manage_authentication]: https://docs.microsoft.com/azure/azure-maps/how-to-manage-authentication#view-authentication-details
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"AssetsRepo": "Azure/azure-sdk-assets",
3+
"AssetsRepoPrefixPath": "python",
4+
"TagPrefix": "python/maps/azure-maps-timezone",
5+
"Tag": "python/maps/azure-maps-timezone_c152685219"
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__path__ = __import__("pkgutil").extend_path(__path__, __name__)

0 commit comments

Comments
 (0)