brynq-sdk-sharepoint 1.1.1__tar.gz → 2.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 1.0
2
2
  Name: brynq_sdk_sharepoint
3
- Version: 1.1.1
3
+ Version: 2.0.0
4
4
  Summary: Sharepoint wrapper from BrynQ
5
5
  Home-page: UNKNOWN
6
6
  Author: BrynQ
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 1.0
2
2
  Name: brynq-sdk-sharepoint
3
- Version: 1.1.1
3
+ Version: 2.0.0
4
4
  Summary: Sharepoint wrapper from BrynQ
5
5
  Home-page: UNKNOWN
6
6
  Author: BrynQ
@@ -1,6 +1,4 @@
1
1
  setup.py
2
- brynq_sdk/sharepoint/__init__.py
3
- brynq_sdk/sharepoint/sharepoint.py
4
2
  brynq_sdk_sharepoint.egg-info/PKG-INFO
5
3
  brynq_sdk_sharepoint.egg-info/SOURCES.txt
6
4
  brynq_sdk_sharepoint.egg-info/dependency_links.txt
@@ -0,0 +1,2 @@
1
+ brynq-sdk-brynq>=2
2
+ requests<=3,>=2
@@ -1,17 +1,16 @@
1
- from setuptools import setup
2
-
1
+ from setuptools import setup, find_namespace_packages
3
2
 
