@@ -37,8 +37,7 @@ Alternatively, you can clone this repository and install manually:
37
37
``` bash
38
38
git clone
[email protected] :cs3org/cs3-python-client.git
39
39
cd cs3-python-client
40
- pip install -e .
41
- export PYTHONPATH=" path/to/cs3-python-client/:$PYTHONPATH "
40
+ pip install .
42
41
```
43
42
44
43
@@ -89,12 +88,14 @@ ssl_verify = False
89
88
# Optional, defaults to an empty string
90
89
ssl_client_cert = test_client_cert
91
90
# Optional, defaults to an empty string
92
- ssl_client_key = test_client_key
91
+ ssl_client_key = test_client_key
93
92
# Optional, defaults to an empty string
94
- ssl_ca_cert = test_ca_cert
93
+ ssl_ca_cert = test_ca_cert
95
94
96
95
# Optinal, defaults to an empty string
97
96
auth_client_id = einstein
97
+ # Optional (can also be set when instansiating the class)
98
+ auth_client_secret = relativity
98
99
# Optional, defaults to basic
99
100
auth_login_type = basic
100
101
@@ -112,129 +113,180 @@ lock_expiration = 1800
112
113
113
114
To use ` cs3client ` , you first need to import and configure it. Here's a simple example of how to set up and start using the client. For configuration see [ Configuration] ( #configuration ) . For more in depth examples see ` cs3-python-client/examples/ ` .
114
115
115
- ### Initilization
116
+ ### Initilization and Authentication
116
117
``` python
117
118
import logging
118
119
import configparser
119
- from cs3client import CS3Client
120
- from cs3resource import Resource
120
+ from cs3client.cs3client import CS3Client
121
+ from cs3client.auth import Auth
121
122
122
123
config = configparser.ConfigParser()
123
124
with open (" default.conf" ) as fdef:
124
125
config.read_file(fdef)
125
-
126
126
log = logging.getLogger(__name__ )
127
127
128
128
client = CS3Client(config, " cs3client" , log)
129
- # client.auth.set_token("<your_token_here>")
130
- # OR
131
- client.auth.set_client_secret(" <your_client_secret_here>" )
129
+ auth = Auth(client)
130
+ # Set the client id (can also be set in the config)
131
+ auth.set_client_id(" <your_client_id_here>" )
132
+ # Set client secret (can also be set in config)
133
+ auth.set_client_secret(" <your_client_secret_here>" )
134
+ # Checks if token is expired if not return ('x-access-token', <token>)
135
+ # if expired, request a new token from reva
136
+ auth_token = auth.get_token()
137
+
138
+ # OR if you already have a reva token
139
+ # Checks if token is expired if not return (x-access-token', <token>)
140
+ # if expired, throws an AuthenticationException (so you can refresh your reva token)
141
+ token = " <your_reva_token>"
142
+ auth_token = Auth.check_token(token)
143
+
132
144
```
133
145
134
146
### File Example
135
147
``` python
136
148
# mkdir
137
- directory_resource = Resource.from_file_ref_and_endpoint( f " /eos/user/r/rwelande/test_directory " )
138
- res = client.file.make_dir(directory_resource)
149
+ directory_resource = Resource( abs_path = f " /eos/user/r/rwelande/test_directory " )
150
+ res = client.file.make_dir(auth.get_token(), directory_resource)
139
151
140
152
# touchfile
141
- touch_resource = Resource.from_file_ref_and_endpoint( " /eos/user/r/rwelande/touch_file.txt" )
142
- res = client.file.touch_file(touch_resource)
153
+ touch_resource = Resource( abs_path = " /eos/user/r/rwelande/touch_file.txt" )
154
+ res = client.file.touch_file(auth.get_token(), touch_resource)
143
155
144
156
# setxattr
145
- resource = Resource.from_file_ref_and_endpoint( " /eos/user/r/rwelande/text_file.txt" )
146
- res = client.file.set_xattr(resource, " iop.wopi.lastwritetime" , str (1720696124 ))
157
+ resource = Resource( abs_path = " /eos/user/r/rwelande/text_file.txt" )
158
+ res = client.file.set_xattr(auth.get_token(), resource, " iop.wopi.lastwritetime" , str (1720696124 ))
147
159
148
160
# rmxattr
149
- res = client.file.remove_xattr(resource, " iop.wopi.lastwritetime" )
161
+ res = client.file.remove_xattr(auth.get_token(), resource, " iop.wopi.lastwritetime" )
150
162
151
163
# stat
152
- res = client.file.stat(resource)
164
+ res = client.file.stat(auth.get_token(), resource)
153
165
154
166
# removefile
155
- res = client.file.remove_file(touch_resource)
167
+ res = client.file.remove_file(auth.get_token(), touch_resource)
156
168
157
169
# rename
158
- rename_resource = Resource.from_file_ref_and_endpoint( " /eos/user/r/rwelande/rename_file.txt" )
159
- res = client.file.rename_file(resource, rename_resource)
170
+ rename_resource = Resource( abs_path = " /eos/user/r/rwelande/rename_file.txt" )
171
+ res = client.file.rename_file(auth.get_token(), resource, rename_resource)
160
172
161
173
# writefile
162
174
content = b " Hello World"
163
175
size = len (content)
164
- res = client.file.write_file(rename_resource, content, size)
176
+ res = client.file.write_file(auth.get_token(), rename_resource, content, size)
165
177
166
178
# listdir
167
- list_directory_resource = Resource.from_file_ref_and_endpoint( " /eos/user/r/rwelande" )
168
- res = client.file.list_dir(list_directory_resource)
179
+ list_directory_resource = Resource( abs_path = " /eos/user/r/rwelande" )
180
+ res = client.file.list_dir(auth.get_token(), list_directory_resource)
169
181
170
182
171
183
# readfile
172
- file_res = client.file.read_file(rename_resource)
184
+ file_res = client.file.read_file(auth.get_token(), rename_resource)
185
+ ```
186
+ ### Lock Example
187
+ ``` python
188
+
189
+ WEBDAV_LOCK_PREFIX = ' opaquelocktoken:797356a8-0500-4ceb-a8a0-c94c8cde7eba'
190
+
191
+
192
+ def encode_lock (lock ):
193
+ ''' Generates the lock payload for the storage given the raw metadata'''
194
+ if lock:
195
+ return WEBDAV_LOCK_PREFIX + ' ' + b64encode(lock.encode()).decode()
196
+ return None
197
+
198
+ resource = Resource(abs_path = " /eos/user/r/rwelande/lock_test.txt" )
199
+
200
+ # Set lock
201
+ client.file.set_lock(auth_token, resource, app_name = " a" , lock_id = encode_lock(" some_lock" ))
202
+
203
+ # Get lock
204
+ res = client.file.get_lock(auth_token, resource)
205
+ if res is not None :
206
+ lock_id = res[" lock_id" ]
207
+ print (res)
208
+
209
+ # Unlock
210
+ res = client.file.unlock(auth_token, resource, app_name = " a" , lock_id = lock_id)
211
+
212
+ # Refresh lock
213
+ client.file.set_lock(auth_token, resource, app_name = " a" , lock_id = encode_lock(" some_lock" ))
214
+ res = client.file.refresh_lock(
215
+ auth_token, resource, app_name = " a" , lock_id = encode_lock(" new_lock" ), existing_lock_id = lock_id
216
+ )
217
+
218
+ if res is not None :
219
+ print (res)
220
+
221
+ res = client.file.get_lock(auth_token, resource)
222
+ if res is not None :
223
+ print (res)
224
+
173
225
```
174
226
175
227
### Share Example
176
228
``` python
177
229
# Create share #
178
- resource = Resource.from_file_ref_and_endpoint( " /eos/user/r/<some_username>/text.txt" )
179
- resource_info = client.file.stat(resource)
230
+ resource = Resource( abs_path = " /eos/user/r/<some_username>/text.txt" )
231
+ resource_info = client.file.stat(auth.get_token(), resource)
180
232
user = client.user.get_user_by_claim(" username" , " <some_username>" )
181
- res = client.share.create_share(resource_info, user.id.opaque_id, user.id.idp, " EDITOR" , " USER" )
233
+ res = client.share.create_share(auth.get_token(), resource_info, user.id.opaque_id, user.id.idp, " EDITOR" , " USER" )
182
234
183
235
# List existing shares #
184
236
filter_list = []
185
237
filter = client.share.create_share_filter(resource_id = resource_info.id, filter_type = " TYPE_RESOURCE_ID" )
186
238
filter_list.append(filter )
187
239
filter = client.share.create_share_filter(share_state = " SHARE_STATE_PENDING" , filter_type = " TYPE_STATE" )
188
240
filter_list.append(filter )
189
- res, _ = client.share.list_existing_shares()
241
+ res, _ = client.share.list_existing_shares(auth.get_token(), )
190
242
191
243
# Get share #
192
244
share_id = " 58"
193
- res = client.share.get_share(opaque_id = share_id)
245
+ res = client.share.get_share(auth.get_token(), opaque_id = share_id)
194
246
195
247
# update share #
196
- res = client.share.update_share(opaque_id = share_id, role = " VIEWER" )
248
+ res = client.share.update_share(auth.get_token(), opaque_id = share_id, role = " VIEWER" )
197
249
198
250
# remove share #
199
- res = client.share.remove_share(opaque_id = share_id)
251
+ res = client.share.remove_share(auth.get_token(), opaque_id = share_id)
200
252
201
253
# List existing received shares #
202
254
filter_list = []
203
255
filter = client.share.create_share_filter(share_state = " SHARE_STATE_ACCEPTED" , filter_type = " TYPE_STATE" )
204
256
filter_list.append(filter )
205
- res, _ = client.share.list_received_existing_shares()
257
+ res, _ = client.share.list_received_existing_shares(auth.get_token() )
206
258
207
259
# get received share #
208
- received_share = client.share.get_received_share(opaque_id = share_id)
260
+ received_share = client.share.get_received_share(auth.get_token(), opaque_id = share_id)
209
261
210
262
# update recieved share #
211
- res = client.share.update_received_share(received_share = received_share, state = " SHARE_STATE_ACCEPTED" )
263
+ res = client.share.update_received_share(auth.get_token(), received_share = received_share, state = " SHARE_STATE_ACCEPTED" )
212
264
213
265
# create public share #
214
- res = client.share.create_public_share(resource_info, role = " VIEWER" )
266
+ res = client.share.create_public_share(auth.get_token(), resource_info, role = " VIEWER" )
215
267
216
268
# list existing public shares #
217
269
filter_list = []
218
270
filter = client.share.create_public_share_filter(resource_id = resource_info.id, filter_type = " TYPE_RESOURCE_ID" )
219
271
filter_list.append(filter )
220
272
res, _ = client.share.list_existing_public_shares(filter_list = filter_list)
221
273
222
- res = client.share.get_public_share(opaque_id = share_id, sign = True )
274
+ res = client.share.get_public_share(auth.get_token(), opaque_id = share_id, sign = True )
223
275
# OR token = "<token>"
224
276
# res = client.share.get_public_share(token=token, sign=True)
225
277
226
278
# update public share #
227
- res = client.share.update_public_share(type = " TYPE_PASSWORD" , token = token, role = " VIEWER" , password = " hello" )
279
+ res = client.share.update_public_share(auth.get_token(), type = " TYPE_PASSWORD" , token = token, role = " VIEWER" , password = " hello" )
228
280
229
281
# remove public share #
230
- res = client.share.remove_public_share(token = token)
282
+ res = client.share.remove_public_share(auth.get_token(), token = token)
231
283
232
284
```
233
285
234
286
### User Example
235
287
``` python
236
288
# find_user
237
- res = client.user.find_users(" rwel" )
289
+ res = client.user.find_users(auth.get_token(), " rwel" )
238
290
239
291
# get_user
240
292
res = client.user.get_user(" https://auth.cern.ch/auth/realms/cern" , " asdoiqwe" )
@@ -253,21 +305,21 @@ res = client.user.get_user_by_claim("username", "rwelande")
253
305
### App Example
254
306
``` python
255
307
# list_app_providers
256
- res = client.app.list_app_providers()
308
+ res = client.app.list_app_providers(auth.get_token() )
257
309
258
310
# open_in_app
259
- resource = Resource.from_file_ref_and_endpoint( " /eos/user/r/rwelande/collabora.odt" )
260
- res = client.app.open_in_app(resource)
311
+ resource = Resource( abs_path = " /eos/user/r/rwelande/collabora.odt" )
312
+ res = client.app.open_in_app(auth.get_token(), resource)
261
313
```
262
314
263
315
### Checkpoint Example
264
316
``` python
265
317
# list file versions
266
- resource = Resource.from_file_ref_and_endpoint( " /eos/user/r/rwelande/test.md" )
267
- res = client.checkpoint.list_file_versions(resource)
318
+ resource = Resource( abs_path = " /eos/user/r/rwelande/test.md" )
319
+ res = client.checkpoint.list_file_versions(auth.get_token(), resource)
268
320
269
321
# restore file version
270
- res = client.checkpoint.restore_file_version(resource, " 1722936250.0569fa2f" )
322
+ res = client.checkpoint.restore_file_version(auth.get_token(), resource, " 1722936250.0569fa2f" )
271
323
```
272
324
273
325
## Documentation
0 commit comments