-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make overview a class with data types instead of a function that retu…
…rns JSON
- Loading branch information
Showing
1 changed file
with
72 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,75 @@ | ||
from zrok.environment.root import Root | ||
import json | ||
from dataclasses import dataclass, field | ||
from typing import List | ||
|
||
import urllib3 | ||
from zrok.environment.root import Root | ||
|
||
from zrok_api.models.environment import Environment | ||
from zrok_api.models.share import Share | ||
|
||
|
||
@dataclass | ||
class EnvironmentAndShares: | ||
environment: Environment | ||
shares: List[Share] = field(default_factory=list) | ||
|
||
|
||
@dataclass | ||
class Overview: | ||
environments: List[EnvironmentAndShares] = field(default_factory=list) | ||
|
||
@classmethod | ||
def create(cls, root: Root) -> 'Overview': | ||
if not root.IsEnabled(): | ||
raise Exception("environment is not enabled; enable with 'zrok enable' first!") | ||
|
||
http = urllib3.PoolManager() | ||
apiEndpoint = root.ApiEndpoint().endpoint | ||
try: | ||
response = http.request( | ||
'GET', | ||
apiEndpoint + "/api/v1/overview", | ||
headers={ | ||
"X-TOKEN": root.env.Token | ||
}) | ||
except Exception as e: | ||
raise Exception("unable to get account overview", e) | ||
|
||
json_data = json.loads(response.data.decode('utf-8')) | ||
overview = cls() | ||
|
||
for env_data in json_data.get('environments', []): | ||
env_dict = env_data['environment'] | ||
# Map the JSON keys to the Environment class parameters | ||
environment = Environment( | ||
description=env_dict.get('description'), | ||
host=env_dict.get('host'), | ||
address=env_dict.get('address'), | ||
z_id=env_dict.get('zId'), | ||
activity=env_dict.get('activity'), | ||
limited=env_dict.get('limited'), | ||
created_at=env_dict.get('createdAt'), | ||
updated_at=env_dict.get('updatedAt') | ||
) | ||
# Map the JSON keys to the Share class parameters | ||
shares = [] | ||
for share_data in env_data.get('shares', []): | ||
share = Share( | ||
token=share_data.get('token'), | ||
z_id=share_data.get('zId'), | ||
share_mode=share_data.get('shareMode'), | ||
backend_mode=share_data.get('backendMode'), | ||
frontend_selection=share_data.get('frontendSelection'), | ||
frontend_endpoint=share_data.get('frontendEndpoint'), | ||
backend_proxy_endpoint=share_data.get('backendProxyEndpoint'), | ||
reserved=share_data.get('reserved'), | ||
activity=share_data.get('activity'), | ||
limited=share_data.get('limited'), | ||
created_at=share_data.get('createdAt'), | ||
updated_at=share_data.get('updatedAt') | ||
) | ||
shares.append(share) | ||
overview.environments.append(EnvironmentAndShares(environment=environment, shares=shares)) | ||
|
||
def Overview(root: Root) -> str: | ||
if not root.IsEnabled(): | ||
raise Exception("environment is not enabled; enable with 'zrok enable' first!") | ||
|
||
http = urllib3.PoolManager() | ||
apiEndpoint = root.ApiEndpoint().endpoint | ||
try: | ||
response = http.request( | ||
'GET', | ||
apiEndpoint + "/api/v1/overview", | ||
headers={ | ||
"X-TOKEN": root.env.Token | ||
}) | ||
except Exception as e: | ||
raise Exception("unable to get account overview", e) | ||
return response.data.decode('utf-8') | ||
return overview |