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

chore: Improve cli client #55

Merged
merged 6 commits into from
Oct 2, 2023
Merged
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ node_modules

# python envs
pyenv
venv

# python cache
**/__pycache__

# JDTLS
bin
Expand Down
48 changes: 48 additions & 0 deletions cli-client/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3

# Simple SOAP client to test the service
# Install Zeep SOAP client https://docs.python-zeep.org/
# $ pip install zeep

import handlers

available_features = [
{"name": "👥 Register default user", "handler": handlers.registerDefaultUserHandler},
{"name": "👥 Register", "handler": handlers.registerHandler},
{"name": "🔑 Login", "handler": handlers.loginHandler},
{"name": "📄 Upload file", "handler": handlers.uploadHandler},
{"name": "📄 Download file", "handler": handlers.downloadHandler},
{"name": "🚪 Finish program", "handler": handlers.exitHandler},
]


def show_menu():
# Print menu
for i in range(len(available_features)):
feature = available_features[i]
print(f"{i + 1}. {feature['name']}")

# Get menu selection and call handler
selection = get_menu_selection()
available_features[selection - 1]["handler"]()

# Show menu again
print("\n")
show_menu()


def get_menu_selection():
# Get menu selection
while True:
try:
selection = int(input("Select: "))
if selection > 0 and selection <= len(available_features):
return selection
else:
print("Invalid selection")
except ValueError:
print("Invalid selection")


if __name__ == "__main__":
show_menu()
80 changes: 80 additions & 0 deletions cli-client/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import random
import time
import os
from zeep import Client

# Soap client
client = Client("http://localhost:8080/service?wsdl")


# Handlers
def registerDefaultUserHandler():
res = client.service.account_register(
{"username": "woynert", "password": "password"}
)
print(res)


def registerHandler():
username = input("Username: ")
password = input("Password: ")
res = client.service.account_register({"username": username, "password": password})
print(res)


def loginHandler():
username = input("Username: ")
password = input("Password: ")
res = client.service.auth_login({"username": username, "password": password})
print(res)


def uploadHandler():
filePath = input("File absolute path (Eg. /home/user/file.jpeg): ")

with open(filePath, "rb") as in_file:
fileName = os.path.basename(filePath)
fileExtension = os.path.splitext(fileName)[1]

# Login with the default user
session = client.service.auth_login(
{"username": "woynert", "password": "password"}
)

res = client.service.file_upload(
{
"fileName": f"{fileName}",
"fileContent": in_file.read(),
"location": "None",
"token": session.auth.token,
}
)
print(res)


def downloadHandler():
fileUUID = input("File UUID: ")

# Login with the default user
session = client.service.auth_login({"username": "woynert", "password": "password"})

# Get the file
res = client.service.file_download(
{"fileUUID": fileUUID, "token": session.auth.token}
)

# write to file
destFile = input(
"Destination file absolute path (Eg. /home/user/downloaded.jpeg): "
)
if res.fileContent:
with open(destFile, "wb") as f:
f.write(res.fileContent)
print("INFO: File successfully written in disk")

res.fileContent = f"{len(res.fileContent)} bytes"
print(res)


def exitHandler():
os._exit(0)
52 changes: 52 additions & 0 deletions cli-client/simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3

# Simple SOAP client to test the service
# Install Zeep SOAP client https://docs.python-zeep.org/
# $ pip install zeep

import random
import time
from zeep import Client

# session

client = Client("http://localhost:8080/service?wsdl")

resR = client.service.account_register({"username": "woynert", "password": "password"})
print(resR)

session = client.service.auth_login({"username": "woynert", "password": "password"})
print(session)

# upload
# NOTE: Put a file here

with open("/home/woynert/Pictures/karolsticker.png", "rb") as in_file:
resU = client.service.file_upload(
{
"fileName": f"karolsticker{random.random()}.png",
"fileContent": in_file.read(),
"location": "None",
"token": session.auth.token,
}
)
print(resU)

time.sleep(1)

# download

res = client.service.file_download(
{"fileUUID": resU.fileUUID, "token": session.auth.token}
)

# write to file
# NOTE: Put a destination here

if res.fileContent:
with open(f"/tmp/dudu/{res.fileName}", "wb") as f:
f.write(res.fileContent)
print("INFO: File successfully written in disk")

res.fileContent = f"{len(res.fileContent)} bytes"
print(res)
58 changes: 0 additions & 58 deletions soap-python-client.py

This file was deleted.