Skip to content

Commit f49f021

Browse files
authored
[Azure Maps] - Onboarding for Render SDK (#24893)
* init PR * Update samples * Add Unit/Live test Update testresopurce.json Update Minor Update * Update according to feedback * Code clean up * Update API to the newest version * Update to fix CI issue * Add recording files * update according to comment * Update docstrings * udpate accordingly * Update according to feedback * update minors according to feedback * update release date
1 parent c0efabb commit f49f021

File tree

73 files changed

+13827
-5
lines changed

Some content is hidden

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

73 files changed

+13827
-5
lines changed

sdk/maps/azure-maps-geolocation/swagger/README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ require: https://raw.githubusercontent.com/Azure/azure-rest-api-specs/main/speci
2525
output-folder: ../azure/maps/geolocation/_generated
2626
namespace: azure.maps.geolocation
2727
no-namespace-folders: true
28-
use-extension:
29-
"@autorest/modelerfour": "4.22.3"
30-
3128
license-header: MICROSOFT_MIT_NO_VERSION
3229
enable-xml: true
3330
vanilla: true
@@ -37,3 +34,11 @@ python3-only: true
3734
version-tolerant: true
3835
models-mode: msrest
3936
```
37+
38+
```yaml
39+
directive:
40+
- from: swagger-document
41+
where: $.securityDefinitions
42+
transform: |
43+
$["SharedKey"]["in"] = "header";
44+
```
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Release History
2+
3+
## 1.0.0b1 (2022-10-13)
4+
5+
### Features Added
6+
7+
- Initial release
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
recursive-include tests *.py *.yaml
2+
recursive-include samples *.py *.md
3+
include *.md
4+
include azure/__init__.py
5+
include azure/maps/__init__.py
6+
include azure/maps/render/py.typed

sdk/maps/azure-maps-render/README.md

+273
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
# Azure Maps Render Package client library for Python
2+
3+
This package contains a Python SDK for Azure Maps Services for Render.
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-render) | [API reference documentation](https://docs.microsoft.com/rest/api/maps/render) | [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 Render SDK.
29+
30+
```bash
31+
pip install azure-maps-render
32+
```
33+
34+
### Create and Authenticate the MapsRenderClient
35+
36+
To create a client object to access the Azure Maps Render API, you will need a **credential** object. Azure Maps Render 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+
from azure.core.credentials import AzureKeyCredential
46+
from azure.maps.render import MapsRenderClient
47+
48+
credential = AzureKeyCredential(os.environ.get("AZURE_SUBSCRIPTION_KEY"))
49+
50+
render_client = MapsRenderClient(
51+
credential=credential,
52+
)
53+
```
54+
55+
#### 2. Authenticate with an Azure Active Directory credential
56+
57+
You can authenticate with [Azure Active Directory (AAD) token credential][maps_authentication_aad] using the [Azure Identity library][azure_identity].
58+
Authentication by using AAD requires some initial setup:
59+
60+
- Install [azure-identity][azure-key-credential]
61+
- Register a [new AAD application][register_aad_app]
62+
- Grant access to Azure Maps by assigning the suitable role to your service principal. Please refer to the [Manage authentication page][manage_aad_auth_page].
63+
64+
After setup, you can choose which type of [credential][azure-key-credential] from `azure.identity` to use.
65+
As an example, [DefaultAzureCredential][default_azure_credential]
66+
can be used to authenticate the client:
67+
68+
Next, set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables:
69+
`AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`
70+
71+
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.
72+
73+
```python
74+
from azure.maps.render import MapsRenderClient
75+
from azure.identity import DefaultAzureCredential
76+
77+
credential = DefaultAzureCredential()
78+
render_client = MapsRenderClient(
79+
client_id="<Azure Maps Client ID>",
80+
credential=credential
81+
)
82+
```
83+
84+
## Key concepts
85+
86+
The Azure Maps Render 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+
`MapsRenderClient` is the primary client for developers using the Azure Maps Render client library for Python.
91+
Once you initialized a `MapsRenderClient` class, you can explore the methods on this client object to understand the different features of the Azure Maps Render 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
99+
objects are async context managers and define async `close` methods.
100+
101+
## Examples
102+
103+
The following sections provide several code snippets covering some of the most common Azure Maps Render tasks, including:
104+
105+
- [Get Maps Attribution](#get-maps-attribution)
106+
- [Get Maps Static Image](#get-maps-static-image)
107+
- [Get Maps Tile](#get-maps-tile)
108+
- [Get Maps Tileset](#get-maps-tileset)
109+
- [Get Maps Copyright for World](#get-maps-copyright-for-world)
110+
111+
### Get Maps Attribution
112+
113+
This request allows users to request map copyright attribution information for a
114+
section of a tileset.
115+
116+
```python
117+
from azure.maps.render import MapsRenderClient
118+
119+
result = maps_render_client.get_map_attribution(
120+
tileset_id=TilesetID.MICROSOFT_BASE,
121+
zoom=6,
122+
bounds=BoundingBox(
123+
south=42.982261,
124+
west=24.980233,
125+
north=56.526017,
126+
east=1.355233
127+
)
128+
)
129+
```
130+
131+
### Get Maps Tile
132+
133+
This request will return map tiles in vector or raster formats typically
134+
to be integrated into a map control or SDK. Some example tiles that can be requested are Azure
135+
Maps road tiles, real-time Weather Radar tiles. By default, Azure Maps uses vector tiles for its web map
136+
control (Web SDK) and Android SDK.
137+
138+
```python
139+
from azure.maps.render import MapsRenderClient
140+
141+
result = maps_render_client.get_map_tile(
142+
tileset_id=TilesetID.MICROSOFT_BASE,
143+
z=6,
144+
x=9,
145+
y=22,
146+
tile_size="512"
147+
)
148+
```
149+
150+
### Get Maps Tileset
151+
152+
This request will give metadata for a tileset.
153+
154+
```python
155+
from azure.maps.render import MapsRenderClient
156+
157+
result = maps_render_client.get_map_tileset(tileset_id=TilesetID.MICROSOFT_BASE)
158+
```
159+
160+
### Get Maps Static Image
161+
162+
This request will provide the static image service renders a user-defined, rectangular image containing a map section
163+
using a zoom level from 0 to 20.
164+
The static image service renders a user-defined,
165+
rectangular image containing a map section using a zoom level from 0 to 20.
166+
And also save the result to file as png.
167+
168+
```python
169+
from azure.maps.render import MapsRenderClient
170+
171+
result = maps_render_client.get_map_static_image(img_format="png", center=(52.41064,4.84228))
172+
# Save result to file as png
173+
file = open('result.png', 'wb')
174+
file.write(next(result))
175+
file.close()
176+
```
177+
178+
### Get Maps Copyright for World
179+
180+
This request will serve copyright information for Render Tile service.
181+
182+
```python
183+
from azure.maps.render import MapsRenderClient
184+
185+
result = maps_render_client.get_copyright_for_world()
186+
```
187+
188+
## Troubleshooting
189+
190+
### General
191+
192+
Maps Render clients raise exceptions defined in [Azure Core](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-core/README.md).
193+
194+
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`.
195+
196+
### Logging
197+
198+
This library uses the standard [logging](https://docs.python.org/3/library/logging.html) library for logging.
199+
Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO level.
200+
201+
Detailed DEBUG level logging, including request/response bodies and unredacted headers, can be enabled on a client with the `logging_enable` argument:
202+
203+
```python
204+
import sys
205+
import logging
206+
from azure.maps.render import MapsRenderClient
207+
208+
# Create a logger for the 'azure.maps.render' SDK
209+
logger = logging.getLogger('azure.maps.render')
210+
logger.setLevel(logging.DEBUG)
211+
212+
# Configure a console output
213+
handler = logging.StreamHandler(stream=sys.stdout)
214+
logger.addHandler(handler)
215+
216+
```
217+
218+
### Additional
219+
220+
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.
221+
222+
## Next steps
223+
224+
### More sample code
225+
226+
Get started with our [Maps Render samples](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/maps/azure-maps-render/samples) ([Async Version samples](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/maps/azure-maps-render/samples/async_samples)).
227+
228+
Several Azure Maps Render 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 Render
229+
230+
```bash
231+
set AZURE_SUBSCRIPTION_KEY="<RealSubscriptionKey>"
232+
233+
pip install azure-maps-render --pre
234+
235+
python samples/sample_authentication.py
236+
python sample/sample_get_copyright_caption.py
237+
python sample/sample_get_copyright_for_tile.py
238+
python sample/sample_get_copyright_for_world.py
239+
python sample/sample_get_copyright_from_bounding_box.py
240+
python sample/sample_get_map_attribution.py
241+
python sample/sample_get_map_static_image.py
242+
python sample/sample_get_map_tile.py
243+
python sample/sample_get_map_tileset.py
244+
```
245+
246+
> 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.
247+
248+
Further detail please refer to [Samples Introduction](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/maps/azure-maps-render/samples/README.md)
249+
250+
### Additional documentation
251+
252+
For more extensive documentation on Azure Maps Render, see the [Azure Maps Render documentation](https://docs.microsoft.com/rest/api/maps/render) on docs.microsoft.com.
253+
254+
## Contributing
255+
256+
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>.
257+
258+
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.
259+
260+
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.
261+
262+
<!-- LINKS -->
263+
[azure_subscription]: https://azure.microsoft.com/free/
264+
[azure_identity]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/identity/azure-identity
265+
[azure_portal]: https://portal.azure.com
266+
[azure_cli]: https://docs.microsoft.com/cli/azure
267+
[azure-key-credential]: https://aka.ms/azsdk/python/core/azurekeycredential
268+
[default_azure_credential]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#defaultazurecredential
269+
[register_aad_app]: https://docs.microsoft.com/powershell/module/Az.Resources/New-AzADApplication?view=azps-8.0.0
270+
[maps_authentication_aad]: https://docs.microsoft.com/azure/azure-maps/how-to-manage-authentication
271+
[create_new_application_registration]: https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/applicationsListBlade/quickStartType/AspNetWebAppQuickstartPage/sourceType/docs
272+
[manage_aad_auth_page]: https://docs.microsoft.com/azure/azure-maps/how-to-manage-authentication
273+
[how_to_manage_authentication]: https://docs.microsoft.com/azure/azure-maps/how-to-manage-authentication#view-authentication-details
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# coding=utf-8
2+
# --------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See License.txt in the project root for license information.
5+
# Code generated by Microsoft (R) AutoRest Code Generator.
6+
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
7+
# --------------------------------------------------------------------------
8+
9+
from ._render_client import MapsRenderClient
10+
from ._version import VERSION
11+
12+
__version__ = VERSION
13+
__all__ = ['MapsRenderClient']
14+
15+
try:
16+
from ._patch import patch_sdk # type: ignore
17+
patch_sdk()
18+
except ImportError:
19+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
6+
from typing import Union, Any
7+
from azure.core.pipeline.policies import AzureKeyCredentialPolicy
8+
from azure.core.credentials import AzureKeyCredential, TokenCredential
9+
from ._generated import MapsRenderClient as _MapsRenderClient
10+
from ._version import API_VERSION
11+
12+
# To check the credential is either AzureKeyCredential or TokenCredential
13+
def _authentication_policy(credential):
14+
authentication_policy = None
15+
if credential is None:
16+
raise ValueError("Parameter 'credential' must not be None.")
17+
if isinstance(credential, AzureKeyCredential):
18+
authentication_policy = AzureKeyCredentialPolicy(
19+
name="subscription-key", credential=credential
20+
)
21+
elif credential is not None and not hasattr(credential, "get_token"):
22+
raise TypeError(
23+
"Unsupported credential: {}. Use an instance of AzureKeyCredential "
24+
"or a token credential from azure.identity".format(type(credential))
25+
)
26+
return authentication_policy
27+
28+
class MapsRenderClientBase:
29+
def __init__(
30+
self,
31+
credential: Union[AzureKeyCredential, TokenCredential],
32+
**kwargs: Any
33+
) -> None:
34+
35+
self._maps_client = _MapsRenderClient(
36+
credential=credential, # type: ignore
37+
api_version=kwargs.pop("api_version", API_VERSION),
38+
authentication_policy=kwargs.pop("authentication_policy", _authentication_policy(credential)),
39+
**kwargs
40+
)
41+
self._render_client = self._maps_client.render
42+
43+
def __enter__(self):
44+
self._maps_client.__enter__() # pylint:disable=no-member
45+
return self
46+
47+
def __exit__(self, *args):
48+
self._maps_client.__exit__(*args) # pylint:disable=no-member

0 commit comments

Comments
 (0)