-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add fetching `info`. - Create classes and enum for all endpoints.
- Loading branch information
1 parent
acc72e0
commit 7f243f9
Showing
4 changed files
with
145 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# -*- coding: utf-8 -*- | ||
from .core import Endpoint, Mikrus | ||
from .response import * |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# -*- coding: utf-8 -*- | ||
from os import getenv | ||
|
||
from requests import HTTPError | ||
|
||
from mikrus.core import Mikrus | ||
|
||
|
||
if __name__ == '__main__': | ||
SEREVR_NAME = getenv('SERVER_NAME') | ||
API_KEY = getenv('API_KEY') | ||
server = Mikrus(SEREVR_NAME, API_KEY) | ||
try: | ||
info = server.info() | ||
print(f'Server: {info.server_name} (ID: {info.server_id})') | ||
except HTTPError as e: | ||
print(f'Error {e.errno}: {e.strerror}') |
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# -*- coding: utf-8 -*- | ||
from enum import Enum | ||
from json import loads as json_loads | ||
from os import getenv | ||
|
||
from requests import HTTPError, post | ||
|
||
from mikrus.response import * | ||
|
||
|
||
BASE_URL = 'https://api.mikr.us' | ||
|
||
|
||
class Endpoint(Enum): | ||
INFO = 'info' | ||
SERVERS = 'serwery' | ||
RESTART = 'restart' | ||
LOGS_LIST = 'logs' | ||
LOG_BY_ID = 'logs/ID' | ||
BOOST = 'amfetamina' | ||
DATABASES = 'db' | ||
COMMAND = 'exec' | ||
STATS = 'stats' | ||
PORTS = 'porty' | ||
CLOUD = 'cloud' | ||
|
||
|
||
class Mikrus: | ||
def __init__(self, server: str, key: str) -> None: | ||
self.server: str = server | ||
self.key: str = key | ||
# Cache base URL | ||
self.base_url: str = BASE_URL | ||
|
||
def _request(self, endpoint: Endpoint, params: str = None) -> dict: | ||
url = f'{self.base_url}/{endpoint.value}' | ||
request = post( | ||
url, | ||
headers={ | ||
'Accept': 'application/json; utf-8' | ||
}, | ||
data={ | ||
'srv': self.server, | ||
'key': self.key | ||
} | ||
) | ||
if (code := request.status_code) != 200: | ||
response = dict( | ||
error = dict( | ||
code = code, | ||
message = request.json().get('error', 'Unknown error.') | ||
)) | ||
else: | ||
response = request.json() | ||
return response | ||
|
||
def info(self) -> MikrusInfo: | ||
response =self._request(Endpoint.INFO) | ||
if (error := response.get('error')): | ||
raise HTTPError( | ||
error.get('code'), | ||
error.get('message') | ||
) | ||
return MikrusInfo(response) |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# -*- coding: utf-8 -*- | ||
class MikrusInfo: | ||
def __init__(self, payload: dict) -> None: | ||
self.server_id: str = payload.get('server_id') | ||
self.server_name: str = payload.get('server_name') | ||
self.expires: str = payload.get('expires') | ||
self.expires_cytrus: str = payload.get('expires_cytrus') | ||
self.expires_storage: str = payload.get('expires_storage') | ||
self.param_ram: int = int(payload.get('param_ram', 0)) | ||
self.param_disk: int = int(payload.get('param_disk', 0)) | ||
self.last_login: str = payload.get('lastlog_panel') | ||
|
||
|
||
class MikrusServers: | ||
def __init__(self, payload: dict) -> None: | ||
self.payload = payload | ||
|
||
|
||
class MikrusRestart: | ||
def __init__(self, payload: dict) -> None: | ||
self.payload = payload | ||
|
||
|
||
class MikrusLogsList: | ||
def __init__(self, payload: dict) -> None: | ||
self.payload = payload | ||
|
||
|
||
class MikrusLog: | ||
def __init__(self, payload: dict) -> None: | ||
self.payload = payload | ||
|
||
|
||
class MikrusBoost: | ||
def __init__(self, payload: dict) -> None: | ||
self.payload = payload | ||
|
||
|
||
class MikrusDatabases: | ||
def __init__(self, payload: dict) -> None: | ||
self.payload = payload | ||
|
||
|
||
class MikrusCommand: | ||
def __init__(self, payload: dict) -> None: | ||
self.payload = payload | ||
|
||
|
||
class MikrusStats: | ||
def __init__(self, payload: dict) -> None: | ||
self.payload = payload | ||
|
||
|
||
class MikrusPorts: | ||
def __init__(self, payload: dict) -> None: | ||
self.payload = payload | ||
|
||
|
||
class MikrusCloud: | ||
def __init__(self, payload: dict) -> None: | ||
self.payload = payload |