Skip to content

Commit 97bd696

Browse files
add redir api
1 parent 66f17ca commit 97bd696

File tree

1 file changed

+33
-10
lines changed
  • custom_components/npm_switches

1 file changed

+33
-10
lines changed

custom_components/npm_switches/api.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ def __init__(
3333
self._token_expires = dt.utcnow()
3434
self._headers = None
3535
self.proxy_hosts_data = None
36-
self.num_enabled = 0
37-
self.num_disabled = 0
36+
self.redirection_hosts_data = None
37+
self.num_proxy_enabled = 0
38+
self.num_proxy_disabled = 0
39+
self.num_redir_enabled = 0
40+
self.num_redir_disabled = 0
3841

3942
async def async_get_data(self) -> dict:
4043
"""Get data from the API."""
@@ -48,8 +51,8 @@ async def async_get_data(self) -> dict:
4851

4952
async def get_proxy_hosts(self) -> list():
5053
"""Get a list of proxy-hosts."""
51-
self.num_enabled = 0
52-
self.num_disabled = 0
54+
self.num_proxy_enabled = 0
55+
self.num_proxy_disabled = 0
5356

5457
if self._token is None:
5558
await self.async_get_new_token()
@@ -59,12 +62,32 @@ async def get_proxy_hosts(self) -> list():
5962
for proxy in proxy_hosts_list:
6063
self.proxy_hosts_data[str(proxy["id"])] = proxy
6164
if proxy["enabled"] == 1:
62-
self.num_enabled += 1
65+
self.num_proxy_enabled += 1
6366
else:
64-
self.num_disabled += 1
67+
self.num_proxy_disabled += 1
6568

6669
return self.proxy_hosts_data
6770

71+
async def get_redirection_hosts(self) -> list():
72+
"""Get a list of redirection-hosts."""
73+
self.num_redir_enabled = 0
74+
self.num_redir_disabled = 0
75+
76+
if self._token is None:
77+
await self.async_get_new_token()
78+
url = self._npm_url + "/api/nginx/redirection-hosts"
79+
redirection_hosts_list = await self.api_wrapper("get", url, headers=self._headers)
80+
print(redirection_hosts_list)
81+
self.redirection_hosts_data = {}
82+
for redirection in redirection_hosts_list:
83+
self.redirection_hosts_data[str(redirection["id"])] = redirection
84+
if redirection["enabled"] == 1:
85+
self.num_redir_enabled += 1
86+
else:
87+
self.num_redir_disabled += 1
88+
89+
return self.redirection_hosts_data
90+
6891
async def get_proxy(self, proxy_id: int) -> dict:
6992
"""Get a proxy by id."""
7093
return self.proxy_hosts_data[proxy_id]
@@ -129,14 +152,14 @@ def is_proxy_enabled(self, proxy_id: str) -> bool:
129152
return False
130153

131154
@property
132-
def get_num_enabled(self) -> int:
155+
def get_num_proxy_enabled(self) -> int:
133156
"""Return the num enabled proxy hosts."""
134-
return self.num_enabled
157+
return self.num_proxy_enabled
135158

136159
@property
137-
def get_num_disabled(self) -> int:
160+
def get_num_proxy_disabled(self) -> int:
138161
"""Return the num disabled proxy hosts."""
139-
return self.num_disabled
162+
return self.num_proxy_disabled
140163

141164
@property
142165
def get_npm_url(self) -> str:

0 commit comments

Comments
 (0)