Skip to content

Commit a64bee9

Browse files
committed
Add drives samples
1 parent e44e613 commit a64bee9

4 files changed

Lines changed: 124 additions & 10 deletions

File tree

‎samples/applications_samples.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Create the API Client
2+
13
```py
24
import asyncio
35

@@ -19,8 +21,11 @@ request_adapter = GraphRequestAdapter(auth_provider)
1921

2022
# Get a service client.
2123
client = GraphServiceClient(request_adapter)
24+
```
25+
26+
## 1. LIST ALL APPLICATIONS IN THE TENANT (GET /applications)
2227

23-
# GET ALL APPLICATIONS IN THE TENANT (GET /applications)
28+
```py
2429
async def get_applications():
2530
try:
2631
apps = await client.applications.get()

‎samples/drives_samples.md‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Create the API Client
2+
3+
```py
4+
import asyncio
5+
6+
from azure.identity import ClientSecretCredential
7+
from kiota_abstractions.api_error import APIError
8+
from kiota_authentication_azure.azure_identity_authentication_provider import AzureIdentityAuthenticationProvider
9+
from msgraph import GraphRequestAdapter, GraphServiceClient
10+
11+
# (Optional) Set the event loop policy for Windows
12+
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
13+
14+
# Create authentication provider object. Used to authenticate request
15+
credential = ClientSecretCredential(
16+
tenant_id='TENANT_ID',
17+
client_id='CLIENT_ID',
18+
client_secret='CLIENT_SECRET'
19+
)
20+
scopes = ['https://graph.microsoft.com/.default']
21+
auth_provider = AzureIdentityAuthenticationProvider(credential, scopes=scopes)
22+
23+
# Initialize a request adapter with the auth provider.
24+
request_adapter = GraphRequestAdapter(auth_provider)
25+
26+
# Create an API client with the request adapter.
27+
client = GraphServiceClient(request_adapter)
28+
```
29+
30+
## 1. LIST ALL DRIVES (GET /drives)
31+
32+
```py
33+
async def get_drives():
34+
try:
35+
drives = await client.drives.get()
36+
if drives and drives.value:
37+
for drive in drives.value:
38+
print(drive.id, drive.drive_type, drive.name, drive.description, drive.web_url)
39+
except APIError as e:
40+
print(e.error.message)
41+
asyncio.run(get_drives())
42+
```
43+
44+
## 2. GET DRIVE BY ID (GET /drives/{id})
45+
46+
```py
47+
async def get_drive():
48+
try:
49+
drive = await client.drives.by_drive_id('DRIVE_ID').get()
50+
if drive:
51+
print(drive.id, drive.drive_type, drive.name, drive.description, drive.web_url)
52+
except APIError as e:
53+
print(e.error.message)
54+
asyncio.run(get_drive())
55+
```
56+
57+
## 3. LIST ALL THE ITEMS IN A DRIVE (GET /drives/{id}/items)
58+
59+
```py
60+
async def get_drive_items():
61+
try:
62+
items = await client.drives.by_drive_id('DRIVE_ID').items.get()
63+
if items and items.value:
64+
for item in items.value:
65+
print(item.id, item.name, item.size, item.folder, item.file)
66+
except APIError as e:
67+
print(e.error.message)
68+
asyncio.run(get_drive_items())
69+
```
70+
71+
## 4. GET AN ITEM IN THE DRIVE (GET /drives/{id}/items/{id})
72+
73+
```py
74+
async def get_drive_item():
75+
try:
76+
item = await client.drives.by_drive_id('DRIVE_ID').items.by_drive_item_id('ITEM_ID').get()
77+
if item:
78+
print(item.id, item.name, item.size, item.folder, item.file)
79+
except APIError as e:
80+
print(e.error.message)
81+
asyncio.run(get_drive_item())
82+
```
83+
84+
## 5. GET THE ROOT FOLDER OF THE DRIVE (GET /drives/{id}/root)
85+
86+
```py
87+
async def get_drive_root():
88+
try:
89+
root = await client.drives.by_drive_id('DRIVE_ID').root.get()
90+
if root:
91+
print(root.id, root.name, root.folder.child_count, root.root, root.size)
92+
except APIError as e:
93+
print(e.error.message)
94+
asyncio.run(get_drive_root())
95+
```
96+
97+
## 6. GET ITEMS IN THE ROOT FOLDER OF THE DRIVE (GET drives/{id}/items/root/children)
98+
99+
```py
100+
async def get_drive():
101+
try:
102+
items = await client.drives.by_drive_id('DRIVE_ID').items.by_drive_item_id('root').children.get()
103+
if items and items.value:
104+
for item in items.value:
105+
print(item.id, item.name, item.size, item.folder, item.file)
106+
except APIError as e:
107+
print(e.error.code)
108+
asyncio.run(get_drive())
109+
```

‎samples/general_samples.md‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Usage Examples
22

3-
## Creating a Graph client
3+
## 1. Creating a Graph client
44
This creates a default Graph client that uses `https://graph.microsoft.com` as the default base URL and default configured HTTPX client to make the requests.
55

