Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 95917da

Browse files
committed
added API management sample
1 parent f4988be commit 95917da

File tree

2 files changed

+109
-3
lines changed

2 files changed

+109
-3
lines changed

‎README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ In the provided notebook you can find samples on how to invoke an [Azure Functio
1818

1919
### [Azure Event Hubs](./azure-event-hubs.ipynb)
2020

21-
The sample notebook contains samples on how to send messages to [Azure Event Hubs](https://learn.microsoft.com/en-us/azure/event-hubs/event-hubs-about):
21+
The notebook contains samples on how to send messages to [Azure Event Hubs](https://learn.microsoft.com/en-us/azure/event-hubs/event-hubs-about):
2222

2323
- Send events using a SAS Token
2424
- Send events using a Managed Identity
@@ -43,9 +43,11 @@ Work in progress
4343

4444
Work in progress
4545

46-
### [Azure API Management]
46+
### [Azure API Management](./azure-api-management.ipynb)
4747

48-
Work in progress
48+
The notebook contains samples on how to invoke an HTTPS REST endpoint no matter if it is residing in Azure or in any other cloud or hosting platform, by securely publishing and API using [Azure API Management](https://learn.microsoft.com/en-us/azure/api-management/)
49+
50+
- Call an external REST endpoint using API Management
4951

5052
## Contribute
5153

‎azure-api-management.ipynb

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{
2+
"metadata": {
3+
"kernelspec": {
4+
"name": "SQL",
5+
"display_name": "SQL",
6+
"language": "sql"
7+
},
8+
"language_info": {
9+
"name": "sql",
10+
"version": ""
11+
}
12+
},
13+
"nbformat_minor": 2,
14+
"nbformat": 4,
15+
"cells": [
16+
{
17+
"cell_type": "markdown",
18+
"source": [
19+
"# Call an REST API published via API Management\r\n",
20+
"\r\n",
21+
"Make sure you have create an API Management instance. If you need help, follow the Getting Started guide here: [Create a new Azure API Management service instance by using the Azure portal](https://learn.microsoft.com/en-us/azure/api-management/get-started-create-service-instance)\r\n",
22+
"\r\n",
23+
"As an example of an API not residing in Azure that you can call, you may use the [NIST Certified cryptographically secure random number generator](https://csrng.net/) API, that are available here: https://csrng.net/csrng/csrng.php\r\n",
24+
"\r\n",
25+
"For example: https://csrng.net/csrng/csrng.php?min=0&max=100\r\n",
26+
"\r\n",
27+
"will return the something like:\r\n",
28+
"\r\n",
29+
"```\r\n",
30+
"[{\"status\":\"success\",\"min\":0,\"max\":100,\"random\":99}]\r\n",
31+
"```\r\n",
32+
"\r\n",
33+
"The HTTPS REST API is not in an domain that is allowed for `sp_invoke_external_rest_endpoint` usage. See the allowed list of endpoints here: [Allowed Endpoints](https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-invoke-external-rest-endpoint-transact-sql?view=azuresqldb-current&tabs=request-headers#allowed-endpoints)\r\n",
34+
"\r\n",
35+
"In order to make the NIST endpoint available to be used by `sp_invoke_external_rest_endpoint`, it must published using Azure API Management.\r\n",
36+
"\r\n",
37+
"In the next samples is assumed that the API Management instance is deployed at https://external.azure-api.net and that the NIST API has been published at the `/random` path. \r\n",
38+
"\r\n",
39+
"Once the API is published, get the Subscription key you can use to call the created API and store it into a Database Scoped Credential\r\n",
40+
""
41+
],
42+
"metadata": {
43+
"azdata_cell_guid": "2fa150ef-9af9-491d-9c6b-50f49a0b67d9"
44+
},
45+
"attachments": {}
46+
},
47+
{
48+
"cell_type": "code",
49+
"source": [
50+
"-- make sure a database master key exists\r\n",
51+
"if not exists(select * from sys.symmetric_keys where [name] = '##MS_DatabaseMasterKey##') begin\r\n",
52+
" create master key encryption by password = 'LONg_Pa$$_w0rd!'\r\n",
53+
"end\r\n",
54+
"\r\n",
55+
"-- create database scoped credential\r\n",
56+
"if exists(select * from sys.database_scoped_credentials where [name] = 'https://api-mgmt.azure-api.net/random') begin\r\n",
57+
" drop database scoped credential [https://api-mgmt.azure-api.net/random];\r\n",
58+
"end\r\n",
59+
"create database scoped credential [https://api-mgmt.azure-api.net/random]\r\n",
60+
"with identity = 'HTTPEndpointHeaders', secret = '{\"Ocp-Apim-Subscription-Key\":\"***\"}';\r\n",
61+
"go"
62+
],
63+
"metadata": {
64+
"azdata_cell_guid": "6fd989ea-205e-424d-9c72-980b6cd9fc50",
65+
"language": "sql"
66+
},
67+
"outputs": [],
68+
"execution_count": null
69+
},
70+
{
71+
"cell_type": "markdown",
72+
"source": [
73+
"And the call random number generator via the API Management endpoint:"
74+
],
75+
"metadata": {
76+
"azdata_cell_guid": "22ff69f0-86d3-40e1-9689-16e6a43762ee"
77+
},
78+
"attachments": {}
79+
},
80+
{
81+
"cell_type": "code",
82+
"source": [
83+
"declare @ret int, @response nvarchar(max);\r\n",
84+
"exec @ret = sp_invoke_external_rest_endpoint \r\n",
85+
" @url = N'https://api-mgmt.azure-api.net/random?min=0&max=100',\r\n",
86+
" @method = 'GET', \r\n",
87+
" @credential = [https://api-mgmt.azure-api.net/random],\r\n",
88+
" @response = @response OUTPUT\r\n",
89+
"select \r\n",
90+
" @ret as ReturnCode, \r\n",
91+
" json_query(@response, '$.response') as Response,\r\n",
92+
" json_query(@response, '$.result') as Result;\r\n",
93+
"\r\n",
94+
""
95+
],
96+
"metadata": {
97+
"azdata_cell_guid": "e7b5fbba-1938-4a0e-a433-7874b79cf47c",
98+
"language": "sql"
99+
},
100+
"outputs": [],
101+
"execution_count": null
102+
}
103+
]
104+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /