From 7d49946c666456e892a5e598187614072a197b61 Mon Sep 17 00:00:00 2001 From: flowzen1337 Date: Mon, 24 Jul 2023 02:05:11 +0200 Subject: [PATCH 1/4] Added api request showcase for demo purposes --- api_demo/README.md | 20 +++++++++++ api_demo/main.py | 72 +++++++++++++++++++++++++++++++++++++++ api_demo/requirements.txt | 2 ++ api_demo/setup.sh | 38 +++++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 api_demo/README.md create mode 100644 api_demo/main.py create mode 100644 api_demo/requirements.txt create mode 100644 api_demo/setup.sh diff --git a/api_demo/README.md b/api_demo/README.md new file mode 100644 index 00000000..213dfcdc --- /dev/null +++ b/api_demo/README.md @@ -0,0 +1,20 @@ + +# Setup + + + +You've got two options to install it + +1.) Automaticaly: +- bash setup.sh +2.) Manually (just execute each step after each other): +- wget -O "openapi.json" "https://raw.githubusercontent.com/nextcloud/server/master/apps/files_sharing/openapi.json" +- sed -i "s#application/json#text/plain#g" "openapi.json" +- pip install -r requirements.txt +- openapi-python-client generate --path "openapi.json" +- cp -v "main.py" "files-sharing-client/main.py" + +Execute the requests (It creates a share on demo_image.jpg, updates the note of the share and gets the information for the created share): +- cd files-sharing-client/ +- Adjust the variables host, username, password and demo_file in main.py +- python3 main.py diff --git a/api_demo/main.py b/api_demo/main.py new file mode 100644 index 00000000..1517a783 --- /dev/null +++ b/api_demo/main.py @@ -0,0 +1,72 @@ +import sys +import json +import base64 +import xmltodict + +from json import JSONDecodeError +from files_sharing_client.api.shareapi import shareapi_create_share, shareapi_get_share, shareapi_update_share, shareapi_delete_share +from files_sharing_client.client import Client + +# Configuration +host = "HOST_URL" +username = "USERNAME" +password = "PASSWORD" +demo_file = "demo_image.jpg" + +user_pass = f"{username}:{password}" + +# Base64 encode the string +user_pass_b64 = base64.b64encode(user_pass.encode()).decode() + +# Create the header dictionary +headers = {"Authorization": f"Basic {user_pass_b64}"} + +# Erstellen Sie eine Instanz des AuthenticatedClient +auth_client = Client(base_url=host, headers=headers, verify_ssl=False) + +# Create public share on a file +print("\033[1;36mCreate public share\033[0m") + +try: + response = shareapi_create_share.sync_detailed( + client=auth_client, path="/"+str(demo_file), share_type=3, public_upload=False, label="Demo Share" + ) + print("\033[1;33mContent Type:\033[0m", response.headers.get("Content-Type")) + print("\033[1;33mStatus Code:\033[0m", response.status_code) + print("\033[1;33mHeaders:\033[0m", response.headers) + print("\033[1;33mContent:\033[0m", response.content.decode()) + data_dict = xmltodict.parse(response.content.decode()) +except JSONDecodeError: + pass + +try: + new_id = int(data_dict['ocs']['data']['id']) + print("\033[1;32m[ Created ]\033[0m", "Created new share with ID", int(new_id)) +except Exception as e: + print("\033[1;31m[ ERROR ]\[033[0m", str(e)) + sys.exit(1) +print() + +# Update the public share +print("\033[1;36mUpdate the public share\033[0m", new_id) +try: + response = shareapi_update_share.sync_detailed(client=auth_client, note="Updated the share", id=new_id) + print("\033[1;33mContent Type:\033[0m", response.headers.get("Content-Type")) + print("\033[1;33mStatus Code:\033[0m", response.status_code) + print("\033[1;33mHeaders:\033[0m", response.headers) + print("\033[1;33mContent:\033[0m", response.content.decode()) +except JSONDecodeError: + pass +print() + +# Get the share information +print("\033[1;36mGet the share information\033[0m", new_id) +try: + response = shareapi_get_share.sync_detailed(client=auth_client, id=new_id) + print("\033[1;33mContent Type:\033[0m", response.headers.get("Content-Type")) + print("\033[1;33mStatus Code:\033[0m", response.status_code) + print("\033[1;33mHeaders:\033[0m", response.headers) + print("\033[1;33mContent:\033[0m", response.content.decode()) +except JSONDecodeError: + pass +print() diff --git a/api_demo/requirements.txt b/api_demo/requirements.txt new file mode 100644 index 00000000..5131b130 --- /dev/null +++ b/api_demo/requirements.txt @@ -0,0 +1,2 @@ +xmltodict +openapi-python-client diff --git a/api_demo/setup.sh b/api_demo/setup.sh new file mode 100644 index 00000000..70180a3e --- /dev/null +++ b/api_demo/setup.sh @@ -0,0 +1,38 @@ +_URL="https://raw.githubusercontent.com/nextcloud/server/master/apps/files_sharing/openapi.json" +_FILE="openapi.json" +_RET=0 + +echo -e "\e[1;32m1. Downloading\e[0m" +wget -q "$_URL" -O "$_FILE" +_RET=$(($_RET+$?)) + +echo -e "\e[1;32m2. Adjusting OpenAPI\e[0m" +sed -i "s#application/json#text/plain#g" "$_FILE" +_RET=$(($_RET+$?)) + +echo -e "\e[1;32m3. Installing requirements\e[0m" +pip install -r requirements.txt + +echo -e "\e[1;32m4. Generating\e[0m" +if [ -d "files-sharing-client" ]; + then + openapi-python-client update --path $_FILE + else + openapi-python-client generate --path $_FILE +fi +_RET=$(($_RET+$?)) + +echo -e "\e[1;32m5. Preparing\e[0m" +cp -v "main.py" "files-sharing-client/main.py" +_RET=$(($_RET+$?)) + +if [ "$_RET" -eq 0 ]; + then + echo -e "\e[0;44m,--------\e[0m" + echo -e "\e[0;44m\e[1;37mSetup\e[0m\t\e[1;32mDone\e[0m" + echo -e "\e[0;44m\`--------\e[0m" + else + echo -e "\e[0;41m,--------\e[0m" + echo -e "\e[0;41m\e[1;37mSetup\e[0m\t\e[1;31mError\e[0m" + echo -e "\e[0;41m\`--------\e[0m" +fi From 9ff164b559c031fc4627c5aea438b507158e18fc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Jul 2023 00:12:52 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- api_demo/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api_demo/README.md b/api_demo/README.md index 213dfcdc..e0ce0c40 100644 --- a/api_demo/README.md +++ b/api_demo/README.md @@ -16,5 +16,5 @@ You've got two options to install it Execute the requests (It creates a share on demo_image.jpg, updates the note of the share and gets the information for the created share): - cd files-sharing-client/ -- Adjust the variables host, username, password and demo_file in main.py +- Adjust the variables host, username, password and demo_file in main.py - python3 main.py From b28b5dd18559e5add8ae42e1e7c3987af1c57fbd Mon Sep 17 00:00:00 2001 From: flowzen1337 Date: Mon, 24 Jul 2023 02:14:59 +0200 Subject: [PATCH 3/4] fix of unused import --- api_demo/main.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/api_demo/main.py b/api_demo/main.py index 1517a783..347f2aad 100644 --- a/api_demo/main.py +++ b/api_demo/main.py @@ -4,8 +4,7 @@ import xmltodict from json import JSONDecodeError -from files_sharing_client.api.shareapi import shareapi_create_share, shareapi_get_share, shareapi_update_share, shareapi_delete_share -from files_sharing_client.client import Client +from files_sharing_client.api.shareapi import shareapi_create_share, shareapi_get_share, shareapi_update_share # Configuration host = "HOST_URL" From 3af6e0ccd420a5a6f1564d722566825a12383cf3 Mon Sep 17 00:00:00 2001 From: flowzen1337 Date: Mon, 24 Jul 2023 02:37:14 +0200 Subject: [PATCH 4/4] small bugfix of missing import --- api_demo/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/api_demo/main.py b/api_demo/main.py index 347f2aad..3881b3da 100644 --- a/api_demo/main.py +++ b/api_demo/main.py @@ -5,6 +5,7 @@ from json import JSONDecodeError from files_sharing_client.api.shareapi import shareapi_create_share, shareapi_get_share, shareapi_update_share +from files_sharing_client.client import Client # Configuration host = "HOST_URL"