Skip to content

Commit 514fd2c

Browse files
author
Rasmus Oscar Welander
committed
Made tokens external to allow parallelism + CI updates
1 parent afe7044 commit 514fd2c

23 files changed

+403
-348
lines changed

.github/workflows/ci-tests.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ jobs:
3232
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
3333
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide, we further relax this
3434
flake8 . --count --exit-zero --max-complexity=30 --max-line-length=130 --statistics
35-
35+
- name: Build
36+
run: |
37+
pip install build
38+
python -m build
39+
- name: Install
40+
run: |
41+
pip install .
3642
- name: Test with pytest
3743
run: |
3844
if [ -f tests/requirements.txt ]; then pip install -r tests/requirements.txt; fi

README.md

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ Alternatively, you can clone this repository and install manually:
3737
```bash
3838
git clone git@github.com:cs3org/cs3-python-client.git
3939
cd cs3-python-client
40-
pip install -e .
41-
export PYTHONPATH="path/to/cs3-python-client/:$PYTHONPATH"
40+
pip install .
4241
```
4342

4443

@@ -112,129 +111,136 @@ lock_expiration = 1800
112111

113112
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/`.
114113

115-
### Initilization
114+
### Initilization and Authentication
116115
```python
117116
import logging
118117
import configparser
119118
from cs3client import CS3Client
120-
from cs3resource import Resource
121119

122120
config = configparser.ConfigParser()
123121
with open("default.conf") as fdef:
124122
config.read_file(fdef)
125-
126123
log = logging.getLogger(__name__)
127124

128125
client = CS3Client(config, "cs3client", log)
129-
# client.auth.set_token("<your_token_here>")
130-
# OR
126+
127+
# Set client secret
131128
client.auth.set_client_secret("<your_client_secret_here>")
129+
# Checks if token is expired if not return ('x-access-token', <token>)
130+
# if expired, request a new token from reva
131+
auth_token = client.auth.get_token()
132+
133+
# OR if you already have a reva token
134+
# Checks if token is expired if not return (x-access-token', <token>)
135+
# if expired, throws an AuthenticationException (so you can refresh your reva token)
136+
token = "<your_reva_token>"
137+
auth_token = client.auth.check_token(token)
132138
```
133139

134140
### File Example
135141
```python
136142
# mkdir
137143
directory_resource = Resource.from_file_ref_and_endpoint(f"/eos/user/r/rwelande/test_directory")
138-
res = client.file.make_dir(directory_resource)
144+
res = client.file.make_dir(client.auth.get_token(), directory_resource)
139145

140146
# touchfile
141147
touch_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/touch_file.txt")
142-
res = client.file.touch_file(touch_resource)
148+
res = client.file.touch_file(client.auth.get_token(), touch_resource)
143149

144150
# setxattr
145151
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))
152+
res = client.file.set_xattr(client.auth.get_token(), resource, "iop.wopi.lastwritetime", str(1720696124))
147153

148154
# rmxattr
149-
res = client.file.remove_xattr(resource, "iop.wopi.lastwritetime")
155+
res = client.file.remove_xattr(client.auth.get_token(), resource, "iop.wopi.lastwritetime")
150156

151157
# stat
152-
res = client.file.stat(resource)
158+
res = client.file.stat(client.auth.get_token(), resource)
153159

154160
# removefile
155-
res = client.file.remove_file(touch_resource)
161+
res = client.file.remove_file(client.auth.get_token(), touch_resource)
156162

157163
# rename
158164
rename_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/rename_file.txt")
159-
res = client.file.rename_file(resource, rename_resource)
165+
res = client.file.rename_file(client.auth.get_token(), resource, rename_resource)
160166

161167
# writefile
162168
content = b"Hello World"
163169
size = len(content)
164-
res = client.file.write_file(rename_resource, content, size)
170+
res = client.file.write_file(client.auth.get_token(), rename_resource, content, size)
165171

166172
# listdir
167173
list_directory_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande")
168-
res = client.file.list_dir(list_directory_resource)
174+
res = client.file.list_dir(client.auth.get_token(), list_directory_resource)
169175

170176

