Skip to content

Commit db5882d

Browse files
committed
Update general samples
1 parent b589932 commit db5882d

1 file changed

Lines changed: 22 additions & 51 deletions

File tree

docs/samples.md renamed to docs/general_samples.md

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ This creates a default Graph client that uses `https://graph.microsoft.com` as t
55

66
```py
77
from azure.identity import AuthorizationCodeCredential
8+
from kiota_abstactions.api_error import APIError
89
from kiota_authentication_azure.azure_identity_authentication_provider import AzureIdentityAuthenticationProvider
910
from msgraph import GraphRequestAdapter
1011
from msgraph import GraphServiceClient
1112

13+
# Set the event loop policy for Windows
14+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
15+
1216
AuthorizationCodeCredential(
1317
tenant_id: str,
1418
client_id: str,
@@ -21,7 +25,7 @@ request_adapter = GraphRequestAdapter(auth_provider)
2125
client = GraphServiceClient(request_adapter)
2226
```
2327

24-
Using a custom `httpx.AsyncClient` instance:
28+
## Creating a Graph client using a custom `httpx.AsyncClient` instance
2529

2630
```py
2731
from msgraph import GraphRequestAdapter
@@ -38,14 +42,15 @@ a delegated permission. Alternatively, using application permissions, you can re
3842

3943

4044
```py
41-
async def get_user():
45+
async def get_me():
4246
try:
43-
user = await client.me.get()
44-
return user
45-
except Exception as e:
47+
me = await client.me.get()
48+
if me:
49+
print(me.user_principal_name, me.display_name, me.id)
50+
except APIError as e:
4651
print(e.error.message)
4752

48-
asyncio.run(get_user())
53+
asyncio.run(get_me())
4954
```
5055

5156
## Get a collection of items
@@ -56,11 +61,11 @@ The Graph API response is deserialized into a collection of `Message` - a model
5661
async def get_user_messages():
5762
try:
5863
messages = await (client.users_by_id('USER_ID').messages.get())
59-
for msg in messages.value:
60-
print(msg.subject, msg.id, msg.from_escaped)
61-
except Exception as e:
64+
if messages and messages.value:
65+
for msg in messages.value:
66+
print(msg.subject, msg.id, msg.from_)
67+
except APIError as e:
6268
print(e.error.message)
63-
6469
asyncio.run(get_user_messages())
6570
```
6671

@@ -79,11 +84,11 @@ async def get_user_messages():
7984
messages = await (client.users_by_id('USER_ID')
8085
.messages
8186
.get(request_configuration=request_config))
82-
for msg in messages.value:
83-
print(msg.subject, msg.id, msg.from_escaped)
87+
if messages and messages.value:
88+
for msg in messages.value:
89+
print(msg.subject, msg.id, msg.from_)
8490
except Exception as e:
8591
print(e.error.message)
86-
8792
asyncio.run(get_user_messages())
8893
```
8994

@@ -104,48 +109,14 @@ async def get_5_user_messages():
104109
messages = await (client.users_by_id('USER_ID')
105110
.messages
106111
.get(request_configuration=request_config))
107-
for msg in messages.value:
108-
print(msg.subject)
112+
if messages and messages.value:
113+
for msg in messages.value:
114+
print(msg.subject)
109115
except Exception as e:
110116
print(e.error.message)
111-
112117
asyncio.run(get_5_user_messages())
113118
```
114119

115-
## Paging through a collection of items
116-
Some queries against Microsoft Graph return multiple pages of data either due to server-side paging or due to the use of the `$top` query parameter to specifically limit the page size in a request. When a result set spans multiple pages, Microsoft Graph returns an `@odata.nextLink` property in the response that contains a URL to the next page of results.
117-
118-
For now, you can page through the collection using the `@odata.nextLink` value. We intend to introduce a Page Iterator component in the future releases:
119-
120-
```py
121-
from msgraph.generated.users.item.messages.messages_request_builder import MessagesRequestBuilder
122-
123-
async def get_user_messages():
124-
query_params = MessagesRequestBuilder.MessagesRequestBuilderGetQueryParameters(
125-
select=['subject',], top=3
126-
)
127-
request_config = MessagesRequestBuilder.MessagesRequestBuilderGetRequestConfiguration(
128-
query_parameters=query_params
129-
)
130-
131-
messages = await (client.users_by_id('USER_ID')
132-
.messages
133-
.get(request_configuration=request_config))
134-
for msg in messages.value:
135-
print(msg.subject)
136-
while messages.odata_next_link:
137-
request_info = client.users_by_id(USER_ID)
138-
.messages
139-
.create_get_request_information(request_configuration=request_config)
140-
request_info.uri = messages.odata_next_link
141-
messages = request_adapter.send_async(request_info, MessageCollectionResponse)
142-
for msg in messages.value:
143-
print(msg.subject)
144-
145-
asyncio.run(get_user_messages())
146-
```
147-
148-
149120
## Get the raw response
150121
The SDK provides a default response handler which returns the native HTTPX response.
151122

@@ -161,7 +132,7 @@ async def get_user_messages():
161132
options=[ResponseHandlerOption(NativeResponseHandler())], )
162133
messages = await client.users_by_id('USER_ID').messages.get(request_configuration=request_config)
163134
print(messages.json())
164-
except Exception as e:
135+
except APIError as e:
165136
print(e.error.message)
166137
asyncio.run(get_user())
167138
```

0 commit comments

Comments
 (0)