Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added api request showcase for demo purposes #41

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions api_demo/README.md
Original file line number Diff line number Diff line change
@@ -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
72 changes: 72 additions & 0 deletions api_demo/main.py
Original file line number Diff line number Diff line change
@@ -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
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()
2 changes: 2 additions & 0 deletions api_demo/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
xmltodict
openapi-python-client
38 changes: 38 additions & 0 deletions api_demo/setup.sh
Original file line number Diff line number Diff line change
@@ -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