Skip to content

Commit

Permalink
Release v0.1.0
Browse files Browse the repository at this point in the history
- Add fetching `info`.
- Create classes and enum for all endpoints.
  • Loading branch information
AnonymousX86 committed Nov 24, 2023
1 parent acc72e0 commit 7f243f9
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mikrus/__init__.py
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 *
17 changes: 17 additions & 0 deletions mikrus/__main__.py
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}')
64 changes: 64 additions & 0 deletions mikrus/core.py
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)
61 changes: 61 additions & 0 deletions mikrus/response.py
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

0 comments on commit 7f243f9

Please sign in to comment.