Python
Требуемые библиотеки:
Библиотека
Версия
requests
2.27.1
Код SDK:
import requests
class PureServers:
def __init__(self, email, password):
self.email = email
self.password = password
self.session = None
def request(self, url, method, data=None, secured=False):
if data is None:
data = {}
headers = {}
if secured and self.session:
headers['session'] = self.session
try:
if method == 'POST':
res = requests.post(f'https://cp.pureservers.org/api/{url}', json=data, headers=headers)
elif method == 'GET':
res = requests.get(f'https://cp.pureservers.org/api/{url}', headers=headers)
else:
raise ValueError("Unsupported method")
res.raise_for_status()
if 'session' in res.headers:
self.session = res.headers['session']
return {'data': res.json(), 'headers': res.headers, 'success': True}
except requests.RequestException as e:
return {'data': e.response.json() if e.response else "No response", 'headers': e.response.headers if e.response else {}, 'success': False}
def login(self):
res = self.request('auth/login', 'POST', {'email': self.email, 'password': self.password})
if res['success']:
return True
return res
Пример использования с получением списка серверов:
email = "<email>"
password = "<password>"
api = PureServers(email, password)
login = api.login()
if login == True:
servers_list = api.request('servers/list', 'GET', {}, True)
print(servers_list)
else:
print("Login failed", login_result)
Last updated