Skip to content

Commit

Permalink
Added project resource
Browse files Browse the repository at this point in the history
  • Loading branch information
KatHellg committed Jan 8, 2025
1 parent 8e55110 commit 08594c0
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 167 deletions.
14 changes: 5 additions & 9 deletions fedn/cli/client_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ def list_clients(ctx, protocol: str, host: str, port: str, token: str = None, n_
if n_max:
headers["X-Limit"] = n_max

_token = get_token(token)
_token = get_token(token, False)

if _token:
headers["Authorization"] = _token


try:
response = requests.get(url, headers=headers)
print_response(response, "clients", None)
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url}")


@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)")
@click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)")
@click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)")
Expand All @@ -80,19 +80,15 @@ def get_client(ctx, protocol: str, host: str, port: str, token: str = None, id:
- result: client with given id
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="clients")
_url = get_api_url(protocol=protocol, host=host, port=port, endpoint="clients")
url = f"{_url}{id}"
headers = {}


_token = get_token(token)
_token = get_token(token, False)

if _token:
headers["Authorization"] = _token

if id:
url = f"{url}{id}"


try:
response = requests.get(url, headers=headers)
print_response(response, "client", id)
Expand Down
10 changes: 4 additions & 6 deletions fedn/cli/combiner_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def list_combiners(ctx, protocol: str, host: str, port: str, token: str = None,
if n_max:
headers["X-Limit"] = n_max

_token = get_token(token)
_token = get_token(token, False)

if _token:
headers["Authorization"] = _token
Expand All @@ -108,17 +108,15 @@ def get_combiner(ctx, protocol: str, host: str, port: str, token: str = None, id
- result: combiner with given id
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="combiners")
_url = get_api_url(protocol=protocol, host=host, port=port, endpoint="combiners")
url = f"{_url}{id}"
headers = {}

_token = get_token(token)
_token = get_token(token, False)

if _token:
headers["Authorization"] = _token

if id:
url = f"{url}{id}"

try:
response = requests.get(url, headers=headers)
print_response(response, "combiner", id)
Expand Down
38 changes: 23 additions & 15 deletions fedn/cli/login_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import yaml

from .main import main
from .shared import CONTROLLER_DEFAULTS, get_token

# Replace this with the platform's actual login endpoint
home_dir = os.path.expanduser("~")
Expand All @@ -19,10 +20,11 @@ def login_cmd(ctx):


@login_cmd.command("login")
@click.option("-p", "--protocol", required=False, default="https", help="Communication protocol")
@click.option("-H", "--host", required=False, default="fedn.scaleoutsystems.com", help="Hostname of controller (api)")
@click.option("-p", "--protocol", required=False, default=CONTROLLER_DEFAULTS["protocol"], help="Communication protocol of controller (api)")
@click.option("-H", "--host", required=False, default=CONTROLLER_DEFAULTS["host"], help="Hostname of controller (api)")
@click.option("-P", "--port", required=False, default=CONTROLLER_DEFAULTS["port"], help="Port of controller (api)")
@click.pass_context
def login_cmd(ctx, protocol: str, host: str):
def login_cmd(ctx, protocol: str, host: str, port: str = None):
"""Logging into FEDn Studio"""
# Step 1: Display welcome message
click.secho("Welcome to Scaleout FEDn!", fg="green")
Expand All @@ -44,7 +46,7 @@ def login_cmd(ctx, protocol: str, host: str):

# Handle the response
if response.status_code == 200:
context_data = get_context(response, protocol, host)
context_data = get_context(response, protocol, host, port)

context_path = os.path.join(home_dir, ".fedn")
if not os.path.exists(context_path):
Expand All @@ -58,33 +60,39 @@ def login_cmd(ctx, protocol: str, host: str):
click.secho(f"Unexpected error: {response.text}", fg="red")


def get_context(response, protocol, host):
def get_context(response, protocol, host, port):
user_token_data = response.json()

if user_token_data.get("access"):
click.secho("Login successful!", fg="green")
user_access_token = user_token_data.get("access")
url_projects = f"{protocol}://{host}/api/v1/projects"
url_projects = f"{protocol}://{host}/api/v1/projects" # get_api_url(protocol=protocol, host=host, port=port, endpoint="projects")
headers_projects = {}

if user_access_token:
headers_projects = {"Authorization": f"Bearer {user_access_token}"}
user_access_token = user_token_data.get("access")
_token = get_token(user_access_token, True)
if _token:
headers_projects["Authorization"] = _token

try:
response_projects = requests.get(url_projects, headers=headers_projects)
projects_response_json = response_projects.json()
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url_projects}")

