Skip to content

Commit

Permalink
typings enhancements and examples updates
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Nov 8, 2023
1 parent 6a6d61f commit 7dadd19
Show file tree
Hide file tree
Showing 44 changed files with 257 additions and 153 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# About
Office 365 & Microsoft Graph library for Python
Microsoft 365 & Microsoft Graph library for Python

# Usage

Expand Down Expand Up @@ -356,11 +356,11 @@ def download_files(remote_folder, local_path):
Additional examples:

- [create list column](examples/onedrive/columns/create_text.py)
- [download file](examples/onedrive/files/download_default.py)
- [download file](examples/onedrive/files/download.py)
- [export files](examples/onedrive/files/export.py)
- [upload folder](examples/onedrive/folders/upload.py)
- [list drives](examples/onedrive/drives/list.py)
- [list files](examples/onedrive/folders/list_with_files.py)
- [list files](examples/onedrive/folders/list_files.py)

Refer to [OneDrive examples section](examples/onedrive) for more examples.

Expand Down
4 changes: 2 additions & 2 deletions examples/onedrive/columns/list_site.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""
Retrieves site columns
https://learn.microsoft.com/en-us/graph/api/site-list-columns?view=graph-rest-1.0
"""
from office365.graph_client import GraphClient
from office365.onedrive.columns.definition import ColumnDefinition
from tests.graph_case import acquire_token_by_username_password

client = GraphClient(acquire_token_by_username_password)
columns = client.sites.root.columns.get().execute_query()
for column in columns:
print(column.name)
print(column)
2 changes: 1 addition & 1 deletion examples/onedrive/drives/list.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
List available drives
https://learn.microsoft.com/en-us/graph/api/drive-list?view=graph-rest-1.0&tabs=http
https://learn.microsoft.com/en-us/graph/api/drive-list?view=graph-rest-1.0
"""
from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_client_credentials
Expand Down
14 changes: 14 additions & 0 deletions examples/onedrive/drives/list_recent_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
List recent files
https://learn.microsoft.com/en-us/graph/api/drive-recent?view=graph-rest-1.0
"""
from office365.graph_client import GraphClient
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient.with_username_and_password(
test_tenant, test_client_id, test_username, test_password
)
items = client.me.drive.recent().execute_query()
for item in items:
print(item.web_url)
Empty file.
14 changes: 10 additions & 4 deletions examples/onedrive/excel/read_worksheet.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
"""
Retrieve a list of worksheet objects.
https://learn.microsoft.com/en-us/graph/api/workbook-list-worksheets?view=graph-rest-1.0
"""

import sys

from examples.onedrive import upload_excel_sample
from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password

client = GraphClient(acquire_token_by_username_password)
drive_item = upload_excel_sample(client)
# Load worksheets
drive_item = client.me.drive.root.get_by_path("Financial Sample.xlsx")
worksheets = drive_item.workbook.worksheets.get().execute_query()
if len(worksheets) == 0:
sys.exit("No worksheets found")
print("Worksheet name: {0}".format(worksheets[0].name))

for worksheet in worksheets:
print("Worksheet name: {0}".format(worksheet))
Empty file.
File renamed without changes.
1 change: 1 addition & 0 deletions examples/onedrive/files/download_large.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


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


Expand Down
10 changes: 6 additions & 4 deletions examples/onedrive/files/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
import tempfile

from office365.graph_client import GraphClient
from office365.onedrive.drives.drive import Drive
from office365.onedrive.driveitems.driveItem import DriveItem
from tests import test_user_principal_name
from tests.graph_case import acquire_token_by_client_credentials

client = GraphClient(acquire_token_by_client_credentials)
drive = client.users[test_user_principal_name].drive # type: Drive
drive = client.users[test_user_principal_name].drive
with tempfile.TemporaryDirectory() as local_path:
drive_items = drive.root.children.get().execute_query()
file_items = [item for item in drive_items if item.file is not None] # files only
file_items = [
item for item in drive_items if item.file is not None
] # type: list[DriveItem]
for drive_item in file_items:
with open(os.path.join(local_path, drive_item.name), "wb") as local_file:
drive_item.download(local_file).execute_query() # download file content
drive_item.download(local_file).execute_query()
print("File '{0}' has been downloaded".format(local_file.name))
4 changes: 2 additions & 2 deletions examples/onedrive/files/get_by_abs_url.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Retrieves file by absolute url
"""
from office365.graph_client import GraphClient
from tests import test_team_site_url
Expand All @@ -9,4 +9,4 @@

client = GraphClient(acquire_token_by_username_password)
file_item = client.shares.by_url(file_abs_url).drive_item.get().execute_query()
print(f"Drive item Id: {file_item.web_url}")
print(file_item.web_url)
4 changes: 1 addition & 3 deletions examples/onedrive/files/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
from tests.graph_case import acquire_token_by_client_credentials

