Skip to content

Commit

Permalink
SearchEntity.query and DriveItem.resumable_upload methods enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Oct 27, 2023
1 parent 9d9f8d9 commit bcc6621
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 209 deletions.
9 changes: 0 additions & 9 deletions examples/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
from office365.onedrive.drives.drive import Drive


def upload_sample_files(drive):
# type: (Drive) -> None
local_paths = ["../../data/Financial Sample.xlsx"]
for local_path in local_paths:
file = drive.root.resumable_upload(local_path).get().execute_query()
print("File {0} has been uploaded", file.web_url)
2 changes: 0 additions & 2 deletions examples/onedrive/files/share_invitation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
"""
from datetime import datetime, timedelta

from examples import upload_sample_files
from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password

file_name = "Financial Sample.xlsx"
client = GraphClient(acquire_token_by_username_password)
# upload_sample_files(client.me.drive)
file_item = client.me.drive.root.get_by_path(file_name)
expired = datetime.utcnow() + timedelta(days=1)
permissions = file_item.invite(
Expand Down
7 changes: 3 additions & 4 deletions examples/onedrive/files/upload_large.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password

client = GraphClient(acquire_token_by_username_password)

chunk_size = 3 * 1024 * 1024


def print_progress(range_pos):
# type: (int) -> None
print("{0} bytes uploaded".format(range_pos))


client = GraphClient(acquire_token_by_username_password)
chunk_size = 1 * 1024 * 1024
local_path = "../../../tests/data/big_buck_bunny.mp4"
remote_folder = client.me.drive.root.get_by_path("archive")
remote_file = (
Expand Down
6 changes: 3 additions & 3 deletions examples/outlook/messages/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
https://learn.microsoft.com/en-us/graph/search-concept-messages
"""

import json

from office365.graph_client import GraphClient
from tests.graph_case import acquire_token_by_username_password

client = GraphClient(acquire_token_by_username_password)
result = client.search.query_messages("Let's go for lunch").execute_query()
print(json.dumps(result.value.to_json(), indent=4))
for item in result.value:
for hit in item.hitsContainers[0].hits:
print(hit.resource.properties.get("webLink"))
25 changes: 14 additions & 11 deletions examples/outlook/messages/send_with_large_attachment.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
"""
Demonstrates how to upload large attachment to Outlook message
Demonstrates how send e message with large attachment to Outlook message
https://learn.microsoft.com/en-us/graph/api/attachment-createuploadsession?view=graph-rest-1.0
"""

from office365.graph_client import GraphClient
from tests import test_user_principal_name_alt
from tests.graph_case import acquire_token_by_username_password

client = GraphClient(acquire_token_by_username_password)

local_path = "../../../tests/data/big_buck_bunny.mp4"


def print_progress(range_pos):
print("{0} bytes uploaded".format(range_pos))


