Skip to content

Commit d1638ea

Browse files
committed
Update README and samples
1 parent 30ed6ef commit d1638ea

7 files changed

Lines changed: 138 additions & 257 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ credential = ClientSecretCredential(
7575
scopes = ['https://graph.microsoft.com/.default']
7676
client = GraphServiceClient(credentials=credential, scopes=scopes)
7777

78+
# GET /users/{id | userPrincipalName}
7879
async def get_user():
7980
user = await client.users.by_user_id('userPrincipalName').get()
8081
if user:
81-
print(user.display_name)
82-
82+
print(user.display_name)
8383
asyncio.run(get_user())
8484
```
8585

@@ -94,11 +94,11 @@ credential = InteractiveBrowserCredential()
9494
scopes=['User.Read']
9595
client = GraphServiceClient(credentials=credential, scopes=scopes,)
9696

97+
# GET /me
9798
async def me():
9899
me = await client.me.get()
99100
if me:
100101
print(me.display_name)
101-
102102
asyncio.run(me())
103103
```
104104

@@ -113,7 +113,6 @@ async def get_user():
113113
print(user.user_principal_name, user.display_name, user.id)
114114
except APIError as e:
115115
print(f'Error: {e.error.message}')
116-
117116
asyncio.run(get_user())
118117
```
119118

@@ -124,7 +123,7 @@ asyncio.run(get_user())
124123

125124
* [Microsoft Graph website](https://aka.ms/graph)
126125

127-
* [Samples](samples)
126+
* [Samples](docs)
128127

129128
## Upgrading
130129

‎docs/applications_samples.md‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,28 @@
33
```py
44
import asyncio
55

6-
from azure.identity import EnvironmentCredential
7-
from kiota_abstractions.api_error import APIError
6+
from azure.identity import ClientSecretCredential
87
from msgraph import GraphServiceClient
98

10-
# Set the event loop policy for Windows
11-
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
12-
139
# Create a credential object. Used to authenticate requests
14-
credentials = EnvironmentCredential()
10+
credential = ClientSecretCredential(
11+
tenant_id='TENANT_ID',
12+
client_id='CLIENT_ID',
13+
client_secret='CLIENT_SECRET',
14+
)
1515
scopes = ['https://graph.microsoft.com/.default']
1616

1717
# Create an API client with the credentials and scopes
18-
client = GraphServiceClient(credentials=credentials, scopes=scopes)
18+
client = GraphServiceClient(credentials=credential, scopes=scopes)
1919
```
2020

2121
## 1. LIST ALL APPLICATIONS IN THE TENANT (GET /applications)
2222

2323
```py
2424
async def get_applications():
25-
try:
26-
apps = await client.applications.get()
27-
if apps and apps.value:
28-
for app in apps.value:
29-
print(app.id)
30-
except APIError as e:
31-
print(e.error.message)
25+
apps = await client.applications.get()
26+
if apps and apps.value:
27+
for app in apps.value:
28+
print(app.id)
3229
asyncio.run(get_applications())
3330
```

‎docs/authentication_samples.md‎

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66
import asyncio
77

88
from azure.identity import DeviceCodeCredential
9-
from kiota_abstractions.api_error import APIError
109
from msgraph import GraphServiceClient
1110

12-
# Set the event loop policy for Windows
13-
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
14-
1511
# Create a credential object. Used to authenticate requests
1612
credential = DeviceCodeCredential(
1713
client_id='CLIENT_ID',
@@ -25,13 +21,9 @@ client = GraphServiceClient(credentials=credential, scopes=scopes)
2521

2622
# GET A USER USING THE USER ID (GET /users/{id})
2723
async def get_user():
28-
try:
29-
user = await client.users_by_id('USER_ID').get()
30-
if user:
31-
print(user.user_principal_name, user.display_name, user.id)
32-
except APIError as e:
33-
print(f'Error: {e.error.message}')
34-
24+
user = await client.users_by_id('USER_ID').get()
25+
if user:
26+
print(user.user_principal_name, user.display_name, user.id)
3527
asyncio.run(get_user())
3628
```
3729

@@ -40,12 +32,8 @@ asyncio.run(get_user())
4032
```py
4133
import asyncio
4234
from azure.identity import InteractiveBrowserCredential
43-
from kiota_abstractions.api_error import APIError
4435
from msgraph import GraphServiceClient
4536

46-
# Set the event loop policy for Windows
47-
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
48-
4937
# Create a credential object. Used to authenticate requests
5038
credential = InteractiveBrowserCredential()
5139
scopes = ["User.Read"]
@@ -55,12 +43,9 @@ client = GraphServiceClient(credentials=credential, scopes=scopes)
5543

5644
# GET A USER USING THE USER ID (GET /users/{id})
5745
async def get_user():
58-
try:
59-
user = await client.users_by_id('USER_ID').get()
60-
if user:
61-
print(user.user_principal_name, user.display_name, user.id)
62-
except APIError as e:
63-
print(f'Error: {e.error.message}')
46+
user = await client.users_by_id('USER_ID').get()
47+
if user:
48+
print(user.user_principal_name, user.display_name, user.id)
6449
asyncio.run(get_user())
6550
```
6651

@@ -72,12 +57,8 @@ asyncio.run(get_user())
7257
import asyncio
7358

7459
from azure.identity import ClientSecretCredential
75-
from kiota_abstractions.api_error import APIError
7660
from msgraph import GraphServiceClient
7761

78-
# Set the event loop policy for Windows
79-
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
80-
8162
# Create a credential object. Used to authenticate requests
8263
credential = ClientSecretCredential(
8364
tenant_id='TENANT_ID',
@@ -91,12 +72,9 @@ client = GraphServiceClient(credentials=credential, scopes=scopes)
9172

9273
# GET A USER USING THE USER ID (GET /users/{id})
9374
async def get_user():
94-
try:
95-
user = await client.users.by_user_id('USER_ID').get()
96-
if user:
97-
print(user.user_principal_name, user.display_name, user.id)
98-
except APIError as e:
99-
print(f'Error: {e.error.message}')
75+
user = await client.users.by_user_id('USER_ID').get()
76+
if user:
77+
print(user.user_principal_name, user.display_name, user.id)
10078
asyncio.run(get_user())
10179
```
10280

@@ -106,12 +84,8 @@ asyncio.run(get_user())
10684
import asyncio
10785

10886
from azure.identity.aio import EnvironmentCredential
109-
from kiota_abstractions.api_error import APIError
11087
from msgraph import GraphServiceClient
11188

112-
# Set the event loop policy for Windows
113-
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
114-
11589
# Create a credential object. Used to authenticate requests
11690
credential = EnvironmentCredential()
11791
scopes = ['https://graph.microsoft.com/.default']
@@ -121,11 +95,7 @@ client = GraphServiceClient(credentials=credential, scopes=scopes)
12195

12296
# GET A USER USING THE USER ID (GET /users/{id})
12397
async def get_user():
124-
try:
125-
user = await client.users.by_user_id('USER_ID').get()
126-
if user:
127-
print(user.user_principal_name, user.display_name, user.id)
128-
except APIError as e:
129-
print(f'Error: {e.error.message}')
130-
98+
user = await client.users.by_user_id('USER_ID').get()
99+
if user:
100+
print(user.user_principal_name, user.display_name, user.id)
131101
asyncio.run(get_user())

‎docs/drives_samples.md‎

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
import asyncio
55

66
from azure.identity import ClientSecretCredential
7-
from kiota_abstractions.api_error import APIError
8-
from msgraph import GraphServiceClient
9-
10-
# (Optional) Set the event loop policy for Windows
11-
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
7+
from msgraph import GraphServiceClient
128

139
# Create a credential object. Used to authenticate requests
1410
credential = ClientSecretCredential(
@@ -26,79 +22,61 @@ client = GraphServiceClient(credentials=credential, scopes=scopes)
2622

2723
```py
2824
async def get_drives():
29-
try:
30-
drives = await client.drives.get()
31-
if drives and drives.value:
32-
for drive in drives.value:
33-
print(drive.id, drive.drive_type, drive.name, drive.description, drive.web_url)
34-
except APIError as e:
35-
print(e.error.message)
25+
drives = await client.drives.get()
26+
if drives and drives.value:
27+
for drive in drives.value:
28+
print(drive.id, drive.drive_type, drive.name, drive.description, drive.web_url)
3629
asyncio.run(get_drives())
3730
```
3831

3932
## 2. GET DRIVE BY ID (GET /drives/{id})
4033

4134
```py
4235
async def get_drive():
43-
try:
44-
drive = await client.drives.by_drive_id('DRIVE_ID').get()
45-
if drive:
46-
print(drive.id, drive.drive_type, drive.name, drive.description, drive.web_url)
47-
except APIError as e:
48-
print(e.error.message)
36+
drive = await client.drives.by_drive_id('DRIVE_ID').get()
37+
if drive:
38+
print(drive.id, drive.drive_type, drive.name, drive.description, drive.web_url)
4939
asyncio.run(get_drive())
5040
```
5141

5242
## 3. LIST ALL THE ITEMS IN A DRIVE (GET /drives/{id}/items)
5343

5444
```py
5545
async def get_drive_items():
56-
try:
57-
items = await client.drives.by_drive_id('DRIVE_ID').items.get()
58-
if items and items.value:
59-
for item in items.value:
60-
print(item.id, item.name, item.size, item.folder, item.file)
61-
except APIError as e:
62-
print(e.error.message)
46+
items = await client.drives.by_drive_id('DRIVE_ID').items.get()
47+
if items and items.value:
48+
for item in items.value:
49+
print(item.id, item.name, item.size, item.folder, item.file)
6350
asyncio.run(get_drive_items())
6451
```
6552

6653
## 4. GET AN ITEM IN THE DRIVE (GET /drives/{id}/items/{id})
6754

6855
```py
6956
async def get_drive_item():
70-
try:
71-
item = await client.drives.by_drive_id('DRIVE_ID').items.by_drive_item_id('ITEM_ID').get()
72-
if item:
73-
print(item.id, item.name, item.size, item.folder, item.file)
74-
except APIError as e:
75-
print(e.error.message)
57+
item = await client.drives.by_drive_id('DRIVE_ID').items.by_drive_item_id('ITEM_ID').get()
58+
if item:
59+
print(item.id, item.name, item.size, item.folder, item.file)
7660
asyncio.run(get_drive_item())
7761
```
7862

7963
## 5. GET THE ROOT FOLDER OF THE DRIVE (GET /drives/{id}/root)
8064

8165
```py
8266
async def get_drive_root():
83-
try:
84-
root = await client.drives.by_drive_id('DRIVE_ID').root.get()
85-
if root:
86-
print(root.id, root.name, root.folder.child_count, root.root, root.size)
87-
except APIError as e:
88-
print(e.error.message)
67+
root = await client.drives.by_drive_id('DRIVE_ID').root.get()
68+
if root:
69+
print(root.id, root.name, root.folder.child_count, root.root, root.size)
8970
asyncio.run(get_drive_root())
9071
```
9172

9273
## 6. GET ITEMS IN THE ROOT FOLDER OF THE DRIVE (GET drives/{id}/items/root/children)
9374

9475
```py
9576
async def get_drive():
96-
try:
97-
items = await client.drives.by_drive_id('DRIVE_ID').items.by_drive_item_id('root').children.get()
98-
if items and items.value:
99-
for item in items.value:
100-
print(item.id, item.name, item.size, item.folder, item.file)
101-
except APIError as e:
102-
print(e.error.code)
77+
items = await client.drives.by_drive_id('DRIVE_ID').items.by_drive_item_id('root').children.get()
78+
if items and items.value:
79+
for item in items.value:
80+
print(item.id, item.name, item.size, item.folder, item.file)
10381
asyncio.run(get_drive())
10482
```

0 commit comments

Comments
 (0)