Skip to content

Commit

Permalink
Adapted unit tests and examples to use the package
Browse files Browse the repository at this point in the history
  • Loading branch information
Rasmus Oscar Welander committed Aug 29, 2024
1 parent 8f91045 commit 8cd4aae
Show file tree
Hide file tree
Showing 14 changed files with 242 additions and 218 deletions.
16 changes: 5 additions & 11 deletions examples/app_api_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,29 @@
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
Emails: [email protected], [email protected], [email protected]
Last updated: 19/08/2024
Last updated: 28/08/2024
"""

import logging
import configparser
from cs3client import CS3Client
from cs3resource import Resource
from cs3client.cs3client import CS3Client
from cs3client.cs3resource import Resource

config = configparser.ConfigParser()
with open("default.conf") as fdef:
config.read_file(fdef)
# log
log = logging.getLogger(__name__)


client = CS3Client(config, "cs3client", log)
# client.auth.set_token("<your_token_here>")
# OR
client.auth.set_client_secret("<your_client_secret_here>")

print(client.auth.get_token())

# list_app_providers
res = client.app.list_app_providers()
res = client.app.list_app_providers(client.auth.get_token())
if res is not None:
print(res)

# open_in_app
resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/collabora.odt")
res = client.app.open_in_app(resource)
res = client.app.open_in_app(client.auth.get_token(), resource)
if res is not None:
print(res)
33 changes: 33 additions & 0 deletions examples/auth_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
auth_example.py
Example script to demonstrate the usage of the app API in the CS3Client class.
note that these are examples, and is not meant to be run as a script.
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
Emails: [email protected], [email protected], [email protected]
Last updated: 28/08/2024
"""

import logging
import configparser
from cs3client.cs3client import CS3Client

config = configparser.ConfigParser()
with open("default.conf") as fdef:
config.read_file(fdef)
log = logging.getLogger(__name__)

client = CS3Client(config, "cs3client", log)

# Set client secret
client.auth.set_client_secret("<your_client_secret_here>")
# Checks if token is expired if not return ('x-access-token', <token>)
# if expired, request a new token from reva
auth_token = client.auth.get_token()

# OR if you already have a reva token
# Checks if token is expired if not return (x-access-token', <token>)
# if expired, throws an AuthenticationException (so you can refresh your reva token)
token = "<your_reva_token>"
auth_token = client.auth.check_token(token)
13 changes: 5 additions & 8 deletions examples/checkpoints_api_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,33 @@
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
Emails: [email protected], [email protected], [email protected]
Last updated: 19/08/2024
Last updated: 28/08/2024
"""

import logging
import configparser
from cs3client import CS3Client
from cs3resource import Resource
from cs3client.cs3client import CS3Client
from cs3client.cs3resource import Resource

config = configparser.ConfigParser()
with open("default.conf") as fdef:
config.read_file(fdef)
# log
log = logging.getLogger(__name__)

client = CS3Client(config, "cs3client", log)
# client.auth.set_token("<your_token_here>")
# OR
client.auth.set_client_secret("<your_client_secret_here>")

res = None

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

res = client.checkpoint.list_file_versions(markdown_resource)
res = client.checkpoint.list_file_versions(client.auth.get_token(), markdown_resource)

if res is not None:
for ver in res:
print(ver)

res = client.checkpoint.restore_file_version(markdown_resource, "1722936250.0569fa2f")
res = client.checkpoint.restore_file_version(client.auth.get_token(), markdown_resource, "1722936250.0569fa2f")
if res is not None:
for ver in res:
print(ver)
40 changes: 17 additions & 23 deletions examples/file_api_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,97 +13,91 @@
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
Emails: [email protected], [email protected], [email protected]
Last updated: 01/08/2024
Last updated: 28/08/2024
"""

import logging
import configparser
from cs3client import CS3Client
from cs3resource import Resource
from cs3client.cs3client import CS3Client
from cs3client.cs3resource import Resource

config = configparser.ConfigParser()
with open("default.conf") as fdef:
config.read_file(fdef)
# log
log = logging.getLogger(__name__)