171177
# readfile
172-
file_res = client.file.read_file(rename_resource)
178+
file_res = client.file.read_file(client.auth.get_token(), rename_resource)
173179
```
174180

175181
### Share Example
176182
```python
177183
# Create share #
178184
resource = Resource.from_file_ref_and_endpoint("/eos/user/r/<some_username>/text.txt")
179-
resource_info = client.file.stat(resource)
185+
resource_info = client.file.stat(client.auth.get_token(), resource)
180186
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")
187+
res = client.share.create_share(client.auth.get_token(), resource_info, user.id.opaque_id, user.id.idp, "EDITOR", "USER")
182188

183189
# List existing shares #
184190
filter_list = []
185191
filter = client.share.create_share_filter(resource_id=resource_info.id, filter_type="TYPE_RESOURCE_ID")
186192
filter_list.append(filter)
187193
filter = client.share.create_share_filter(share_state="SHARE_STATE_PENDING", filter_type="TYPE_STATE")
188194
filter_list.append(filter)
189-
res, _ = client.share.list_existing_shares()
195+
res, _ = client.share.list_existing_shares(client.auth.get_token(), )
190196

191197
# Get share #
192198
share_id = "58"
193-
res = client.share.get_share(opaque_id=share_id)
199+
res = client.share.get_share(client.auth.get_token(), opaque_id=share_id)
194200

195201
# update share #
196-
res = client.share.update_share(opaque_id=share_id, role="VIEWER")
202+
res = client.share.update_share(client.auth.get_token(), opaque_id=share_id, role="VIEWER")
197203

198204
# remove share #
199-
res = client.share.remove_share(opaque_id=share_id)
205+
res = client.share.remove_share(client.auth.get_token(), opaque_id=share_id)
200206

201207
# List existing received shares #
202208
filter_list = []
203209
filter = client.share.create_share_filter(share_state="SHARE_STATE_ACCEPTED", filter_type="TYPE_STATE")
204210
filter_list.append(filter)
205-
res, _ = client.share.list_received_existing_shares()
211+
res, _ = client.share.list_received_existing_shares(client.auth.get_token())
206212

207213
# get received share #
208-
received_share = client.share.get_received_share(opaque_id=share_id)
214+
received_share = client.share.get_received_share(client.auth.get_token(), opaque_id=share_id)
209215

210216
# update recieved share #
211-
res = client.share.update_received_share(received_share=received_share, state="SHARE_STATE_ACCEPTED")
217+
res = client.share.update_received_share(client.auth.get_token(), received_share=received_share, state="SHARE_STATE_ACCEPTED")
212218

213219
# create public share #
214-
res = client.share.create_public_share(resource_info, role="VIEWER")
220+
res = client.share.create_public_share(client.auth.get_token(), resource_info, role="VIEWER")
215221

216222
# list existing public shares #
217223
filter_list = []
218224
filter = client.share.create_public_share_filter(resource_id=resource_info.id, filter_type="TYPE_RESOURCE_ID")
219225
filter_list.append(filter)
220226
res, _ = client.share.list_existing_public_shares(filter_list=filter_list)
221227

222-
res = client.share.get_public_share(opaque_id=share_id, sign=True)
228+
res = client.share.get_public_share(client.auth.get_token(), opaque_id=share_id, sign=True)
223229
# OR token = "<token>"
224230
# res = client.share.get_public_share(token=token, sign=True)
225231

226232
# update public share #
227-
res = client.share.update_public_share(type="TYPE_PASSWORD", token=token, role="VIEWER", password="hello")
233+
res = client.share.update_public_share(client.auth.get_token(), type="TYPE_PASSWORD", token=token, role="VIEWER", password="hello")
228234

229235
# remove public share #
230-
res = client.share.remove_public_share(token=token)
236+
res = client.share.remove_public_share(client.auth.get_token(), token=token)
231237

232238
```
233239

234240
### User Example
235241
```python
236242
# find_user
237-
res = client.user.find_users("rwel")
243+
res = client.user.find_users(client.auth.get_token(), "rwel")
238244

239245
# get_user
240246
res = client.user.get_user("https://auth.cern.ch/auth/realms/cern", "asdoiqwe")
@@ -253,21 +259,21 @@ res = client.user.get_user_by_claim("username", "rwelande")
253259
### App Example
254260
```python
255261
# list_app_providers
256-
res = client.app.list_app_providers()
262+
res = client.app.list_app_providers(client.auth.get_token())
257263

258264
# open_in_app
259265
resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/collabora.odt")
260-
res = client.app.open_in_app(resource)
266+
res = client.app.open_in_app(client.auth.get_token(), resource)
261267
```
262268

