Skip to content

Commit

Permalink
Examples update & Planner API fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Mar 31, 2024
1 parent 124423a commit 615211c
Show file tree
Hide file tree
Showing 51 changed files with 384 additions and 103 deletions.
9 changes: 0 additions & 9 deletions examples/directory/users/create_activity.py

This file was deleted.

6 changes: 4 additions & 2 deletions examples/informationprotection/create_mail_assessment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"""
from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
messages = client.me.messages.get().filter("isDraft eq false").top(1).execute_query()
result = client.information_protection.create_mail_assessment(
messages[0]
Expand Down
6 changes: 4 additions & 2 deletions examples/onedrive/bundles/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
https://learn.microsoft.com/en-us/graph/api/drive-post-bundles?view=graph-rest-1.0&tabs=http#example-1-create-a-bundle
"""
from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
file_item = client.me.drive.root.get_by_path("Sample.html").get().execute_query()
bundle = client.me.drive.create_bundle(
"Just some files", [file_item.id]
Expand Down
15 changes: 11 additions & 4 deletions examples/onedrive/columns/create_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
"""

from office365.graph_client import GraphClient
from tests import create_unique_name
from tests.graph_case import acquire_token_by_username_password
from tests import (
create_unique_name,
test_client_id,
test_password,
test_tenant,
test_username,
)

client = GraphClient(acquire_token_by_username_password)
lib = client.sites.root.lists["Docs"]
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
lib = client.sites.root.lists["Documents"]
column_name = create_unique_name("TextColumn")
column = lib.columns.add_text(column_name).execute_query()
print(column.display_name)
Expand Down
6 changes: 4 additions & 2 deletions examples/onedrive/drives/list_shared_with_me.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
https://learn.microsoft.com/en-us/graph/api/drive-sharedwithme?view=graph-rest-1.0
"""
from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
drive_items = client.me.drive.shared_with_me().execute_query()
for item in drive_items:
print("Drive Item url: {0}".format(item.web_url))
9 changes: 5 additions & 4 deletions examples/onedrive/excel/read_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
https://learn.microsoft.com/en-us/graph/api/resources/excel?view=graph-rest-1.0#get-list-of-table-rows
"""
from examples.onedrive import upload_excel_sample
from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
drive_item = upload_excel_sample(client)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
drive_item = client.me.drive.root.get_by_path("Financial Sample.xlsx")
table = (
drive_item.workbook.worksheets["Sheet1"].tables["financials"].get().execute_query()
)
Expand Down
8 changes: 5 additions & 3 deletions examples/onedrive/files/copy_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
This example copies a file identified by {server relative path} into a folder identified with a {server relative path}.
The new copy of the file will be named Sample (copy).rtf.
https://learn.microsoft.com/en-us/graph/api/driveitem-copy?view=graph-rest-1.0&tabs=http
https://learn.microsoft.com/en-us/graph/api/driveitem-copy?view=graph-rest-1.0
"""
from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
source_path = "archive/Sample.rtf"
new_name = "Sample (copy).rtf"
target_path = "archive/2018"
Expand Down
5 changes: 4 additions & 1 deletion examples/onedrive/files/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import tempfile

from office365.graph_client import GraphClient
from tests import test_client_id, test_password, test_tenant, test_username
from tests.graph_case import acquire_token_by_username_password

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
# 1. address file by path
# remote_path = "archive/countries.json"
remote_path = "archive/Financial Sample.xlsx"
Expand Down
6 changes: 4 additions & 2 deletions examples/onedrive/files/download_large.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
import tempfile

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username


def print_progress(offset):
# type: (int) -> None
print("Downloaded '{0}' bytes...".format(offset))


client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
# # 1. address file by path and get file metadata
file_item = (
client.me.drive.root.get_by_path("archive/big_buck_bunny.mp4").get().execute_query()
Expand Down
13 changes: 10 additions & 3 deletions examples/onedrive/files/get_by_abs_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
Retrieves file by absolute url
"""
from office365.graph_client import GraphClient
from tests import test_team_site_url
from tests.graph_case import acquire_token_by_username_password
from tests import (
test_client_id,
test_password,
test_team_site_url,
test_tenant,
test_username,
)

file_abs_url = "{0}/Shared Documents/Financial Sample.xlsx".format(test_team_site_url)

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
file_item = client.shares.by_url(file_abs_url).drive_item.get().execute_query()
print(file_item.web_url)
6 changes: 4 additions & 2 deletions examples/onedrive/files/move_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
https://learn.microsoft.com/en-us/graph/api/driveitem-move?view=graph-rest-1.0
"""
from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
source_path = "archive/Sample.rtf"
target_path = "archive/2018"
source_file_item = client.me.drive.root.get_by_path(source_path)
Expand Down
6 changes: 4 additions & 2 deletions examples/onedrive/files/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
result = client.search.query_drive_items("Guide.docx").execute_query()
for item in result.value:
for hit_container in item.hitsContainers:
Expand Down
5 changes: 4 additions & 1 deletion examples/onedrive/files/send_sharing_invitation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
from datetime import datetime, timedelta

from office365.graph_client import GraphClient
from tests import test_client_id, test_password, test_tenant, test_username
from tests.graph_case import acquire_token_by_username_password

file_name = "Financial Sample.xlsx"
client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
file_item = client.me.drive.root.get_by_path(file_name)
expired = datetime.utcnow() + timedelta(days=1)
permissions = file_item.invite(
Expand Down
4 changes: 2 additions & 2 deletions examples/onedrive/files/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
client = GraphClient.with_client_secret(test_tenant, test_client_id, test_client_secret)
folder = client.users.get_by_principal_name(test_user_principal_name_alt).drive.root

# local_path = "../../data/Financial Sample.xlsx"
local_path = "../../data/countries.json"
local_path = "../../data/Financial Sample.xlsx"
# local_path = "../../data/countries.json"
# file = folder.upload_file(local_path).execute_query()
with open(local_path, "rb") as f:
file = folder.upload_file(f).execute_query()
Expand Down
10 changes: 6 additions & 4 deletions examples/onedrive/folders/list_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
https://learn.microsoft.com/en-us/graph/api/driveitem-list-children?view=graph-rest-1.0
"""
from office365.graph_client import GraphClient
from tests import test_client_id, test_password, test_tenant, test_username
from tests.graph_case import acquire_token_by_username_password

client = GraphClient(acquire_token_by_username_password)
drive = client.me.drive
# items = client.me.drive.root.get_files(True).execute_query()
items = client.sites.root.lists["Documents"].drive.root.get_files(True).execute_query()
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
items = client.me.drive.root.get_files(False).execute_query()
# items = client.sites.root.lists["Documents"].drive.root.get_files(True).execute_query()
for file_item in items:
print(file_item.web_url)
6 changes: 4 additions & 2 deletions examples/onedrive/folders/list_with_files.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from office365.graph_client import GraphClient
from office365.onedrive.driveitems.driveItem import DriveItem
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username


def enum_folders_and_files(root_folder):
Expand All @@ -12,6 +12,8 @@ def enum_folders_and_files(root_folder):
enum_folders_and_files(drive_item)


client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
root = client.me.drive.root
enum_folders_and_files(root)
6 changes: 4 additions & 2 deletions examples/onedrive/powerpoint/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
remote_drive = client.me.drive.root
pptx_file = remote_drive.create_powerpoint("sample.pptx").execute_query()
print(f"File {pptx_file.web_url} has been uploaded")
6 changes: 4 additions & 2 deletions examples/onedrive/termstore/get_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
term_store = client.sites.root.term_store
sets = term_store.groups.get_by_name("Geography").sets.get().execute_query()
# term_set = group.sets.get_by_name("Locations").get().execute_query()
Expand Down
13 changes: 10 additions & 3 deletions examples/onenote/create_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
"""

from office365.graph_client import GraphClient
from tests import create_unique_name
from tests.graph_case import acquire_token_by_username_password
from tests import (
create_unique_name,
test_client_id,
test_password,
test_tenant,
test_username,
)

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
display_name = create_unique_name("My Private notebook")
notebook = client.me.onenote.notebooks.add(display_name).execute_query()
print(notebook.display_name)
6 changes: 4 additions & 2 deletions examples/outlook/calendars/find_meeting_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
result = client.me.find_meeting_times().execute_query()
for suggestion in result.value.meetingTimeSuggestions:
print(suggestion)
13 changes: 10 additions & 3 deletions examples/outlook/calendars/get_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@
from datetime import datetime, timedelta

from office365.graph_client import GraphClient
from tests import test_user_principal_name
from tests.graph_case import acquire_token_by_username_password
from tests import (
test_client_id,
test_password,
test_tenant,
test_user_principal_name,
test_username,
)

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
start_time = datetime.utcnow()
end_time = start_time + timedelta(days=1)
result = client.me.calendar.get_schedule(
Expand Down
6 changes: 4 additions & 2 deletions examples/outlook/calendars/list_my.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
https://learn.microsoft.com/en-us/graph/api/user-list-calendars?view=graph-rest-1.0
"""
from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
calendars = client.me.calendars.top(10).get().execute_query()
for cal in calendars:
print(cal)
6 changes: 4 additions & 2 deletions examples/outlook/messages/list_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
"""

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(acquire_token_by_username_password)
client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
messages = client.me.messages.get().top(10).execute_query()
for m in messages:
print(m.subject)
5 changes: 4 additions & 1 deletion examples/planner/create_plan.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Creates a new plannerPlan
Creates a new planner plan
https://learn.microsoft.com/en-us/graph/api/planner-post-plans?view=graph-rest-1.0
"""

Expand All @@ -12,3 +12,6 @@
group = client.groups.get_by_name("My Sample Team")
plan = client.planner.plans.add("My Plan", group).execute_query()
print(plan)


plan.delete_object().execute_query() # clean up
Loading

0 comments on commit 615211c

Please sign in to comment.