client = GraphClient(acquire_token_by_client_credentials)
folder = client.users.get_by_principal_name(
test_user_principal_name_alt
).drive.root.get_by_path("archive")
folder = client.users.get_by_principal_name(test_user_principal_name_alt).drive.root

local_path = "../../data/Financial Sample.xlsx"
file = folder.upload_file(local_path).execute_query()
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions examples/onedrive/lists/create_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
from tests.graph_case import acquire_token_by_client_credentials

client = GraphClient(acquire_token_by_client_credentials)

print("Creating a custom list...")
custom_list = client.sites.root.lists.add(
create_unique_name("Books"), "genericList"
).execute_query()
print("List has been created at {0}".format(custom_list.web_url))

print("Cleaning up resources...")
custom_list.delete_object().execute_query()
2 changes: 1 addition & 1 deletion examples/onedrive/lists/get_metadata.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Get metadata for a list
https://learn.microsoft.com/en-us/graph/api/list-get?view=graph-rest-1.0&tabs=http
https://learn.microsoft.com/en-us/graph/api/list-get?view=graph-rest-1.0
"""

from office365.graph_client import GraphClient
Expand Down
7 changes: 3 additions & 4 deletions examples/onedrive/sites/grant_permission.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"""
Grant permissions on a site.
Grants permissions on a site.
https://learn.microsoft.com/en-us/graph/api/site-post-permissions?view=graph-rest-1.0
"""
import json

from office365.graph_client import GraphClient
from tests import test_client_credentials, test_team_site_url
Expand All @@ -16,5 +15,5 @@

print("Granting an Application a permissions on Site...")
site = client.sites.get_by_url(test_team_site_url)
permission = site.permissions.add(["read", "write"], app).execute_query()
print(json.dumps(permission.granted_to.to_json(), indent=4))
permission = site.permissions.add(["write"], app).execute_query()
print(permission.granted_to)
15 changes: 15 additions & 0 deletions examples/onedrive/sites/list_permission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
Lists site permissions.
"""

from office365.graph_client import GraphClient
from tests import test_team_site_url
from tests.graph_case import acquire_token_by_client_credentials

client = GraphClient(acquire_token_by_client_credentials)

site = client.sites.get_by_url(test_team_site_url)
permissions = site.permissions.get().execute_query()
for perm in permissions:
print(perm.granted_to)
16 changes: 16 additions & 0 deletions examples/onedrive/sites/revoke_permission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
Revokes permissions from a site.
https://learn.microsoft.com/en-us/graph/api/site-delete-permission?view=graph-rest-1.0
"""

from office365.graph_client import GraphClient
from tests import test_client_credentials, test_team_site_url
from tests.graph_case import acquire_token_by_client_credentials

client = GraphClient(acquire_token_by_client_credentials)

app = client.applications.get_by_app_id(test_client_credentials.clientId)
site = client.sites.get_by_url(test_team_site_url)
site.permissions.delete(["write"], app).execute_query()
# site.permissions.delete_all().execute_query()
2 changes: 0 additions & 2 deletions examples/onedrive/sites/search.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""
Search across a SharePoint tenant for sites that match keywords provided.
https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/site_search?view=odsp-graph-online
"""

Expand Down
16 changes: 16 additions & 0 deletions examples/sharepoint/folders/get_by_shared_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
Returns the folder object from the tokenized sharing link URL.
"""
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.sharing.links.kind import SharingLinkKind
from tests import test_team_site_url, test_user_credentials

ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)

folder = ctx.web.get_folder_by_server_relative_url("Shared Documents/Archive")
# Share a folder
result = folder.share_link(SharingLinkKind.OrganizationView).execute_query()


shared_folder = ctx.web.get_folder_by_guest_url(str(result.value)).execute_query()
print(shared_folder)
2 changes: 1 addition & 1 deletion examples/sharepoint/folders/list_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
folders = (
ctx.web.default_document_library().root_folder.get_folders(False).execute_query()
ctx.web.default_document_library().root_folder.get_files(False).execute_query()
)
for folder in folders:
print(
Expand Down
15 changes: 15 additions & 0 deletions examples/sharepoint/folders/rename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
Demonstrates how to rename a folder
"""
from office365.sharepoint.client_context import ClientContext
from tests import create_unique_name, test_client_credentials, test_team_site_url

ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)

folder = ctx.web.default_document_library().root_folder.add(
create_unique_name("Orders - (2007)")
) # create temp folder

folder.rename("OUT - (Drafts 123)").execute_query()