263269
### Checkpoint Example
264270
```python
265271
# list file versions
266272
resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/test.md")
267-
res = client.checkpoint.list_file_versions(resource)
273+
res = client.checkpoint.list_file_versions(client.auth.get_token(), resource)
268274

269275
# restore file version
270-
res = client.checkpoint.restore_file_version(resource, "1722936250.0569fa2f")
276+
res = client.checkpoint.restore_file_version(client.auth.get_token(), resource, "1722936250.0569fa2f")
271277
```
272278

273279
## Documentation

examples/app_api_example.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
88
Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch
9-
Last updated: 19/08/2024
9+
Last updated: 28/08/2024
1010
"""
1111

1212
import logging
@@ -17,24 +17,18 @@
1717
config = configparser.ConfigParser()
1818
with open("default.conf") as fdef:
1919
config.read_file(fdef)
20-
# log
2120
log = logging.getLogger(__name__)
2221

23-
2422
client = CS3Client(config, "cs3client", log)
25-
# client.auth.set_token("<your_token_here>")
26-
# OR
2723
client.auth.set_client_secret("<your_client_secret_here>")
2824

29-
print(client.auth.get_token())
30-
3125
# list_app_providers
32-
res = client.app.list_app_providers()
26+
res = client.app.list_app_providers(client.auth.get_token())
3327
if res is not None:
3428
print(res)
3529

3630
# open_in_app
3731
resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/collabora.odt")
38-
res = client.app.open_in_app(resource)
32+
res = client.app.open_in_app(client.auth.get_token(), resource)
3933
if res is not None:
4034
print(res)

examples/auth_example.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
auth_example.py
3+
4+
Example script to demonstrate the usage of the app API in the CS3Client class.
5+
note that these are examples, and is not meant to be run as a script.
6+
7+
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
8+
Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch
9+
Last updated: 28/08/2024
10+
"""
11+
12+
import logging
13+
import configparser
14+
from cs3client import CS3Client
15+
16+
config = configparser.ConfigParser()
17+
with open("default.conf") as fdef:
18+
config.read_file(fdef)
19+
log = logging.getLogger(__name__)
20+
21+
client = CS3Client(config, "cs3client", log)
22+
23+
# Set client secret
24+
client.auth.set_client_secret("<your_client_secret_here>")
25+
# Checks if token is expired if not return ('x-access-token', <token>)
26+
# if expired, request a new token from reva
27+
auth_token = client.auth.get_token()
28+
29+
# OR if you already have a reva token
30+
# Checks if token is expired if not return (x-access-token', <token>)
31+
# if expired, throws an AuthenticationException (so you can refresh your reva token)
32+
token = "<your_reva_token>"
33+
auth_token = client.auth.check_token(token)

examples/checkpoints_api_example.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
88
Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch
9-
Last updated: 19/08/2024
9+
Last updated: 28/08/2024
1010
"""
1111

1212
import logging
@@ -17,25 +17,22 @@
1717
config = configparser.ConfigParser()
1818
with open("default.conf") as fdef:
1919
config.read_file(fdef)
20-
# log
2120
log = logging.getLogger(__name__)
2221

2322
client = CS3Client(config, "cs3client", log)
24-
# client.auth.set_token("<your_token_here>")
25-
# OR
2623
client.auth.set_client_secret("<your_client_secret_here>")
2724

2825
res = None
2926

3027
markdown_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/test.md")
3128

32-
res = client.checkpoint.list_file_versions(markdown_resource)
29+
res = client.checkpoint.list_file_versions(client.auth.get_token(), markdown_resource)
3330

3431
if res is not None:
3532
for ver in res:
3633
print(ver)
3734

38-
res = client.checkpoint.restore_file_version(markdown_resource, "1722936250.0569fa2f")
35+
res = client.checkpoint.restore_file_version(client.auth.get_token(), markdown_resource, "1722936250.0569fa2f")
3936
if res is not None:
4037
for ver in res:
4138
print(ver)

0 commit comments

Comments
 (0)