client = GraphClient(acquire_token_by_username_password)
local_path = "../../../tests/data/big_buck_bunny.mp4"
message = (
client.me.messages.add(
subject="Meet for lunch?",
body="The new cafeteria is open.",
to_recipients=["[email protected]"],
(
client.me.messages.add(
subject="Meet for lunch?",
body="The new cafeteria is open.",
to_recipients=[
"[email protected]",
test_user_principal_name_alt,
],
).upload_attachment(local_path, print_progress)
)
.upload_attachment(local_path, print_progress)
.send()
.execute_query()
)
message.send().execute_query()
138 changes: 91 additions & 47 deletions generator/metadata/MicrosoftGraph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3871,6 +3871,11 @@
<PropertyValue Property="Supported" Bool="false"/>
</Record>
</Annotation>
<Annotation Term="Org.OData.Capabilities.V1.CountRestrictions">
<Record>
<PropertyValue Property="Countable" Bool="false"/>
</Record>
</Annotation>
<Annotation Term="Org.OData.Capabilities.V1.DeleteRestrictions">
<Record>
<PropertyValue Property="Deletable" Bool="false"/>
Expand Down Expand Up @@ -5242,6 +5247,11 @@
<PropertyValue Property="Supported" Bool="false"/>
</Record>
</Annotation>
<Annotation Term="Org.OData.Capabilities.V1.CountRestrictions">
<Record>
<PropertyValue Property="Countable" Bool="false"/>
</Record>
</Annotation>
<Annotation Term="Org.OData.Capabilities.V1.DeleteRestrictions">
<Record>
<PropertyValue Property="Deletable" Bool="false"/>
Expand Down Expand Up @@ -5298,11 +5308,18 @@
</Collection>
</Annotation>
</Annotations>
<Annotations Target="microsoft.graph.certification/certificationDetailsUrl">
<Annotation Term="Org.OData.Core.V1.Computed" Bool="true"/>
</Annotations>
<Annotations Target="microsoft.graph.certification/isCertifiedByMicrosoft">
<Annotation Term="Org.OData.Core.V1.Computed" Bool="true"/>
<Annotations Target="microsoft.graph.directory/deviceLocalCredentials">
<Annotation Term="Org.OData.Capabilities.V1.ExpandRestrictions">
<Record>
<PropertyValue Property="Expandable" Bool="false"/>
</Record>
</Annotation>
<Annotation Term="Org.OData.Capabilities.V1.NavigationRestrictions">
<Record>
<PropertyValue Property="Referenceable" Bool="false"/>
</Record>
</Annotation>
<Annotation Term="Org.OData.Capabilities.V1.SkipSupported" Bool="false"/>
</Annotations>
<Annotations Target="microsoft.graph.administrativeUnit">
<Annotation Term="Org.OData.Capabilities.V1.ChangeTracking">
Expand All @@ -5311,6 +5328,19 @@
</Record>
</Annotation>
</Annotations>
<Annotations Target="microsoft.graph.directory/administrativeUnits">
<Annotation Term="Org.OData.Capabilities.V1.ChangeTracking">
<Record>
<PropertyValue Property="Supported" Bool="true"/>
</Record>
</Annotation>
</Annotations>
<Annotations Target="microsoft.graph.certification/certificationDetailsUrl">
<Annotation Term="Org.OData.Core.V1.Computed" Bool="true"/>
</Annotations>
<Annotations Target="microsoft.graph.certification/isCertifiedByMicrosoft">
<Annotation Term="Org.OData.Core.V1.Computed" Bool="true"/>
</Annotations>
<Annotations Target="microsoft.graph.crossTenantAccessPolicyConfigurationDefault">
<Annotation Term="Org.OData.Capabilities.V1.CountRestrictions">
<Record>
Expand Down Expand Up @@ -5407,13 +5437,6 @@
</Record>
</Annotation>
</Annotations>
<Annotations Target="microsoft.graph.directory/administrativeUnits">
<Annotation Term="Org.OData.Capabilities.V1.ChangeTracking">
<Record>
<PropertyValue Property="Supported" Bool="true"/>
</Record>
</Annotation>
</Annotations>
<Annotations Target="microsoft.graph.directoryRole">
<Annotation Term="Org.OData.Capabilities.V1.ChangeTracking">
<Record>
Expand Down Expand Up @@ -20612,6 +20635,7 @@
<EntityType Name="extensionProperty" BaseType="graph.directoryObject" OpenType="true">
<Property Name="appDisplayName" Type="Edm.String"/>
<Property Name="dataType" Type="Edm.String" Nullable="false"/>
<Property Name="isMultiValued" Type="Edm.Boolean" Nullable="false"/>
<Property Name="isSyncedFromOnPremises" Type="Edm.Boolean"/>
<Property Name="name" Type="Edm.String" Nullable="false"/>
<Property Name="targetObjects" Type="Collection(Edm.String)" Nullable="false"/>
Expand Down Expand Up @@ -22504,6 +22528,7 @@
<Property Name="email" Type="Edm.String"/>
<Property Name="isFavoriteByDefault" Type="Edm.Boolean"/>
<Property Name="membershipType" Type="graph.channelMembershipType"/>
<Property Name="summary" Type="graph.channelSummary"/>
<Property Name="tenantId" Type="Edm.String"/>
<Property Name="webUrl" Type="Edm.String"/>
<NavigationProperty Name="filesFolder" Type="graph.driveItem" ContainsTarget="true"/>
Expand Down Expand Up @@ -22905,6 +22930,54 @@
<Property Name="isEnabledForContentSharing" Type="Edm.Boolean"/>
<Property Name="isEnabledForVideo" Type="Edm.Boolean"/>
</ComplexType>
<ComplexType Name="deviceLocalCredential">
<Property Name="accountName" Type="Edm.String" Nullable="false"/>
<Property Name="accountSid" Type="Edm.String" Nullable="false"/>
<Property Name="backupDateTime" Type="Edm.DateTimeOffset" Nullable="false"/>
<Property Name="passwordBase64" Type="Edm.String" Nullable="false"/>
</ComplexType>
<EntityType Name="deviceLocalCredentialInfo" BaseType="graph.entity">
<Property Name="credentials" Type="Collection(graph.deviceLocalCredential)" Nullable="false"/>
<Property Name="deviceName" Type="Edm.String" Nullable="false"/>
<Property Name="lastBackupDateTime" Type="Edm.DateTimeOffset" Nullable="false"/>
<Property Name="refreshDateTime" Type="Edm.DateTimeOffset" Nullable="false"/>
</EntityType>
<EntityType Name="directory" BaseType="graph.entity">
<NavigationProperty Name="deviceLocalCredentials" Type="Collection(graph.deviceLocalCredentialInfo)" ContainsTarget="true"/>
<NavigationProperty Name="administrativeUnits" Type="Collection(graph.administrativeUnit)" ContainsTarget="true"/>
<NavigationProperty Name="attributeSets" Type="Collection(graph.attributeSet)" ContainsTarget="true"/>
<NavigationProperty Name="customSecurityAttributeDefinitions" Type="Collection(graph.customSecurityAttributeDefinition)" ContainsTarget="true"/>
<NavigationProperty Name="deletedItems" Type="Collection(graph.directoryObject)" ContainsTarget="true"/>
<NavigationProperty Name="federationConfigurations" Type="Collection(graph.identityProviderBase)" ContainsTarget="true"/>
<NavigationProperty Name="onPremisesSynchronization" Type="Collection(graph.onPremisesDirectorySynchronization)" ContainsTarget="true"/>
</EntityType>
<EntityType Name="administrativeUnit" BaseType="graph.directoryObject" OpenType="true">
<Property Name="description" Type="Edm.String"/>
<Property Name="displayName" Type="Edm.String"/>
<Property Name="visibility" Type="Edm.String"/>
<NavigationProperty Name="members" Type="Collection(graph.directoryObject)"/>
<NavigationProperty Name="scopedRoleMembers" Type="Collection(graph.scopedRoleMembership)" ContainsTarget="true"/>
<NavigationProperty Name="extensions" Type="Collection(graph.extension)" ContainsTarget="true"/>
</EntityType>
<EntityType Name="attributeSet" BaseType="graph.entity">
<Property Name="description" Type="Edm.String"/>
<Property Name="maxAttributesPerSet" Type="Edm.Int32"/>
</EntityType>
<EntityType Name="customSecurityAttributeDefinition" BaseType="graph.entity">
<Property Name="attributeSet" Type="Edm.String" Nullable="false"/>
<Property Name="description" Type="Edm.String"/>
<Property Name="isCollection" Type="Edm.Boolean" Nullable="false"/>
<Property Name="isSearchable" Type="Edm.Boolean"/>
<Property Name="name" Type="Edm.String" Nullable="false"/>
<Property Name="status" Type="Edm.String" Nullable="false"/>
<Property Name="type" Type="Edm.String" Nullable="false"/>
<Property Name="usePreDefinedValuesOnly" Type="Edm.Boolean"/>
<NavigationProperty Name="allowedValues" Type="Collection(graph.allowedValue)" ContainsTarget="true"/>
</EntityType>
<EntityType Name="onPremisesDirectorySynchronization" BaseType="graph.entity">
<Property Name="configuration" Type="graph.onPremisesDirectorySynchronizationConfiguration"/>
<Property Name="features" Type="graph.onPremisesDirectorySynchronizationFeature" Nullable="false"/>
</EntityType>
<ComplexType Name="alternativeSecurityId">
<Property Name="identityProvider" Type="Edm.String"/>
<Property Name="key" Type="Edm.Binary"/>
Expand Down Expand Up @@ -23111,25 +23184,13 @@
<Property Name="name" Type="Edm.String"/>
<Property Name="type" Type="Edm.String"/>
</ComplexType>
<EntityType Name="administrativeUnit" BaseType="graph.directoryObject" OpenType="true">
<Property Name="description" Type="Edm.String"/>
<Property Name="displayName" Type="Edm.String"/>
<Property Name="visibility" Type="Edm.String"/>
<NavigationProperty Name="members" Type="Collection(graph.directoryObject)"/>
<NavigationProperty Name="scopedRoleMembers" Type="Collection(graph.scopedRoleMembership)" ContainsTarget="true"/>
<NavigationProperty Name="extensions" Type="Collection(graph.extension)" ContainsTarget="true"/>
</EntityType>
<EntityType Name="allowedValue" BaseType="graph.entity">
<Property Name="isActive" Type="Edm.Boolean"/>
</EntityType>
<EntityType Name="appScope" BaseType="graph.entity">
<Property Name="displayName" Type="Edm.String"/>
<Property Name="type" Type="Edm.String"/>
</EntityType>
<EntityType Name="attributeSet" BaseType="graph.entity">
<Property Name="description" Type="Edm.String"/>
<Property Name="maxAttributesPerSet" Type="Edm.Int32"/>
</EntityType>
<EntityType Name="certificateBasedAuthConfiguration" BaseType="graph.entity">
<Property Name="certificateAuthorities" Type="Collection(graph.certificateAuthority)" Nullable="false"/>
</EntityType>
Expand Down Expand Up @@ -23170,17 +23231,6 @@
<Property Name="tenantId" Type="Edm.String" Nullable="false"/>
<Property Name="userSyncInbound" Type="graph.crossTenantUserSyncInbound"/>
</EntityType>
<EntityType Name="customSecurityAttributeDefinition" BaseType="graph.entity">
<Property Name="attributeSet" Type="Edm.String" Nullable="false"/>
<Property Name="description" Type="Edm.String"/>
<Property Name="isCollection" Type="Edm.Boolean" Nullable="false"/>
<Property Name="isSearchable" Type="Edm.Boolean"/>
<Property Name="name" Type="Edm.String" Nullable="false"/>
<Property Name="status" Type="Edm.String" Nullable="false"/>
<Property Name="type" Type="Edm.String" Nullable="false"/>
<Property Name="usePreDefinedValuesOnly" Type="Edm.Boolean"/>
<NavigationProperty Name="allowedValues" Type="Collection(graph.allowedValue)" ContainsTarget="true"/>
</EntityType>
<EntityType Name="device" BaseType="graph.directoryObject" OpenType="true">
<Property Name="accountEnabled" Type="Edm.Boolean"/>
<Property Name="alternativeSecurityIds" Type="Collection(graph.alternativeSecurityId)" Nullable="false"/>
Expand Down Expand Up @@ -23225,18 +23275,6 @@
</Collection>
</Annotation>
</EntityType>
<EntityType Name="directory" BaseType="graph.entity">
<NavigationProperty Name="administrativeUnits" Type="Collection(graph.administrativeUnit)" ContainsTarget="true"/>
<NavigationProperty Name="attributeSets" Type="Collection(graph.attributeSet)" ContainsTarget="true"/>
<NavigationProperty Name="customSecurityAttributeDefinitions" Type="Collection(graph.customSecurityAttributeDefinition)" ContainsTarget="true"/>
<NavigationProperty Name="deletedItems" Type="Collection(graph.directoryObject)" ContainsTarget="true"/>
<NavigationProperty Name="federationConfigurations" Type="Collection(graph.identityProviderBase)" ContainsTarget="true"/>
<NavigationProperty Name="onPremisesSynchronization" Type="Collection(graph.onPremisesDirectorySynchronization)" ContainsTarget="true"/>
</EntityType>
<EntityType Name="onPremisesDirectorySynchronization" BaseType="graph.entity">
<Property Name="configuration" Type="graph.onPremisesDirectorySynchronizationConfiguration"/>
<Property Name="features" Type="graph.onPremisesDirectorySynchronizationFeature" Nullable="false"/>
</EntityType>
<EntityType Name="directoryObjectPartnerReference" BaseType="graph.directoryObject">
<Property Name="description" Type="Edm.String"/>
<Property Name="displayName" Type="Edm.String"/>
Expand Down Expand Up @@ -31068,6 +31106,12 @@
<Property Name="channelId" Type="Edm.String"/>
<Property Name="initiator" Type="graph.identitySet"/>
</ComplexType>
<ComplexType Name="channelSummary">
<Property Name="guestsCount" Type="Edm.Int32"/>
<Property Name="hasMembersFromOtherTenants" Type="Edm.Boolean"/>
<Property Name="membersCount" Type="Edm.Int32"/>
<Property Name="ownersCount" Type="Edm.Int32"/>
</ComplexType>
<ComplexType Name="channelUnsetAsFavoriteByDefaultEventMessageDetail" BaseType="graph.eventMessageDetail">
<Property Name="channelId" Type="Edm.String"/>
<Property Name="initiator" Type="graph.identitySet"/>
Expand Down
19 changes: 13 additions & 6 deletions office365/onedrive/driveitems/driveItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
from office365.onedrive.folders.folder import Folder
from office365.onedrive.internal.paths.children import ChildrenPath
from office365.onedrive.internal.paths.url import UrlPath
from office365.onedrive.internal.queries.resumable_file_upload import (
create_resumable_file_upload_query,
)
from office365.onedrive.listitems.item_reference import ItemReference
from office365.onedrive.listitems.list_item import ListItem
from office365.onedrive.operations.pending import PendingOperations
Expand All @@ -50,6 +47,7 @@
from office365.runtime.http.http_method import HttpMethod
from office365.runtime.http.request_options import RequestOptions
from office365.runtime.odata.v4.upload_session import UploadSession
from office365.runtime.odata.v4.upload_session_request import UploadSessionRequest
from office365.runtime.paths.resource_path import ResourcePath
from office365.runtime.queries.create_entity import CreateEntityQuery
from office365.runtime.queries.function import FunctionQuery
Expand Down Expand Up @@ -198,12 +196,21 @@ def resumable_upload(self, source_path, chunk_size=2000000, chunk_uploaded=None)
:param str source_path: File path
:param int chunk_size: chunk size
"""

def _start_upload():
with open(source_path, "rb") as local_file:
session_request = UploadSessionRequest(
local_file, chunk_size, chunk_uploaded
)
session_request.execute_query(qry)

file_name = os.path.basename(source_path)
return_type = DriveItem(self.context, UrlPath(file_name, self.resource_path))
qry = create_resumable_file_upload_query(
return_type, source_path, chunk_size, chunk_uploaded

qry = UploadSessionQuery(
return_type, {"item": DriveItemUploadableProperties(name=file_name)}
)
self.context.add_query(qry)
self.context.add_query(qry).after_query_execute(_start_upload)
return return_type

def create_upload_session(self, item):
Expand Down
Loading

0 comments on commit bcc6621

Please sign in to comment.