Skip to content

Commit

Permalink
Update README and samples
Browse files Browse the repository at this point in the history
  • Loading branch information
samwelkanda committed Jun 29, 2023
1 parent 30ed6ef commit d1638ea
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 257 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ credential = ClientSecretCredential(
scopes = ['https://graph.microsoft.com/.default']
client = GraphServiceClient(credentials=credential, scopes=scopes)

# GET /users/{id | userPrincipalName}
async def get_user():
user = await client.users.by_user_id('userPrincipalName').get()
if user:
print(user.display_name)

print(user.display_name)
asyncio.run(get_user())
```

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

# GET /me
async def me():
me = await client.me.get()
if me:
print(me.display_name)

asyncio.run(me())
```

Expand All @@ -113,7 +113,6 @@ async def get_user():
print(user.user_principal_name, user.display_name, user.id)
except APIError as e:
print(f'Error: {e.error.message}')

asyncio.run(get_user())
```

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

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

* [Samples](samples)
* [Samples](docs)

## Upgrading

Expand Down
25 changes: 11 additions & 14 deletions docs/applications_samples.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,28 @@
```py
import asyncio

from azure.identity import EnvironmentCredential
from kiota_abstractions.api_error import APIError
from azure.identity import ClientSecretCredential
from msgraph import GraphServiceClient

# Set the event loop policy for Windows
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

# Create a credential object. Used to authenticate requests
credentials = EnvironmentCredential()
credential = ClientSecretCredential(
tenant_id='TENANT_ID',
client_id='CLIENT_ID',
client_secret='CLIENT_SECRET',
)
scopes = ['https://graph.microsoft.com/.default']

# Create an API client with the credentials and scopes
client = GraphServiceClient(credentials=credentials, scopes=scopes)
client = GraphServiceClient(credentials=credential, scopes=scopes)
```

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

```py
async def get_applications():
try:
apps = await client.applications.get()
if apps and apps.value:
for app in apps.value:
print(app.id)
except APIError as e:
print(e.error.message)
apps = await client.applications.get()
if apps and apps.value:
for app in apps.value:
print(app.id)
asyncio.run(get_applications())
```
54 changes: 12 additions & 42 deletions docs/authentication_samples.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@
import asyncio

from azure.identity import DeviceCodeCredential
from kiota_abstractions.api_error import APIError
from msgraph import GraphServiceClient

# Set the event loop policy for Windows
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

# Create a credential object. Used to authenticate requests
credential = DeviceCodeCredential(
client_id='CLIENT_ID',
Expand All @@ -25,13 +21,9 @@ client = GraphServiceClient(credentials=credential, scopes=scopes)

# GET A USER USING THE USER ID (GET /users/{id})
async def get_user():
try:
user = await client.users_by_id('USER_ID').get()
if user:
print(user.user_principal_name, user.display_name, user.id)
except APIError as e:
print(f'Error: {e.error.message}')

user = await client.users_by_id('USER_ID').get()
if user:
print(user.user_principal_name, user.display_name, user.id)
asyncio.run(get_user())
```

Expand All @@ -40,12 +32,8 @@ asyncio.run(get_user())
```py
import asyncio
from azure.identity import InteractiveBrowserCredential
from kiota_abstractions.api_error import APIError
from msgraph import GraphServiceClient

# Set the event loop policy for Windows
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

# Create a credential object. Used to authenticate requests
credential = InteractiveBrowserCredential()
scopes = ["User.Read"]
Expand All @@ -55,12 +43,9 @@ client = GraphServiceClient(credentials=credential, scopes=scopes)

# GET A USER USING THE USER ID (GET /users/{id})
async def get_user():
try:
user = await client.users_by_id('USER_ID').get()
if user:
print(user.user_principal_name, user.display_name, user.id)
except APIError as e:
print(f'Error: {e.error.message}')
user = await client.users_by_id('USER_ID').get()
if user:
print(user.user_principal_name, user.display_name, user.id)
asyncio.run(get_user())
```

Expand All @@ -72,12 +57,8 @@ asyncio.run(get_user())
import asyncio

from azure.identity import ClientSecretCredential
from kiota_abstractions.api_error import APIError
from msgraph import GraphServiceClient

# Set the event loop policy for Windows
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

# Create a credential object. Used to authenticate requests
credential = ClientSecretCredential(
tenant_id='TENANT_ID',
Expand All @@ -91,12 +72,9 @@ client = GraphServiceClient(credentials=credential, scopes=scopes)

# GET A USER USING THE USER ID (GET /users/{id})
async def get_user():
try:
user = await client.users.by_user_id('USER_ID').get()
if user:
print(user.user_principal_name, user.display_name, user.id)
except APIError as e:
print(f'Error: {e.error.message}')
user = await client.users.by_user_id('USER_ID').get()
if user:
print(user.user_principal_name, user.display_name, user.id)
asyncio.run(get_user())
```

Expand All @@ -106,12 +84,8 @@ asyncio.run(get_user())
import asyncio

from azure.identity.aio import EnvironmentCredential
from kiota_abstractions.api_error import APIError
from msgraph import GraphServiceClient

# Set the event loop policy for Windows
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

# Create a credential object. Used to authenticate requests
credential = EnvironmentCredential()
scopes = ['https://graph.microsoft.com/.default']
Expand All @@ -121,11 +95,7 @@ client = GraphServiceClient(credentials=credential, scopes=scopes)

# GET A USER USING THE USER ID (GET /users/{id})
async def get_user():
try:
user = await client.users.by_user_id('USER_ID').get()
if user:
print(user.user_principal_name, user.display_name, user.id)
except APIError as e:
print(f'Error: {e.error.message}')

user = await client.users.by_user_id('USER_ID').get()
if user:
print(user.user_principal_name, user.display_name, user.id)
asyncio.run(get_user())
66 changes: 22 additions & 44 deletions docs/drives_samples.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import asyncio

from azure.identity import ClientSecretCredential
from kiota_abstractions.api_error import APIError
from msgraph import GraphServiceClient

# (Optional) Set the event loop policy for Windows
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
from msgraph import GraphServiceClient

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

```py
async def get_drives():
try:
drives = await client.drives.get()
if drives and drives.value:
for drive in drives.value:
print(drive.id, drive.drive_type, drive.name, drive.description, drive.web_url)
except APIError as e:
print(e.error.message)
drives = await client.drives.get()
if drives and drives.value:
for drive in drives.value:
print(drive.id, drive.drive_type, drive.name, drive.description, drive.web_url)
asyncio.run(get_drives())
```

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

```py
async def get_drive():
try:
drive = await client.drives.by_drive_id('DRIVE_ID').get()
if drive:
print(drive.id, drive.drive_type, drive.name, drive.description, drive.web_url)
except APIError as e:
print(e.error.message)
drive = await client.drives.by_drive_id('DRIVE_ID').get()
if drive:
print(drive.id, drive.drive_type, drive.name, drive.description, drive.web_url)
asyncio.run(get_drive())
```

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

```py
async def get_drive_items():
try:
items = await client.drives.by_drive_id('DRIVE_ID').items.get()
if items and items.value:
for item in items.value:
print(item.id, item.name, item.size, item.folder, item.file)
except APIError as e:
print(e.error.message)
items = await client.drives.by_drive_id('DRIVE_ID').items.get()
if items and items.value:
for item in items.value:
print(item.id, item.name, item.size, item.folder, item.file)
asyncio.run(get_drive_items())
```

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

```py
async def get_drive_item():
try:
item = await client.drives.by_drive_id('DRIVE_ID').items.by_drive_item_id('ITEM_ID').get()
if item:
print(item.id, item.name, item.size, item.folder, item.file)
except APIError as e:
print(e.error.message)
item = await client.drives.by_drive_id('DRIVE_ID').items.by_drive_item_id('ITEM_ID').get()
if item:
print(item.id, item.name, item.size, item.folder, item.file)
asyncio.run(get_drive_item())
```

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

```py
async def get_drive_root():
try:
root = await client.drives.by_drive_id('DRIVE_ID').root.get()
if root:
print(root.id, root.name, root.folder.child_count, root.root, root.size)
except APIError as e:
print(e.error.message)
root = await client.drives.by_drive_id('DRIVE_ID').root.get()
if root:
print(root.id, root.name, root.folder.child_count, root.root, root.size)
asyncio.run(get_drive_root())
```

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

```py
async def get_drive():
try:
items = await client.drives.by_drive_id('DRIVE_ID').items.by_drive_item_id('root').children.get()
if items and items.value:
for item in items.value:
print(item.id, item.name, item.size, item.folder, item.file)
except APIError as e:
print(e.error.code)
items = await client.drives.by_drive_id('DRIVE_ID').items.by_drive_item_id('root').children.get()
if items and items.value:
for item in items.value:
print(item.id, item.name, item.size, item.folder, item.file)
asyncio.run(get_drive())
```
Loading

0 comments on commit d1638ea

Please sign in to comment.