client = CS3Client(config, "cs3client", log)
client.auth.set_token("<your_token_here>")
# OR
# client.auth.set_client_secret("<your_client_secret_here>")

# Authentication
print(client.auth.get_token())
client.auth.set_client_secret("<your_client_secret_here>")

res = None

# mkdir
for i in range(1, 4):
directory_resource = Resource.from_file_ref_and_endpoint(f"/eos/user/r/rwelande/test_directory{i}")
res = client.file.make_dir(directory_resource)
res = client.file.make_dir(client.auth.get_token(), directory_resource)
if res is not None:
print(res)

# touchfile
touch_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/touch_file.txt")
text_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/text_file.txt")
res = client.file.touch_file(touch_resource)
res = client.file.touch_file(text_resource)
res = client.file.touch_file(client.auth.get_token(), touch_resource)

Check warning

Code scanning / CodeQL

Variable defined multiple times Warning

This assignment to 'res' is unnecessary as it is
redefined
before this value is used.
res = client.file.touch_file(client.auth.get_token(), text_resource)

if res is not None:
print(res)

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

if res is not None:
print(res)

# rmxattr
res = client.file.remove_xattr(resource, "iop.wopi.lastwritetime")
res = client.file.remove_xattr(client.auth.get_token(), resource, "iop.wopi.lastwritetime")

if res is not None:
print(res)

# stat
res = client.file.stat(text_resource)
res = client.file.stat(client.auth.get_token(), text_resource)

if res is not None:
print(res)

# removefile
res = client.file.remove_file(touch_resource)
res = client.file.remove_file(client.auth.get_token(), touch_resource)

if res is not None:
print(res)

res = client.file.touch_file(touch_resource)
res = client.file.touch_file(client.auth.get_token(), touch_resource)

Check warning

Code scanning / CodeQL

Variable defined multiple times Warning

This assignment to 'res' is unnecessary as it is
redefined
before this value is used.

# rename
rename_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/rename_file.txt")
res = client.file.rename_file(resource, rename_resource)
res = client.file.rename_file(client.auth.get_token(), resource, rename_resource)

if res is not None:
print(res)

# writefile
content = b"Hello World"
size = len(content)
res = client.file.write_file(rename_resource, content, size)
res = client.file.write_file(client.auth.get_token(), rename_resource, content, size)

if res is not None:
print(res)

# rmdir (same as deletefile)
res = client.file.remove_file(directory_resource)
res = client.file.remove_file(client.auth.get_token(), directory_resource)

if res is not None:
print(res)

# listdir
list_directory_resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande")
res = client.file.list_dir(list_directory_resource)
res = client.file.list_dir(client.auth.get_token(), list_directory_resource)

first_item = next(res, None)
if first_item is not None:
Expand All @@ -114,7 +108,7 @@
print("empty response")

