Skip to content

Commit 8cd4aae

Browse files
author
Rasmus Oscar Welander
committed
Adapted unit tests and examples to use the package
1 parent 8f91045 commit 8cd4aae

14 files changed

+242
-218
lines changed

examples/app_api_example.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,29 @@
66
77
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
88
9-
Last updated: 19/08/2024
9+
Last updated: 28/08/2024
1010
"""
1111

1212
import logging
1313
import configparser
14-
from cs3client import CS3Client
15-
from cs3resource import Resource
14+
from cs3client.cs3client import CS3Client
15+
from cs3client.cs3resource import Resource
1616

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+
9+
Last updated: 28/08/2024
10+
"""
11+
12+
import logging
13+
import configparser
14+
from cs3client.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: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,33 @@
66
77
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
88
9-
Last updated: 19/08/2024
9+
Last updated: 28/08/2024
1010
"""
1111

1212
import logging
1313
import configparser
14-
from cs3client import CS3Client
15-
from cs3resource import Resource
14+
from cs3client.cs3client import CS3Client
15+
from cs3client.cs3resource import Resource
1616

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)

examples/file_api_example.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,97 +13,91 @@
1313
1414
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
1515
16-
Last updated: 01/08/2024
16+
Last updated: 28/08/2024
1717
"""
1818

1919
import logging
2020
import configparser
21-
from cs3client import CS3Client
22-
from cs3resource import Resource
21+
from cs3client.cs3client import CS3Client
22+
from cs3client.cs3resource import Resource
2323

2424
config = configparser.ConfigParser()
2525
with open("default.conf") as fdef:
2626
config.read_file(fdef)
27-
# log
2827
log = logging.getLogger(__name__)
2928

3029
client = CS3Client(config, "cs3client", log)
31-
client.auth.set_token("<your_token_here>")
32-
# OR
33-
# client.auth.set_client_secret("<your_client_secret_here>")
34-
35-
# Authentication
36-
print(client.auth.get_token())
30+
client.auth.set_client_secret("<your_client_secret_here>")
3731

3832
res = None
3933

4034
# mkdir
4135
for i in range(1, 4):
4236
directory_resource = Resource.from_file_ref_and_endpoint(f"/eos/user/r/rwelande/test_directory{i}")
43-
res = client.file.make_dir(directory_resource)
37+
res = client.file.make_dir(client.auth.get_token(), directory_resource)
4438
if res is not None:
4539
print(res)
4640

4741
# touchfile
4842
touch_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/touch_file.txt")
4943
text_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/text_file.txt")
50-
res = client.file.touch_file(touch_resource)
51-
res = client.file.touch_file(text_resource)
44+
res = client.file.touch_file(client.auth.get_token(), touch_resource)
45+
res = client.file.touch_file(client.auth.get_token(), text_resource)
5246

5347
if res is not None:
5448
print(res)
5549

5650
# setxattr
5751
resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/text_file.txt")
58-
res = client.file.set_xattr(resource, "iop.wopi.lastwritetime", str(1720696124))
52+
res = client.file.set_xattr(client.auth.get_token(), resource, "iop.wopi.lastwritetime", str(1720696124))
5953

6054
if res is not None:
6155
print(res)
6256

6357
# rmxattr
64-
res = client.file.remove_xattr(resource, "iop.wopi.lastwritetime")
58+
res = client.file.remove_xattr(client.auth.get_token(), resource, "iop.wopi.lastwritetime")
6559

6660
if res is not None:
6761
print(res)
6862

6963
# stat
70-
res = client.file.stat(text_resource)
64+
res = client.file.stat(client.auth.get_token(), text_resource)
7165

7266
if res is not None:
7367
print(res)
7468

7569
# removefile
76-
res = client.file.remove_file(touch_resource)
70+
res = client.file.remove_file(client.auth.get_token(), touch_resource)
7771

7872
if res is not None:
7973
print(res)
8074

81-
res = client.file.touch_file(touch_resource)
75+
res = client.file.touch_file(client.auth.get_token(), touch_resource)
8276

8377
# rename
8478
rename_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/rename_file.txt")
85-
res = client.file.rename_file(resource, rename_resource)
79+
res = client.file.rename_file(client.auth.get_token(), resource, rename_resource)
8680

8781
if res is not None:
8882
print(res)
8983

9084
# writefile
9185
content = b"Hello World"
9286
size = len(content)
93-
res = client.file.write_file(rename_resource, content, size)
87+
res = client.file.write_file(client.auth.get_token(), rename_resource, content, size)
9488

9589
if res is not None:
9690
print(res)
9791

9892
# rmdir (same as deletefile)
99-
res = client.file.remove_file(directory_resource)
93+
res = client.file.remove_file(client.auth.get_token(), directory_resource)
10094

10195
if res is not None:
10296
print(res)
10397

10498
# listdir
10599
list_directory_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande")
106-
res = client.file.list_dir(list_directory_resource)
100+
res = client.file.list_dir(client.auth.get_token(), list_directory_resource)
107101

108102
first_item = next(res, None)
109103
if first_item is not None:
@@ -114,7 +108,7 @@
114108
print("empty response")
115109

116110
# readfile
117-
file_res = client.file.read_file(rename_resource)
111+
file_res = client.file.read_file(client.auth.get_token(), rename_resource)
118112
content = b""
119113
try:
120114
for chunk in file_res:

