Skip to content

Commit 6c955c3

Browse files
author
Rasmus Oscar Welander
committed
Changed tokens to be handled externally, CI fixes, and fixed package structure
1 parent afe7044 commit 6c955c3

30 files changed

+463
-403
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: 45 additions & 36 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 [email protected]: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
119-
from cs3client import CS3Client
120-
from cs3resource import Resource
118+
from cs3client.cs3client import CS3Client
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
@@ -282,6 +288,9 @@ make html
282288
## Unit tests
283289

284290
```bash
291+
# install library
292+
pip install .
293+
# run unit tests
285294
pytest --cov-report term --cov=serc tests/
286295
```
287296

File renamed without changes.

src/app.py renamed to cs3client/app.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
44
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
55
6-
Last updated: 19/08/2024
6+
Last updated: 28/08/2024
77
"""
88

99
import logging
10-
from auth import Auth
11-
from cs3resource import Resource
1210
import cs3.app.registry.v1beta1.registry_api_pb2 as cs3arreg
1311
import cs3.app.registry.v1beta1.resources_pb2 as cs3arres
1412
import cs3.gateway.v1beta1.gateway_api_pb2 as cs3gw
1513
import cs3.app.provider.v1beta1.resources_pb2 as cs3apr
1614
from cs3.gateway.v1beta1.gateway_api_pb2_grpc import GatewayAPIStub
17-
from statuscodehandler import StatusCodeHandler
18-
from config import Config
15+
16+
from cs3client.cs3resource import Resource
17+
from cs3client.statuscodehandler import StatusCodeHandler
18+
from cs3client.config import Config
1919

2020

2121
class App:
@@ -28,7 +28,6 @@ def __init__(
2828
config: Config,
2929
log: logging.Logger,
3030
gateway: GatewayAPIStub,
31-
auth: Auth,
3231
status_code_handler: StatusCodeHandler,
3332
) -> None:
3433
"""
@@ -37,20 +36,21 @@ def __init__(
3736
:param config: Config object containing the configuration parameters.
3837
:param log: Logger instance for logging.
3938
:param gateway: GatewayAPIStub instance for interacting with CS3 Gateway.
40-
:param auth: An instance of the auth class.
4139
:param status_code_handler: An instance of the StatusCodeHandler class.
4240
"""
4341
self._status_code_handler: StatusCodeHandler = status_code_handler
4442
self._gateway: GatewayAPIStub = gateway
4543
self._log: logging.Logger = log
4644
self._config: Config = config
47-
self._auth: Auth = auth
4845

49-
def open_in_app(self, resource: Resource, view_mode: str = None, app: str = None) -> cs3apr.OpenInAppURL:
46+
def open_in_app(
47+
self, auth_token: tuple, resource: Resource, view_mode: str = None, app: str = None
48+
) -> cs3apr.OpenInAppURL:
5049
"""
5150
Open a file in an app, given the resource, view mode (VIEW_MODE_VIEW_ONLY, VIEW_MODE_READ_ONLY,
5251
VIEW_MODE_READ_WRITE, VIEW_MODE_PREVIEW), and app name.
5352
53+
:param auth_token: tuple in the form ('x-access-token', <token> (see auth.get_token/auth.check_token)
5454
:param resource: Resource object containing the resource information.
5555
:param view_mode: View mode of the app.
5656
:param app: App name.
@@ -63,21 +63,22 @@ def open_in_app(self, resource: Resource, view_mode: str = None, app: str = None
6363
if view_mode:
6464
view_mode_type = cs3gw.OpenInAppRequest.ViewMode.Value(view_mode)
6565
req = cs3gw.OpenInAppRequest(ref=resource.ref, view_mode=view_mode_type, app=app)
66-
res = self._gateway.OpenInApp(request=req, metadata=[self._auth.get_token()])
66+
res = self._gateway.OpenInApp(request=req, metadata=[auth_token])
6767
self._status_code_handler.handle_errors(res.status, "open in app", f"{resource.get_file_ref_str()}")
6868
self._log.debug(f'msg="Invoked OpenInApp" {resource.get_file_ref_str()} trace="{res.status.trace}"')
6969
return res.OpenInAppURL
7070

71-
def list_app_providers(self) -> list[cs3arres.ProviderInfo]:
71+
def list_app_providers(self, auth_token: dict) -> list[cs3arres.ProviderInfo]:
7272
"""
7373
list_app_providers lists all the app providers.
7474
75+
:param auth_token: tuple in the form ('x-access-token', <token> (see auth.get_token/auth.check_token)
7576
:return: List of app providers.
7677
:raises: AuthenticationException (Operation not permitted)
7778
:raises: UnknownException (Unknown error)
7879
"""
7980
req = cs3arreg.ListAppProvidersRequest()
80-
res = self._gateway.ListAppProviders(request=req, metadata=[self._auth.get_token()])
81+
res = self._gateway.ListAppProviders(request=req, metadata=[auth_token])
8182
self._status_code_handler.handle_errors(res.status, "list app providers")
8283
self._log.debug(f'msg="Invoked ListAppProviders" res_count="{len(res.providers)}" trace="{res.status.trace}"')
8384
return res.providers

0 commit comments

Comments
 (0)