folder.delete_object().execute_query()
4 changes: 2 additions & 2 deletions examples/sharepoint/sites/get_admins.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"""

from office365.sharepoint.client_context import ClientContext
from tests import test_admin_credentials, test_site_url
from tests import test_admin_credentials, test_team_site_url

client = ClientContext(test_site_url).with_credentials(test_admin_credentials)
client = ClientContext(test_team_site_url).with_credentials(test_admin_credentials)
result = client.site.get_site_administrators().execute_query()
for info in result.value:
print(info)
4 changes: 2 additions & 2 deletions examples/sharepoint/sites/get_basic_props.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Gets site basic properties
"""
from office365.sharepoint.client_context import ClientContext
from tests import test_client_credentials, test_site_url
from tests import test_client_credentials, test_site_url, test_team_site_url

client = ClientContext(test_site_url).with_credentials(test_client_credentials)
client = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
site = client.site.get().execute_query()
print("Site url: {}".format(site.url))

Expand Down
37 changes: 8 additions & 29 deletions examples/sharepoint/sites/grant_app_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,19 @@
Refer:
https://developer.microsoft.com/en-us/office/blogs/controlling-app-access-on-specific-sharepoint-site-collections/
"""
import json

from office365.graph_client import GraphClient
from tests import test_client_credentials, test_team_site_url
from tests.graph_case import acquire_token_by_client_credentials


def assign_site_access(site, application, roles=None, clear_existing=False):
"""
:param office365.onedrive.sites.site.Site site: Site the permissions to grant
:param office365.onedrive.directory.applications.Application application: Application
:param list[str] roles: The list of roles to add
:param bool clear_existing: Clear existing permissions first
"""
if clear_existing:
print("Clearing existing permissions...")
target_site.permissions.delete_all().execute_query()

if roles:
print(
"Granting {0} permissions for application {1}".format(
roles, application.app_id
)
)
site.permissions.add(roles, application).execute_query()

result = site.permissions.get().execute_query()
print("Current permissions: {0}".format(json.dumps(result.to_json(), indent=4)))


client = GraphClient(acquire_token_by_client_credentials)
target_site = client.sites.get_by_url(test_team_site_url)
site = client.sites.get_by_url(test_team_site_url)
app = client.applications.get_by_app_id(test_client_credentials.clientId)
# assign_site_access(user_site_url, [], True)
assign_site_access(target_site, app, ["read", "write"], True)
roles = ["read", "write"]

print("Granting {0} permissions for application {1}".format(roles, app))
site.permissions.add(roles, app).execute_query()
result = site.permissions.get().execute_query()
for perm in result:
print("Current permissions: {0}".format(perm.granted_to_identities))
4 changes: 2 additions & 2 deletions examples/teams/create_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
client = GraphClient(acquire_token_by_username_password)
team_name = create_unique_name("Team")
print("Creating a team '{0}' ...".format(team_name))
new_team = client.teams.create(team_name).execute_query_and_wait()
team = client.teams.create(team_name).execute_query_and_wait()
print("Team has been created")

print("Cleaning up temporary resources... ")
new_team.delete_object().execute_query()
team.delete_object().execute_query()
4 changes: 2 additions & 2 deletions generator/import_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ def export_to_file(path, content):
"--endpoint",
dest="endpoint",
help="Import metadata endpoint",
default="sharepoint",
default="microsoftgraph",
)
parser.add_argument(
"-p",
"--path",
dest="path",
default="./metadata/SharePoint.xml",
default="./metadata/MicrosoftGraph.xml",
help="Import metadata endpoint",
)

Expand Down
8 changes: 8 additions & 0 deletions generator/metadata/MicrosoftGraph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21358,6 +21358,7 @@
<Property Name="videoTeleconferenceId" Type="Edm.String"/>
<Property Name="watermarkProtection" Type="graph.watermarkProtectionValues"/>
<NavigationProperty Name="attendanceReports" Type="Collection(graph.meetingAttendanceReport)" ContainsTarget="true"/>
<NavigationProperty Name="recordings" Type="Collection(graph.callRecording)" ContainsTarget="true"/>
<NavigationProperty Name="transcripts" Type="Collection(graph.callTranscript)" ContainsTarget="true"/>
</EntityType>
<EntityType Name="presence" BaseType="graph.entity">
Expand Down Expand Up @@ -25293,6 +25294,13 @@
<Property Name="totalParticipantCount" Type="Edm.Int32"/>
<NavigationProperty Name="attendanceRecords" Type="Collection(graph.attendanceRecord)" ContainsTarget="true"/>
</EntityType>
<EntityType Name="callRecording" BaseType="graph.entity">
<Property Name="content" Type="Edm.Stream"/>
<Property Name="createdDateTime" Type="Edm.DateTimeOffset"/>
<Property Name="meetingId" Type="Edm.String"/>
<Property Name="meetingOrganizer" Type="graph.identitySet"/>
<Property Name="recordingContentUrl" Type="Edm.String"/>
</EntityType>
<EntityType Name="callTranscript" BaseType="graph.entity">
<Property Name="content" Type="Edm.Stream"/>
<Property Name="createdDateTime" Type="Edm.DateTimeOffset"/>
Expand Down
Loading

0 comments on commit 7dadd19

Please sign in to comment.