4
3
  setup(
5
4
  name='brynq_sdk_sharepoint',
6
- version='1.1.1',
5
+ version='2.0.0',
7
6
  description='Sharepoint wrapper from BrynQ',
8
7
  long_description='Sharepoint wrapper from BrynQ',
9
8
  author='BrynQ',
10
9
  author_email='support@brynq.com',
11
- packages=["brynq_sdk.sharepoint"],
10
+ packages=find_namespace_packages(include=['brynq_sdk*']),
12
11
  license='BrynQ License',
13
12
  install_requires=[
14
- 'brynq-sdk-brynq>=1',
13
+ 'brynq-sdk-brynq>=2',
15
14
  'requests>=2,<=3'
16
15
  ],
17
16
  zip_safe=False,
@@ -1 +0,0 @@
1
- from brynq_sdk.sharepoint.sharepoint import Sharepoint
@@ -1,249 +0,0 @@
1
- import os
2
- from typing import List, Union
3
- import requests
4
- import json
5
- from io import BytesIO
6
- import typing
7
- from brynq_sdk.brynq import BrynQ
8
-
9
-
10
- class Sharepoint(BrynQ):
11
- def __init__(self, label: Union[str, List], site: str = None, site_id: str = None, json_subset: int = None, site_name: str = None, debug: bool = False):
12
- """
13
- :param label: label of the sharepoint system in BrynQ
14
- :param site: base url of the sharepoint site
15
- :param site_id: site id of the sharepoint site
16
- :param json_subset: fill in the part of the json that needs to be accessed to get the wanted drive id, accompanying the drive you are looking for
17
- :param debug: set to True to enable debug logging
18
- """
19
- super().__init__()
20
- credentials = self.get_system_credential(system='sharepoint', label=label)
21
- self.debug = debug
22
- if self.debug:
23
- print(f"credentials: {credentials}")
24
- self.access_token = credentials['auth']['access_token']
25
- self.brynq_system_id = credentials['id']
26
- if site_name is not None:
27
- self.json_subset = 0 if json_subset is None else json_subset
28
- self.site_id = self.get_site_id(site_name=site_name)
29
- elif site_id is not None:
30
- self.site_id = f"{site},{site_id}"
31
- self.json_subset = json_subset
32
- else:
33
- raise KeyError('Either site_name or site_id, site and json_subset must be provided')
34
- if self.debug:
35
- print(f"site_id: {self.site_id}, json_subset: {self.json_subset}, credentials: {credentials}, brynq_system_id: {self.brynq_system_id}")
36
-
37
- def _get_sharepoint_headers(self):
38
- access_token = self.refresh_system_credential(system='sharepoint', system_id=self.brynq_system_id)['access_token']
39
- headers = {'Authorization': f'Bearer {access_token}'}
40
- if self.debug:
41
- print(headers)
42
-
43
- return headers
44
-
45
- def get_site_id(self, site_name: str) -> str:
46
- """
47
- Get the site id of a site
48
- :param site_name: name of the site
49
- :return: site id
50
- """
51
- url = f'https://graph.microsoft.com/v1.0/sites?search={site_name}'
52
- if self.debug:
53
- print(f"url: {url}")
54
- response = requests.get(url=url, headers=self._get_sharepoint_headers())
55
- response.raise_for_status()
56
- site_id = response.json()['value'][0]['id']
57
- if self.debug:
58
- print(f"site_id: {site_id}")
59
-
60
- return site_id
61
-
62
- def get_driveid(self):
63
- """
64
- This method is used to derive the driveid to which the files have to be uploaded. Needed in the upload url for file upload.
65
- :return: returns the needed driveid
66
- """
67
- url = f'https://graph.microsoft.com/v1.0/sites/{self.site_id}/drives'
68
- if self.debug:
69
- print(f"url: {url}")
70
- response = requests.get(url, headers=self._get_sharepoint_headers())
71
- response.raise_for_status()
72
- drive_id = response.json()['value'][self.json_subset]['id']
73
- if self.debug:
74
- print(f"drive_id: {drive_id}")
75
-
76
- return drive_id
77
-
78
- def upload_file(self, local_file_path: str, remote_file_path: str):
79
- """
80
- This method performs the actual file upload to the formerly derived site + drive.
81
- local_file_path: local path of the file you want to upload
82
- remote_file_path: remote path of the folder and filename where you want to place the file
83
- """
84
- drive_id = self.get_driveid()
85
- url = f'https://graph.microsoft.com/v1.0/sites/{self.site_id}/drives/{drive_id}/root:/{remote_file_path}:/createUploadSession'
86
- if self.debug:
87
- print(f"url: {url}")
88
- headers = self._get_sharepoint_headers()
89
- response = requests.post(url, headers=headers)
90
- response.raise_for_status()
91
- upload_url = response.json()['uploadUrl']
92
- if self.debug:
93
- print(f"upload_url: {upload_url}")
94
- with open(f'{local_file_path}', 'rb') as file_input:
95
- file_bytes = os.path.getsize(f'{local_file_path}')
96
- headers_upload = {'Content-Type': 'application/json',
97
- 'Content-Length': f'{file_bytes}',
98
- 'Content-Range': f'bytes 0-{file_bytes - 1}/{file_bytes}'}
99
- response_upload = requests.put(url=upload_url, headers=headers_upload, data=file_input)
100
- response_upload.raise_for_status()
101
-
102
- return response_upload
103
-
104
- def open_file(self, remote_file_path: str) -> bytes:
105
- """
106
- Get a file from sharepoint as a bytesstream
107
- remote_file_path: filepath on sharepoint
108
- :return: bytes of file object
109
- """
110
- drive_id = self.get_driveid()
111
- url = f'https://graph.microsoft.com/v1.0/sites/{self.site_id}/drives/{drive_id}/root:/{remote_file_path}'
112
- if self.debug:
113
- print(f"url: {url}")
114
- headers = self._get_sharepoint_headers()
115
- response = requests.get(url=url, headers=headers)
116
- response.raise_for_status()
117
- download_url = response.json()['@microsoft.graph.downloadUrl']
118
- if self.debug:
119
- print(f"download_url: {download_url}")
120
- response_download = requests.get(url=download_url, headers=headers)
121
- response_download.raise_for_status()
122
-
123
- return response_download.content
124
-
125
- def download_file(self, local_file_path: str, remote_file_path: str):
126
- """
127
- This method downloads a file from sharepoint to the local machine.
128
- local_file_path: local folder where the file will be downloaded to
129
- remote_file_path: remote path of the file on sharepoint
130
- """
131
- driveid = self.get_driveid()
132
- url = f'https://graph.microsoft.com/v1.0/sites/{self.site_id}/drives/{driveid}/root:/{remote_file_path}'
133
- headers = self._get_sharepoint_headers()
134
- response = requests.get(url=url, headers=headers)
135
- response.raise_for_status()
136
- download_url = response.json()['@microsoft.graph.downloadUrl']
137
- response_download = requests.get(url=download_url, headers=headers)
138
- response_download.raise_for_status()
139
- with open(file=f'{local_file_path}', mode='wb') as f:
140
- f.write(BytesIO(response_download.content).read())
141
-
142
- return response_download
143
-
144
- def download_files(self, local_folder_path: str, remote_folder_path: str):
145
- """
146
- This method downloads a file from sharepoint to the local machine.
147
- local_folder_path: local folder where the files will be downloaded to
148
- remote_folder_path: remote path of the folder you want to get on sharepoint
149
- """
150
- driveid = self.get_driveid()
151
- folder_content = self.list_dir(remote_folder_path=remote_folder_path)
152
- # remove subdirectories, can not be downloaded
153
- folder_content = [item for item in folder_content if 'file' in item]
154
- if self.debug:
155
- print(f"folder_content: {folder_content}")
156
- filecount = 0
157
-
158
- responses = []
159
- for file in folder_content:
160
- url = f'https://graph.microsoft.com/v1.0/sites/{self.site_id}/drives/{driveid}/root:/{remote_folder_path}{file["name"]}'
161
- if self.debug:
162
- print(f"url: {url}")
163
- headers = self._get_sharepoint_headers()
164
- response = requests.get(url=url, headers=headers)
165
- response.raise_for_status()
166
- download_url = response.json()['@microsoft.graph.downloadUrl']
167
- response_download = requests.get(url=download_url, headers=headers)
168
- with open(file=f'{local_folder_path}{file["name"]}', mode='wb') as f:
169
- f.write(BytesIO(response_download.content).read())
170
- filecount += 1
171
- responses.append(response_download)
172
- print(f'{filecount} files downloaded')
173
-
174
- return responses
175
-
176
- def list_dir(self, remote_folder_path: str, get_files_from_nested_folders: bool = False) -> [json, typing.Generator]:
177
- """
178
- Fetch the contents of the API and return the "children"
179
- which has the information of all the items under that folder
180
- remote_folder_path: folder path you want to list
181
- :return: all the contents of the folder items
182
- """
183
- if get_files_from_nested_folders:
184
- return list(self._get_all_files_in_folder(folder_path=remote_folder_path))
185
-
186
- drive_id = self.get_driveid()
187
- url = f'https://graph.microsoft.com/v1.0/sites/{self.site_id}/drives/{drive_id}/root:/{remote_folder_path}?expand=children'
188
- if self.debug:
189
- print(f"url: {url}")
190
- response = requests.get(url, headers=self._get_sharepoint_headers(), timeout=120)
191
- response.raise_for_status()
192
-
193
- return response.json()['children']
194
-
195
- # helpers function to get all files in a nested directory
196
- def _get_all_files_in_folder(self, folder_path) -> typing.Generator:
197
- children = self.list_dir(remote_folder_path=folder_path)
198
- for child in children:
199
- if 'file' in child:
200
- yield {"folder": folder_path, "file": child['name'], "id": child['id']}
201
- else:
202
- yield from self._get_all_files_in_folder(folder_path=f"{folder_path}/{child['name']}")
203
-
204
- def remove_file(self, remote_file_path: str):
205
- """
206
- Remove a file from Sharepoint
207
- remote_file_path: complete path including filename
208
- :return: response from Sharepoint
209
- """
210
- drive_id = self.get_driveid()
211
- url = f'https://graph.microsoft.com/v1.0/sites/{self.site_id}/drives/{drive_id}/root:/{remote_file_path}'
212
- if self.debug:
213
- print(f"url: {url}")
214
- response = requests.delete(url=url, headers=self._get_sharepoint_headers())
215
- response.raise_for_status()
216
-
217
- return response
218
-
219
- def remove_files(self, remote_folder_path: str):
220
- """
221
- Remove a file from Sharepoint
222
- remote_folder_path: folder path that you want to empty
223
- """
224
- drive_id = self.get_driveid()
225
- folder_content = self.list_dir(remote_folder_path=remote_folder_path)
226
- responses = []
227
- for file in folder_content:
228
- url = f'https://graph.microsoft.com/v1.0/sites/{self.site_id}/drives/{drive_id}/root:/{remote_folder_path}{file["name"]}'
229
- if self.debug:
230
- print(f"url: {url}")
231
- response = requests.delete(url=url, headers=self._get_sharepoint_headers())
232
- response.raise_for_status()
233
- responses.append(response)
234
-
235
- return responses
236
-
237
- def remove_folder(self, folder_id: str):
238
- """
239
- Remove a folder from Sharepoint
240
- folder: folder id that you want to delete
241
- """
242
- drive_id = self.get_driveid()
243
- url = f'https://graph.microsoft.com/v1.0/sites/{self.site_id}/drives/{drive_id}/items/{folder_id}'
244
- if self.debug:
245
- print(f"url: {url}")
246
- response = requests.delete(url=url, headers=self._get_sharepoint_headers())
247
- response.raise_for_status()
248
-
249
- return response
@@ -1,2 +0,0 @@
1
- brynq-sdk-brynq>=1
2
- requests<=3,>=2