Skip to content

Commit 3597bb2

Browse files
authored
[dpg] add send request doc (Azure#24700)
1 parent 9d16240 commit 3597bb2

File tree

1 file changed

+217
-0
lines changed

1 file changed

+217
-0
lines changed

doc/send_request.md

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
# Using your Client's `send_request` Method
2+
3+
In this doc, we will be showing how to build your own ***http requests*** and how to send these requests directly to the service using the ***send_request*** method.
4+
5+
Here's how to get started:
6+
7+
```python
8+
>>> from azure.identity import DefaultAzureCredential
9+
>>> from azure.example.service import ExampleClient
10+
>>> from azure.core.rest import HttpRequest, HttpResponse
11+
>>> client = ExampleClient(endpoint="https://www.example.org/", credential=DefaultAzureCredential())
12+
>>> request = HttpRequest(method="GET", url="https://www.example.org")
13+
>>> request
14+
<HttpRequest [GET], url: "https://www.example.org">
15+
>>> response = client.send_request(request)
16+
>>> response
17+
<HttpResponse: 200 OK, Content-Type: text/plain>
18+
>>> response.raise_for_status()
19+
>>> response.text()
20+
'Happy to see you!'
21+
```
22+
23+
## Code Snippets
24+
25+
**End-to-end code snippets for creating and sending requests with `send_request`**:
26+
27+
1. [Sync client](#sync-client)
28+
2. [Async client](#async-client)
29+
30+
## Steps To Make a Call
31+
32+
1. [Create a request](#1-create-a-request "Create a Request")
33+
2. [Send the request](#2-send-the-request "Send the Request")
34+
3. [Handle the response](#3-handle-the-response "Handle the Response")
35+
36+
We will go into each step in the following sections. To initialize and authenticate your client, please follow your client's README examples.
37+
38+
## 1. Create a Request
39+
40+
First, we will go over how to create the [`HttpRequest`][azure_core_http_request] you want to be sent to the service.
41+
42+
We will be making a `POST` request with a `JSON` body. The following code snippet uses a relative url, which will be relative
43+
to your client's `endpoint`. You can also pass in a full-path url, and we will honor that full path.
44+
45+
```python
46+
from azure.core.rest import HttpRequest
47+
48+
# this URL is relative to the endpoint we passed our client
49+
request = HttpRequest(
50+
method="POST",
51+
url="/helloWorld",
52+
json={"document": "Hello world!"},
53+
params={"language": "en"}
54+
)
55+
```
56+
57+
## 2. Send the Request
58+
59+
Now, we pass this request to your client's `send_request` method. This actually makes the network call.
60+
61+
```python
62+
from azure.example.service import ExampleClient
63+
64+
response = client.send_request(request) # makes the network call
65+
```
66+
67+
## 3. Handle the Response
68+
69+
Our `send_request` call returns an [`HttpResponse`][azure_core_http_response].
70+
71+
### Error handling
72+
73+
The response you get back from `send_request` will not automatically raise if your response is an error.
74+
If you wish to raise an error if your response is bad, call [`.raise_for_status()`][azure_core_raise_for_status] on your returned
75+
response.
76+
77+
```python
78+
try:
79+
response.raise_for_status() # raises an error if your response is not good
80+
except HttpResponseError as e:
81+
print(str(e))
82+
```
83+
84+
### JSON response
85+
86+
If the response you get back should be a `json` object, you can call `.json()` on your response
87+
to get it `json`-deserialized.
88+
89+
Putting this all together, see our code snippets for how you can deal with your response object
90+
91+
```python
92+
93+
response = client.send_request(request)
94+
try:
95+
response.raise_for_status() # raises an error if your response is not good
96+
json_response = response.json() # get your response as a json object
97+
# Now play with your JSON response!
98+
99+
except HttpResponseError as e:
100+
print(str(e))
101+
```
102+
103+
## Examples
104+
105+
### Sync Client
106+
107+
```python
108+
from azure.identity import DefaultAzureCredential
109+
from azure.example.service import ExampleClient
110+
from azure.core.rest import HttpRequest, HttpResponse
111+
from azure.core.exceptions import HttpResponseError
112+
113+
client = ExampleClient(
114+
endpoint="https://example.org",
115+
credential=DefaultAzureCredential()
116+
)
117+
118+
request = HttpRequest(
119+
method="POST",
120+
url="/helloWorld",
121+
json={"document": "Hello world!"},
122+
params={"language": "en"}
123+
)
124+
125+
response = client.send_request(request) # returns an azure.core.rest.HttpResponse
126+
127+
try:
128+
response.raise_for_status()
129+
json_response = response.json()
130+
# Play with your response!
131+
print(json_response["language"])
132+
except HttpResponseError:
133+
print(str(e))
134+
```
135+
136+
### Async Client
137+
138+
```python
139+
from azure.identity.aio import DefaultAzureCredential
140+
from azure.example.service.aio import ExampleClient
141+
from azure.core.rest import HttpRequest, AsyncHttpResponse
142+
from azure.core.exceptions import HttpResponseError
143+
144+
request = HttpRequest(
145+
method="POST",
146+
url="/helloWorld",
147+
json={"document": "Hello world!"},
148+
params={"language": "en"}
149+
)
150+
151+
with DefaultAzureCredential() as credential:
152+
with ExampleClient(endpoint="https://example.org", credential=credential) as client:
153+
response = await client.send_request(request)
154+
155+
try:
156+
response.raise_for_status()
157+
await response.load_body()
158+
json_response = response.json() # returns an azure.core.rest.AsyncHttpResponse
159+
# Play with your response!
160+
print(json_response["language"])
161+
except HttpResponseError:
162+
print(str(e))
163+
```
164+
165+
## Troubleshooting
166+
167+
### Errors
168+
169+
All errors thrown by `.raise_for_error()` are [exceptions defined in `azure-core`][azure_core_exceptions].
170+
171+
### Logging
172+
173+
Our clients also have logging support. They use the standard
174+
[logging][python_logging] library for logging.
175+
Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO
176+
level.
177+
178+
Detailed DEBUG level logging, including request/response bodies and un-redacted
179+
headers, can be enabled on a client with the `logging_enable` keyword argument.
180+
181+
```python
182+
from azure.identity import DefaultAzureCredential
183+
from azure.example.service import ExampleClient
184+
185+
client = ExampleClient(
186+
endpoint="https://example.org",
187+
credential=DefaultAzureCredential(),
188+
logging_enable=True
189+
)
190+
```
191+
192+
### File an Issue
193+
194+
You can file issues [here][issues] in our repo.
195+
196+
<!-- LINKS -->
197+
198+
[azure_core_docs]: https://docs.microsoft.com/python/api/overview/azure/core-readme?view=azure-python
199+
[azure_identity_docs]: https://docs.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python
200+
[http_response]: https://docs.microsoft.com/python/api/azure-core/azure.core.pipeline.transport.httpresponse?view=azure-python
201+
[azure_identity_pip]: https://pypi.org/project/azure-identity/
202+
[aad_authentication]: https://docs.microsoft.com/azure/cognitive-services/authentication?tabs=powershell#authenticate-with-an-authentication-token
203+
[identity_credentials]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#credentials
204+
[default_azure_credential]: https://docs.microsoft.com/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python
205+
[azure_key_credential]: https://docs.microsoft.com/python/api/azure-core/azure.core.credentials.azurekeycredential?view=azure-python
206+
[bearer_token_credential_policy]: https://docs.microsoft.com/python/api/azure-core/azure.core.pipeline.policies.bearertokencredentialpolicy?view=azure-python
207+
[azure_key_credential_policy]: https://docs.microsoft.com/python/api/azure-core/azure.core.pipeline.policies.azurekeycredentialpolicy?view=azure-python
208+
[azure_core_exceptions]: https://docs.microsoft.com/python/api/azure-core/azure.core.exceptions?view=azure-python
209+
[azure_core_http_request]: https://docsupport.blob.core.windows.net/$web/azure-core/azure.core.html#azure.core.protocol.HttpRequest
210+
[azure_core_http_response]: https://docsupport.blob.core.windows.net/$web/azure-core/azure.core.html#azure.core.protocol.HttpResponse
211+
[azure_core_async_http_response]: https://docsupport.blob.core.windows.net/$web/azure-core/azure.core.html#azure.core.protocol.AsyncHttpResponse
212+
[azure_core_raise_for_status]: https://docsupport.blob.core.windows.net/$web/azure-core/azure.core.html#azure.core.protocol.HttpResponse.raise_for_status
213+
[python_logging]: https://docs.python.org/3.5/library/logging.html
214+
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/
215+
[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/
216+
[coc_contact]: mailto:opencode@microsoft.com
217+
[issues]: https://github.com/Azure/azure-sdk-for-python/issues

0 commit comments

Comments
 (0)