mirror of
https://github.com/openziti/zrok.git
synced 2025-06-20 17:58:50 +02:00
make overview a class with data types instead of a function that returns JSON
This commit is contained in:
parent
9ee3db852a
commit
701f897678
@ -1,20 +1,75 @@
|
|||||||
from zrok.environment.root import Root
|
import json
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from typing import List
|
||||||
|
|
||||||
import urllib3
|
import urllib3
|
||||||
|
from zrok.environment.root import Root
|
||||||
|
|
||||||
|
from zrok_api.models.environment import Environment
|
||||||
|
from zrok_api.models.share import Share
|
||||||
|
|
||||||
|
|
||||||
def Overview(root: Root) -> str:
|
@dataclass
|
||||||
if not root.IsEnabled():
|
class EnvironmentAndShares:
|
||||||
raise Exception("environment is not enabled; enable with 'zrok enable' first!")
|
environment: Environment
|
||||||
|
shares: List[Share] = field(default_factory=list)
|
||||||
|
|
||||||
http = urllib3.PoolManager()
|
|
||||||
apiEndpoint = root.ApiEndpoint().endpoint
|
@dataclass
|
||||||
try:
|
class Overview:
|
||||||
response = http.request(
|
environments: List[EnvironmentAndShares] = field(default_factory=list)
|
||||||
'GET',
|
|
||||||
apiEndpoint + "/api/v1/overview",
|
@classmethod
|
||||||
headers={
|
def create(cls, root: Root) -> 'Overview':
|
||||||
"X-TOKEN": root.env.Token
|
if not root.IsEnabled():
|
||||||
})
|
raise Exception("environment is not enabled; enable with 'zrok enable' first!")
|
||||||
except Exception as e:
|
|
||||||
raise Exception("unable to get account overview", e)
|
http = urllib3.PoolManager()
|
||||||
return response.data.decode('utf-8')
|
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))
|
||||||
|
|
||||||
|
return overview
|
||||||
|
Loading…
x
Reference in New Issue
Block a user