headers_projects["X-Project-Slug"] = projects_response_json[0].get("slug")
url_project_token = f"{protocol}://{host}/api/v1/admin-token"
slug = projects_response_json[0].get("slug")
headers_projects["X-Project-Slug"] = slug
url_project_token = f"{protocol}://{host}/api/v1/admin-token" # get_api_url(protocol=protocol, host=host, port=port, endpoint="admin-token")
try:
response_project_tokens = requests.get(url_project_token, headers=headers_projects)
project_tokens = response_project_tokens.json()
except requests.exceptions.ConnectionError:
click.echo(f"Error: Could not connect to {url_project_token}")

context_data = {"User tokens": user_token_data, "Active project tokens": project_tokens, "Active project slug": projects_response_json[0].get("slug")}
controller_url = f"{protocol}://{host}/{slug}-fedn-reducer"
context_data = {
"User tokens": user_token_data,
"Active project tokens": project_tokens,
"Active project slug": slug,
"Active project url": controller_url,
}
click.secho("Login successful!", fg="green")
return context_data
else:
click.secho("Login failed. Please check your credentials.", fg="red")
12 changes: 3 additions & 9 deletions fedn/cli/model_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
@main.group("model")
@click.pass_context
def model_cmd(ctx):
""":param ctx:
"""
""":param ctx:"""
pass


Expand All @@ -30,21 +29,19 @@ def list_models(ctx, protocol: str, host: str, port: str, token: str = None, ses
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="models")


headers = {}

if n_max:
headers["X-Limit"] = n_max

_token = get_token(token)
_token = get_token(token, False)

if _token:
headers["Authorization"] = _token

if session_id:
url = f"{url}?session_id={session_id}"


try:
response = requests.get(url, headers=headers)
print_response(response, "models", None)
Expand All @@ -67,19 +64,16 @@ def get_model(ctx, protocol: str, host: str, port: str, token: str = None, id: s
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="models")


headers = {}


_token = get_token(token)
_token = get_token(token, False)

if _token:
headers["Authorization"] = _token

if id:
url = f"{url}{id}"


try:
response = requests.get(url, headers=headers)
print_response(response, "model", id)
Expand Down
16 changes: 5 additions & 11 deletions fedn/cli/package_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
@main.group("package")
@click.pass_context
def package_cmd(ctx):
""":param ctx:
"""
""":param ctx:"""
pass


Expand Down Expand Up @@ -61,12 +60,11 @@ def list_packages(ctx, protocol: str, host: str, port: str, token: str = None, n
if n_max:
headers["X-Limit"] = n_max

_token = get_token(token)
_token = get_token(token, False)

if _token:
headers["Authorization"] = _token


try:
response = requests.get(url, headers=headers)
print_response(response, "packages", None)
Expand All @@ -87,19 +85,15 @@ def get_package(ctx, protocol: str, host: str, port: str, token: str = None, id:
- result: package with given id
"""
url = get_api_url(protocol=protocol, host=host, port=port, endpoint="packages")
_url = get_api_url(protocol=protocol, host=host, port=port, endpoint="packages")
url = f"{_url}{id}"
headers = {}


_token = get_token(token)
_token = get_token(token, False)

if _token:
headers["Authorization"] = _token

if id:
url = f"{url}{id}"


try:
response = requests.get(url, headers=headers)
print_response(response, "package", id)
Expand Down
Loading

0 comments on commit 08594c0

Please sign in to comment.