examples/shares_api_example.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,41 @@
66
77
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
88
9-
Last updated: 19/08/2024
9+
Last updated: 28/08/2024
1010
"""
1111

1212
import logging
1313
import configparser
14-
from cs3client import CS3Client
15-
from cs3resource import Resource
14+
from cs3client.cs3client import CS3Client
15+
from cs3client.cs3resource import Resource
1616

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

28-
# Authentication
29-
print(client.auth.get_token())
30-
3125
res = None
3226

3327
# Create share #
3428
resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/text.txt")
35-
resource_info = client.file.stat(resource)
29+
resource_info = client.file.stat(client.auth.get_token(), resource)
3630

3731
# VIEWER
38-
user = client.user.get_user_by_claim("mail", "[email protected]")
39-
res = client.share.create_share(resource_info, user.id.opaque_id, user.id.idp, "VIEWER", "USER")
32+
user = client.user.get_user_by_claim(client.auth.get_token(), "mail", "[email protected]")
33+
res = client.share.create_share(
34+
client.auth.get_token(), resource_info, user.id.opaque_id, user.id.idp, "VIEWER", "USER"
35+
)
4036
if res is not None:
4137
print(res)
4238

4339
# EDITOR
44-
user = client.user.get_user_by_claim("username", "lopresti")
45-
res = client.share.create_share(resource_info, user.id.opaque_id, user.id.idp, "EDITOR", "USER")
40+
user = client.user.get_user_by_claim(client.auth.get_token(), "username", "lopresti")
41+
res = client.share.create_share(
42+
client.auth.get_token(), resource_info, user.id.opaque_id, user.id.idp, "EDITOR", "USER"
43+
)
4644
if res is not None:
4745
print(res)
4846

@@ -54,58 +52,62 @@
5452
filter_list.append(filter)
5553
filter = client.share.create_share_filter(share_state="SHARE_STATE_PENDING", filter_type="TYPE_STATE")
5654
filter_list.append(filter)
57-
res, _ = client.share.list_existing_shares()
55+
res, _ = client.share.list_existing_shares(client.auth.get_token(), filter_list=filter_list)
5856
if res is not None:
5957
for share_info in res:
6058
print(share_info.share)
6159

6260
# Get share #
6361
share_id = "58"
64-
res = client.share.get_share(opaque_id=share_id)
62+
res = client.share.get_share(client.auth.get_token(), opaque_id=share_id)
6563
if res is not None:
6664
print(res)
6765

6866
# update share #
6967
share_id = "58"
70-
res = client.share.update_share(opaque_id=share_id, role="VIEWER")
68+
res = client.share.update_share(client.auth.get_token(), opaque_id=share_id, role="VIEWER")
7169
if res is not None:
7270
print(res)
7371

7472
# remove share #
7573
share_id = "58"
76-
res = client.share.remove_share(opaque_id=share_id)
74+
res = client.share.remove_share(client.auth.get_token(), opaque_id=share_id)
7775
if res is not None:
7876
print(res)
7977

8078
# List existing received shares #
8179

8280
# Create a filter list
8381
filter_list = []
84-
filter = client.share.create_share_filter(share_state="SHARE_STATE_ACCEPTED", filter_type="TYPE_STATE")
82+
filter = client.share.create_share_filter(
83+
client.auth.get_token(), share_state="SHARE_STATE_ACCEPTED", filter_type="TYPE_STATE"
84+
)
8585

8686
# Append the filter to the filter list
8787
filter_list.append(filter)
8888

8989
# NOTE: filters for received shares are not implemented (14/08/2024), therefore it is left out
90-
res, _ = client.share.list_received_existing_shares()
90+
res, _ = client.share.list_received_existing_shares(client.auth.get_token())
9191
if res is not None:
9292
for share_info in res:
9393
print(share_info.received_share)
9494

9595
# get received share #
9696
share_id = "43"
9797

98-
received_share = client.share.get_received_share(opaque_id=share_id)
98+
received_share = client.share.get_received_share(client.auth.get_token(), opaque_id=share_id)
9999
if received_share is not None:
100100
print(received_share)
101101

102102
# update recieved share #
103-
res = client.share.update_received_share(received_share=received_share, state="SHARE_STATE_ACCEPTED")
103+
res = client.share.update_received_share(
104+
client.auth.get_token(), received_share=received_share, state="SHARE_STATE_ACCEPTED"
105+
)
104106
if res is not None:
105107
print(res)
106108

107109
# create public share #
108-
res = client.share.create_public_share(resource_info, role="VIEWER")
110+
res = client.share.create_public_share(client.auth.get_token(), resource_info, role="VIEWER")
109111
if res is not None:
110112
print(res)
111113

@@ -116,7 +118,7 @@
116118
filter = client.share.create_public_share_filter(resource_id=resource_info.id, filter_type="TYPE_RESOURCE_ID")
117119
filter_list.append(filter)
118120
print(filter_list)
119-
res, _ = client.share.list_existing_public_shares(filter_list=filter_list)
121+
res, _ = client.share.list_existing_public_shares(client.auth.get_token(), filter_list=filter_list)
120122
if res is not None:
121123
for share_info in res:
122124
print(share_info.share)
@@ -125,16 +127,18 @@
125127
share_id = "63"
126128
# OR
127129
token = "7FbP1EBXJQTqK0d"
128-
res = client.share.get_public_share(opaque_id=share_id, sign=True)
130+
res = client.share.get_public_share(client.auth.get_token(), opaque_id=share_id, sign=True)
129131
if res is not None:
130132
print(res)
131133

132134
# update public share #
133-
res = client.share.update_public_share(type="TYPE_PASSWORD", token=token, role="VIEWER", password="hello")
135+
res = client.share.update_public_share(
136+
client.auth.get_token(), type="TYPE_PASSWORD", token=token, role="VIEWER", password="hello"
137+
)
134138
if res is not None:
135139
print(res)
136140

137141
# remove public share #
138-
res = client.share.remove_public_share(token=token)
142+
res = client.share.remove_public_share(client.auth.get_token(), token=token)
139143
if res is not None:
140144
print(res)

0 commit comments

Comments
 (0)