66
```py
@@ -25,7 +25,7 @@ request_adapter = GraphRequestAdapter(auth_provider)
2525
client = GraphServiceClient(request_adapter)
2626
```
2727

28-
## Creating a Graph client using a custom `httpx.AsyncClient` instance
28+
## 2. Creating a Graph client using a custom `httpx.AsyncClient` instance
2929

3030
```py
3131
from msgraph import GraphRequestAdapter
@@ -35,7 +35,7 @@ http_client = GraphClientFactory.create_with_default_middleware(client=httpx.Asy
3535
request_adapter = GraphRequestAdapter(auth_provider, http_client)
3636
```
3737

38-
## Get an item from the Graph
38+
## 3. Get an item from the Microsoft Graph API
3939

4040
This sample fetches the current signed-in user. Note that to use `/me` endpoint you need
4141
a delegated permission. Alternatively, using application permissions, you can request `/users/[userPrincipalName]`. See [Microsoft Graph Permissions](https://docs.microsoft.com/en-us/graph/auth/auth-concepts#microsoft-graph-permissions) for more.
@@ -53,7 +53,7 @@ async def get_me():
5353
asyncio.run(get_me())
5454
```
5555

56-
## Get a collection of items
56+
## 4. Get a collection of items
5757
This snippet retrieves the messages in a user's mailbox. Ensure you have the [correct permissions](https://docs.microsoft.com/en-us/graph/api/user-list-messages?view=graph-rest-1.0&tabs=http#permissions) set.
5858
The Graph API response is deserialized into a collection of `Message` - a model class provided by the SDK.
5959

@@ -69,7 +69,7 @@ async def get_user_messages():
6969
asyncio.run(get_user_messages())
7070
```
7171

72-
## Passing custom request headers
72+
## 5. Passing custom request headers
7373
Each execution method i.e. `get()`, `post()`, `put()`, `patch()`, `delete()` accepts a `RequestConfiguration` object where the request headers can be set:
7474

7575
```py
@@ -92,7 +92,7 @@ async def get_user_messages():
9292
asyncio.run(get_user_messages())
9393
```
9494

95-
## Passing query parameters
95+
## 6. Passing query parameters
9696

9797
```py
9898
from msgraph.generated.users.item.messages.messages_request_builder import MessagesRequestBuilder
@@ -117,7 +117,7 @@ async def get_5_user_messages():
117117
asyncio.run(get_5_user_messages())
118118
```
119119

120-
## Get the raw response
120+
## 7. Get the raw http response
121121
The SDK provides a default response handler which returns the native HTTPX response.
122122

123123
To get the raw response:
@@ -137,7 +137,7 @@ async def get_user_messages():
137137
asyncio.run(get_user())
138138
```
139139

140-
## Send an email
140+
## 8. Send an email
141141

142142
This sample sends an email. The request body is constructed using the provided models.
143143
Ensure you have the [right permissions](https://docs.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=http#permissions).

‎samples/groups_samples.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Create the API Client
1+
# Create the API Client
22

33
```py
44
import asyncio

0 commit comments

Comments
 (0)