152 lines
4.3 KiB
Python
152 lines
4.3 KiB
Python
#!/usr/bin/env python
|
|
# ~
|
|
# ~ Copyright (C) 2021 Mauricio Baeza Servin - elmau [AT] correlibre [DOT] net
|
|
# ~
|
|
# ~ This program is free software: you can redistribute it and/or modify
|
|
# ~ it under the terms of the GNU General Public License as published by
|
|
# ~ the Free Software Foundation, either version 3 of the License, or
|
|
# ~ (at your option) any later version.
|
|
# ~
|
|
# ~ This program is distributed in the hope that it will be useful,
|
|
# ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# ~ GNU General Public License for more details.
|
|
# ~
|
|
# ~ You should have received a copy of the GNU General Public License
|
|
# ~ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
import json
|
|
import logging
|
|
import requests
|
|
from requests.exceptions import ConnectionError, Timeout
|
|
|
|
|
|
LOG_FORMAT = '%(asctime)s - %(levelname)s - %(message)s'
|
|
LOG_DATE = '%d/%m/%Y %H:%M:%S'
|
|
logging.addLevelName(logging.ERROR, '\033[1;41mERROR\033[1;0m')
|
|
logging.addLevelName(logging.DEBUG, '\x1b[33mDEBUG\033[1;0m')
|
|
logging.addLevelName(logging.INFO, '\x1b[32mINFO\033[1;0m')
|
|
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT, datefmt=LOG_DATE)
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
TIMEOUT = 10
|
|
STATUS = {
|
|
'OK': 200,
|
|
'INVALID': 422,
|
|
}
|
|
METHODS = {
|
|
'AUTH': 'auth/login',
|
|
}
|
|
|
|
|
|
class ACloudServer(object):
|
|
|
|
def __init__(self, server, version='v1'):
|
|
self._server = f'{server}/api/{version}'
|
|
self._token = ''
|
|
self._error = ''
|
|
self._status = 0
|
|
|
|
@property
|
|
def error(self):
|
|
return self._error
|
|
|
|
@property
|
|
def status(self):
|
|
return self._status
|
|
|
|
@property
|
|
def token(self):
|
|
return self._token
|
|
@token.setter
|
|
def token(self, value):
|
|
self._token = value
|
|
|
|
def _post(self, method, data, headers={}):
|
|
if not headers:
|
|
headers = {
|
|
'Authorization': f'Bearer {self.token}',
|
|
'accept': 'application/json',
|
|
}
|
|
url = f'{self._server}/{method}'
|
|
try:
|
|
response = requests.post(url, data=data, headers=headers, timeout=TIMEOUT)
|
|
except ConnectionError:
|
|
self._error = 'ConnectionError'
|
|
log.error('ConnectionError')
|
|
except Timeout:
|
|
self._error = 'Timeout'
|
|
log.error('Timeout')
|
|
|
|
if self.error:
|
|
return
|
|
|
|
self._status = response.status_code
|
|
data = response.json()
|
|
# ~ log.debug(data)
|
|
|
|
if self.status == STATUS['OK']:
|
|
return data
|
|
|
|
if self.status == STATUS['INVALID']:
|
|
self._error = data['errors']
|
|
log.error(self.error)
|
|
|
|
return
|
|
|
|
def _delete(self, method, params):
|
|
headers = {
|
|
'Authorization': f'Bearer {self.token}',
|
|
'accept': 'application/json',
|
|
'Content-Type': 'application/json',
|
|
}
|
|
url = f'{self._server}/{method}'
|
|
response = requests.delete(url, headers=headers, json=params)
|
|
self._status = response.status_code
|
|
return response.json()
|
|
|
|
def login(self, user, contra, token_name='token'):
|
|
data = {
|
|
'email': user,
|
|
'password': contra,
|
|
'token_name': token_name
|
|
}
|
|
headers = {'accept': 'application/json'}
|
|
data = self._post(METHODS['AUTH'], data, headers)
|
|
|
|
if self.error:
|
|
return False
|
|
|
|
self._token = data['user']['access_token']
|
|
return True
|
|
|
|
def folder_create(self, name, id_parent=0):
|
|
self._error = ''
|
|
id = None
|
|
data = {'name': name, 'parentID': id_parent}
|
|
data = self._post('folders', data=data)
|
|
|
|
print(data)
|
|
|
|
if self.status == STATUS['OK']:
|
|
id = data['folder']['id']
|
|
else:
|
|
self._error = data['errors']
|
|
log.error(self._error)
|
|
return id
|
|
|
|
def delete(self, ids, delete_forever=False):
|
|
if isinstance(ids, int):
|
|
ids = [ids]
|
|
result = True
|
|
params = {'entryIds': ids, 'deleteForever': delete_forever}
|
|
data = self._delete('entries', params)
|
|
if self.status != STATUS['OK']:
|
|
self._error = data['errors']
|
|
log.error(self._error)
|
|
result = False
|
|
return result
|
|
|