# readfile
file_res = client.file.read_file(rename_resource)
file_res = client.file.read_file(client.auth.get_token(), rename_resource)
content = b""
try:
for chunk in file_res:
Expand Down
58 changes: 31 additions & 27 deletions examples/shares_api_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,41 @@
Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
Emails: [email protected], [email protected], [email protected]
Last updated: 19/08/2024
Last updated: 28/08/2024
"""

import logging
import configparser
from cs3client import CS3Client
from cs3resource import Resource
from cs3client.cs3client import CS3Client
from cs3client.cs3resource import Resource

config = configparser.ConfigParser()
with open("default.conf") as fdef:
config.read_file(fdef)
# log
log = logging.getLogger(__name__)

client = CS3Client(config, "cs3client", log)
# client.auth.set_token("<your_token_here>")
# OR
client.auth.set_client_secret("<your_client_secret_here>")

# Authentication
print(client.auth.get_token())

res = None

# Create share #
resource = Resource.from_file_ref_and_endpoint("/eos/user/r/rwelande/text.txt")
resource_info = client.file.stat(resource)
resource_info = client.file.stat(client.auth.get_token(), resource)

# VIEWER
user = client.user.get_user_by_claim("mail", "[email protected]")
res = client.share.create_share(resource_info, user.id.opaque_id, user.id.idp, "VIEWER", "USER")
user = client.user.get_user_by_claim(client.auth.get_token(), "mail", "[email protected]")
res = client.share.create_share(
client.auth.get_token(), resource_info, user.id.opaque_id, user.id.idp, "VIEWER", "USER"
)
if res is not None:
print(res)

# EDITOR
user = client.user.get_user_by_claim("username", "lopresti")
res = client.share.create_share(resource_info, user.id.opaque_id, user.id.idp, "EDITOR", "USER")
user = client.user.get_user_by_claim(client.auth.get_token(), "username", "lopresti")
res = client.share.create_share(
client.auth.get_token(), resource_info, user.id.opaque_id, user.id.idp, "EDITOR", "USER"
)
if res is not None:
print(res)

Expand All @@ -54,58 +52,62 @@
filter_list.append(filter)
filter = client.share.create_share_filter(share_state="SHARE_STATE_PENDING", filter_type="TYPE_STATE")
filter_list.append(filter)
res, _ = client.share.list_existing_shares()
res, _ = client.share.list_existing_shares(client.auth.get_token(), filter_list=filter_list)
if res is not None:
for share_info in res:
print(share_info.share)

# Get share #
share_id = "58"
res = client.share.get_share(opaque_id=share_id)
res = client.share.get_share(client.auth.get_token(), opaque_id=share_id)
if res is not None:
print(res)

# update share #
share_id = "58"
res = client.share.update_share(opaque_id=share_id, role="VIEWER")
res = client.share.update_share(client.auth.get_token(), opaque_id=share_id, role="VIEWER")
if res is not None:
print(res)

# remove share #
share_id = "58"
res = client.share.remove_share(opaque_id=share_id)
res = client.share.remove_share(client.auth.get_token(), opaque_id=share_id)
if res is not None:
print(res)

# List existing received shares #

# Create a filter list
filter_list = []
filter = client.share.create_share_filter(share_state="SHARE_STATE_ACCEPTED", filter_type="TYPE_STATE")
filter = client.share.create_share_filter(
client.auth.get_token(), share_state="SHARE_STATE_ACCEPTED", filter_type="TYPE_STATE"
)

# Append the filter to the filter list
filter_list.append(filter)

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

# get received share #
share_id = "43"

received_share = client.share.get_received_share(opaque_id=share_id)
received_share = client.share.get_received_share(client.auth.get_token(), opaque_id=share_id)
if received_share is not None:
print(received_share)

# update recieved share #
res = client.share.update_received_share(received_share=received_share, state="SHARE_STATE_ACCEPTED")
res = client.share.update_received_share(
client.auth.get_token(), received_share=received_share, state="SHARE_STATE_ACCEPTED"
)
if res is not None:
print(res)

# create public share #
res = client.share.create_public_share(resource_info, role="VIEWER")
res = client.share.create_public_share(client.auth.get_token(), resource_info, role="VIEWER")
if res is not None:
print(res)

Expand All @@ -116,7 +118,7 @@
filter = client.share.create_public_share_filter(resource_id=resource_info.id, filter_type="TYPE_RESOURCE_ID")
filter_list.append(filter)
print(filter_list)
res, _ = client.share.list_existing_public_shares(filter_list=filter_list)
res, _ = client.share.list_existing_public_shares(client.auth.get_token(), filter_list=filter_list)
if res is not None:
for share_info in res:
print(share_info.share)
Expand All @@ -125,16 +127,18 @@
share_id = "63"
# OR
token = "7FbP1EBXJQTqK0d"
res = client.share.get_public_share(opaque_id=share_id, sign=True)
res = client.share.get_public_share(client.auth.get_token(), opaque_id=share_id, sign=True)
if res is not None:
print(res)

# update public share #
res = client.share.update_public_share(type="TYPE_PASSWORD", token=token, role="VIEWER", password="hello")
res = client.share.update_public_share(
client.auth.get_token(), type="TYPE_PASSWORD", token=token, role="VIEWER", password="hello"
)
if res is not None:
print(res)

# remove public share #
res = client.share.remove_public_share(token=token)
res = client.share.remove_public_share(client.auth.get_token(), token=token)
if res is not None:
print(res)
Loading

0 comments on commit 8cd4aae

Please sign in to comment.