add option to load Caddyfile
This commit is contained in:
42
CaddyAPI.py
42
CaddyAPI.py
@@ -5,7 +5,7 @@ import json
|
|||||||
class CaddyAPI:
|
class CaddyAPI:
|
||||||
"""API wrapper for Caddy"""
|
"""API wrapper for Caddy"""
|
||||||
__AUTHOR__ = 'anima'
|
__AUTHOR__ = 'anima'
|
||||||
__VERSION__ = '0.1.0'
|
__VERSION__ = '0.2.0'
|
||||||
|
|
||||||
_log = logging.getLogger(__name__)
|
_log = logging.getLogger(__name__)
|
||||||
__logFile = logging.FileHandler('CaddyAPI.log')
|
__logFile = logging.FileHandler('CaddyAPI.log')
|
||||||
@@ -20,18 +20,28 @@ class CaddyAPI:
|
|||||||
self.__baseurl = host
|
self.__baseurl = host
|
||||||
self.__port = port
|
self.__port = port
|
||||||
|
|
||||||
def __query(self, path: str, data: str = None) -> dict | bool:
|
def __query(self, path: str, data: str = None, contentType: str = 'json') -> dict | bool:
|
||||||
"""base query method
|
"""base query method
|
||||||
|
|
||||||
"""
|
"""
|
||||||
url = f'http://{self.__baseurl}:{self.__port}/{path}'
|
url = f'http://{self.__baseurl}:{self.__port}/{path}'
|
||||||
headers = {
|
headers = {"Accept": "application/json"}
|
||||||
"Accept": "application/json"
|
match contentType:
|
||||||
}
|
case 'json':
|
||||||
|
headers['Content-Type'] = 'text/caddyfile'
|
||||||
|
case 'caddyfile':
|
||||||
|
headers['Content-Type'] = 'text/caddyfile'
|
||||||
|
case _:
|
||||||
|
self._log.error(f'not a supported {contentType=}')
|
||||||
|
return False
|
||||||
|
|
||||||
if data is None:
|
if data is None:
|
||||||
response = requests.get(url, headers = headers)
|
response = requests.get(url, headers = headers)
|
||||||
else:
|
else:
|
||||||
|
if contentType == 'json':
|
||||||
response = requests.post(url, headers = headers, json = data)
|
response = requests.post(url, headers = headers, json = data)
|
||||||
|
else:
|
||||||
|
response = requests.post(url, headers = headers, data = data)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
content = response.content.decode()
|
content = response.content.decode()
|
||||||
@@ -43,21 +53,33 @@ class CaddyAPI:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def get_config(self, saveTo: str = None) -> dict:
|
def get_config(self, saveTo: str = None, path: str = None) -> dict:
|
||||||
config = self.__query('config')
|
if path is None:
|
||||||
|
path = 'config'
|
||||||
|
config = self.__query(path)
|
||||||
if config and config is not None:
|
if config and config is not None:
|
||||||
if saveTo is not None:
|
if saveTo is not None:
|
||||||
with open(saveTo, 'w') as f:
|
with open(saveTo, 'w') as f:
|
||||||
f.write(json.dumps(config))
|
f.write(json.dumps(config))
|
||||||
return config
|
return config
|
||||||
|
|
||||||
def load_config(self, configFile: str):
|
def load_config(self, configFile: str, configAdaapter: str = 'json'):
|
||||||
if configFile is not None:
|
if configFile is not None:
|
||||||
|
match configAdaapter:
|
||||||
|
case 'json':
|
||||||
with open(configFile, 'r') as f:
|
with open(configFile, 'r') as f:
|
||||||
config = json.load(f)
|
config = json.load(f)
|
||||||
return self.__query('load', config)
|
return self.__query('load', config)
|
||||||
return config
|
case 'caddyfile':
|
||||||
|
with open(configFile) as f:
|
||||||
|
config = f.read()
|
||||||
|
return self.__query('load', config, configAdaapter)
|
||||||
|
case _:
|
||||||
|
self._log.error(f'not supported {configAdaapter=}')
|
||||||
|
return False
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
self._log.error(f'cant load config without configfile {configFile=}')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@@ -65,5 +87,5 @@ if __name__ == '__main__':
|
|||||||
caddy = CaddyAPI('localhost')
|
caddy = CaddyAPI('localhost')
|
||||||
data = caddy.get_config('caddy.bak.json')
|
data = caddy.get_config('caddy.bak.json')
|
||||||
print(f'{data=}')
|
print(f'{data=}')
|
||||||
load = caddy.load_config('caddy.json')
|
load = caddy.load_config('Caddyfile', 'caddyfile')
|
||||||
print(f'{load=}')
|
print(f'{load=}')
|
||||||
Reference in New Issue
Block a user