Repository navigation
Expand file tree
/
Copy pathfiles.py
More file actions
193 lines (147 loc) · 7.83 KB
/
Copy pathfiles.py
File metadata and controls
193 lines (147 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import base64
import requests
from microsoftgraph.decorators import token_required
from microsoftgraph.response import Response
class Files(object):
def __init__(self, client) -> None:
"""Working with files in Microsoft Graph
https://docs.microsoft.com/en-us/graph/api/resources/onedrive?view=graph-rest-1.0
Args:
client (Client): Library Client.
"""
self._client = client
@token_required
def drive_root_items(self, params: dict = None) -> Response:
"""Return a collection of DriveItems in the children relationship of a DriveItem.
https://docs.microsoft.com/en-us/graph/api/driveitem-list-children?view=graph-rest-1.0&tabs=http
Args:
params (dict, optional): Query. Defaults to None.
Returns:
Response: Microsoft Graph Response.
"""
return self._client._get(self._client.base_url + "me/drive/root", params=params)
@token_required
def drive_root_children_items(self, params: dict = None) -> Response:
"""Return a collection of DriveItems in the children relationship of a DriveItem.
https://docs.microsoft.com/en-us/graph/api/driveitem-list-children?view=graph-rest-1.0&tabs=http
Args:
params (dict, optional): Query. Defaults to None.
Returns:
Response: Microsoft Graph Response.
"""
return self._client._get(self._client.base_url + "me/drive/root/children", params=params)
@token_required
def drive_specific_folder(self, folder_id: str, params: dict = None) -> Response:
"""Return a collection of DriveItems in the children relationship of a DriveItem.
https://docs.microsoft.com/en-us/graph/api/driveitem-list-children?view=graph-rest-1.0&tabs=http
Args:
folder_id (str): Unique identifier of the folder.
params (dict, optional): Query. Defaults to None.
Returns:
Response: Microsoft Graph Response.
"""
url = "me/drive/items/{}/children".format(folder_id)
return self._client._get(self._client.base_url + url, params=params)
@token_required
def drive_get_item(self, item_id: str, params: dict = None, **kwargs) -> Response:
"""Retrieve the metadata for a driveItem in a drive by file system path or ID. It may also be the unique ID of a
SharePoint list item.
https://docs.microsoft.com/en-us/graph/api/driveitem-get?view=graph-rest-1.0&tabs=http
Args:
item_id (str): ID of a driveItem.
params (dict, optional): Query. Defaults to None.
Returns:
Response: Microsoft Graph Response.
"""
url = "me/drive/items/{}".format(item_id)
return self._client._get(self._client.base_url + url, params=params, **kwargs)
@token_required
def drive_download_contents(self, item_id: str, params: dict = None, **kwargs) -> Response:
"""Download the contents of the primary stream (file) of a DriveItem. Only driveItems with the file property can
be downloaded.
https://docs.microsoft.com/en-us/graph/api/driveitem-get-content?view=graph-rest-1.0&tabs=http
Args:
item_id (str): ID of a driveItem.
params (dict, optional): Extra params. Defaults to None.
Returns:
Response: Microsoft Graph Response.
"""
url = "me/drive/items/{}/content".format(item_id)
return self._client._get(self._client.base_url + url, params=params, **kwargs)
@token_required
def drive_download_shared_contents(self, share_id: str, params: dict = None, **kwargs) -> Response:
"""Download the contents of the primary stream (file) of a DriveItem. Only driveItems with the file property can
be downloaded.
https://docs.microsoft.com/en-us/graph/api/driveitem-get-content?view=graph-rest-1.0&tabs=http
Args:
share_id (str): ID of a driveItem.
params (dict, optional): Extra params. Defaults to None.
Returns:
Response: Microsoft Graph Response.
"""
base64_value = base64.b64encode(share_id.encode()).decode()
encoded_share_url = "u!" + base64_value.rstrip("=").replace("/", "_").replace("+", "-")
url = self._client.base_url + "shares/{}/driveItem".format(encoded_share_url)
drive_item = self._client._get(url)
file_download_url = drive_item["@microsoft.graph.downloadUrl"]
return drive_item["name"], requests.get(file_download_url).content
@token_required
def drive_download_large_contents(self, downloadUrl: str, offset: int, size: int) -> Response:
"""Download the contents of the primary stream (file) of a DriveItem. Only driveItems with the file property can
be downloaded.
https://docs.microsoft.com/en-us/graph/api/driveitem-get-content?view=graph-rest-1.0&tabs=http
Args:
downloadUrl (str): Url of the driveItem.
offset (int): offset.
size (int): size.
Returns:
Response: Microsoft Graph Response.
"""
headers = {"Range": f"bytes={offset}-{size + offset - 1}"}
return self._client._get(downloadUrl, headers=headers)
@token_required
def drive_upload_new_file(self, filename: str, file_path: str, params: dict = None, **kwargs) -> Response:
"""The simple upload API allows you to provide the contents of a new file in a single API call. This method only
supports files up to 4MB in size.
https://docs.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http
Args:
filename (str): The name of the item (filename and extension).
file_path (str): File path to upload.
params (dict, optional): Extra params. Defaults to None.
Returns:
Response: Microsoft Graph Response.
"""
kwargs["headers"] = {"Content-Type": "text/plain"}
content = open(file_path, "rb").read()
url = "me/drive/root:{}:/content".format(filename)
return self._client._put(self._client.base_url + url, params=params, data=content, **kwargs)
@token_required
def drive_update_existing_file(self, item_id: str, file_path: str, params: dict = None, **kwargs) -> Response:
"""The simple upload API allows you to update the contents of an existing file in a single API call. This method
only supports files up to 4MB in size.
https://docs.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http
Args:
item_id (str): Id of a driveItem.
file_path (str): File path to upload.
params (dict, optional): Extra params. Defaults to None.
Returns:
Response: Microsoft Graph Response.
"""
kwargs["headers"] = {"Content-Type": "text/plain"}
content = open(file_path, "rb").read()
url = "me/drive/items/{}/content".format(item_id)
return self._client._put(self._client.base_url + url, params=params, data=content, **kwargs)
@token_required
def search_items(self, q: str, params: dict = None, **kwargs) -> Response:
"""Search the hierarchy of items for items matching a query. You can search within a folder hierarchy, a whole
drive, or files shared with the current user.
https://docs.microsoft.com/en-us/graph/api/driveitem-search?view=graph-rest-1.0&tabs=http
Args:
q (str): The query text used to search for items. Values may be matched across several fields including
filename, metadata, and file content.
params (dict, optional): Query. Defaults to None.
Returns:
Response: Microsoft Graph Response.
"""
url = "me/drive/root/search(q='{}')".format(q)
return self._client._get(self._client.base_url + url, params=params, **kwargs)