Format code using black
This commit is contained in:
parent
efe4fa027e
commit
c27f020d73
85 changed files with 3238 additions and 2243 deletions
|
@ -6,23 +6,28 @@ import socket
|
|||
import requests
|
||||
from requests.auth import AuthBase
|
||||
|
||||
|
||||
def get_protocol(config):
|
||||
positive_values = ['Yes', 'yes', 'True', 'true', True]
|
||||
port = config['general'].get('base_port', 80)
|
||||
force_ssl = config['general'].get('force_ssl', False)
|
||||
positive_values = ["Yes", "yes", "True", "true", True]
|
||||
port = config["general"].get("base_port", 80)
|
||||
force_ssl = config["general"].get("force_ssl", False)
|
||||
if force_ssl in positive_values:
|
||||
protocol = 'https'
|
||||
protocol = "https"
|
||||
else:
|
||||
protocol = config['general'].get('protocol')
|
||||
protocol = config["general"].get("protocol")
|
||||
if not protocol:
|
||||
protocol = str(("http", "https")[int(port) == 443])
|
||||
return protocol
|
||||
|
||||
|
||||
class UrlParamDict(dict):
|
||||
def __missing__(self, key):
|
||||
return '{' + key + '}'
|
||||
return "{" + key + "}"
|
||||
|
||||
|
||||
class UrlException(Exception):
|
||||
pass
|
||||
|
||||
class UrlException(Exception): pass
|
||||
|
||||
class IncompleteUrl(UrlException):
|
||||
def __init__(self, url):
|
||||
|
@ -31,6 +36,7 @@ class IncompleteUrl(UrlException):
|
|||
def __str__(self):
|
||||
return "Incomplete url: '{}'".format(self.url)
|
||||
|
||||
|
||||
class UrlBadParam(UrlException):
|
||||
def __init__(self, url, param):
|
||||
self.url = url
|
||||
|
@ -39,17 +45,20 @@ class UrlBadParam(UrlException):
|
|||
def __str__(self):
|
||||
return "Bad param '{}' passed into url: '{}'".format(self.param, self.url)
|
||||
|
||||
|
||||
class KeyAuth(AuthBase):
|
||||
def __init__(self, key):
|
||||
self.key = key
|
||||
|
||||
def __call__(self, r):
|
||||
r.headers['Authorization'] = "Api-Key {}".format(self.key)
|
||||
r.headers["Authorization"] = "Api-Key {}".format(self.key)
|
||||
return r
|
||||
|
||||
|
||||
class ApcUrl:
|
||||
""" A safe abstraction and testable for filling in parameters in
|
||||
"""A safe abstraction and testable for filling in parameters in
|
||||
api_client.cfg"""
|
||||
|
||||
def __init__(self, base_url):
|
||||
self.base_url = base_url
|
||||
|
||||
|
@ -63,17 +72,18 @@ class ApcUrl:
|
|||
return ApcUrl(temp_url)
|
||||
|
||||
def url(self):
|
||||
if '{' in self.base_url:
|
||||
if "{" in self.base_url:
|
||||
raise IncompleteUrl(self.base_url)
|
||||
else:
|
||||
return self.base_url
|
||||
|
||||
|
||||
class ApiRequest:
|
||||
API_HTTP_REQUEST_TIMEOUT = 30 # 30 second HTTP request timeout
|
||||
API_HTTP_REQUEST_TIMEOUT = 30 # 30 second HTTP request timeout
|
||||
|
||||
def __init__(self, name, url, logger=None, api_key=None):
|
||||
self.name = name
|
||||
self.url = url
|
||||
self.url = url
|
||||
self.__req = None
|
||||
if logger is None:
|
||||
self.logger = logging
|
||||
|
@ -86,36 +96,45 @@ class ApiRequest:
|
|||
self.logger.debug(final_url)
|
||||
try:
|
||||
if _post_data:
|
||||
response = requests.post(final_url,
|
||||
data=_post_data, auth=self.auth,
|
||||
timeout=ApiRequest.API_HTTP_REQUEST_TIMEOUT)
|
||||
response = requests.post(
|
||||
final_url,
|
||||
data=_post_data,
|
||||
auth=self.auth,
|
||||
timeout=ApiRequest.API_HTTP_REQUEST_TIMEOUT,
|
||||
)
|
||||
else:
|
||||
response = requests.get(final_url, params=params, auth=self.auth,
|
||||
timeout=ApiRequest.API_HTTP_REQUEST_TIMEOUT)
|
||||
if 'application/json' in response.headers['content-type']:
|
||||
response = requests.get(
|
||||
final_url,
|
||||
params=params,
|
||||
auth=self.auth,
|
||||
timeout=ApiRequest.API_HTTP_REQUEST_TIMEOUT,
|
||||
)
|
||||
if "application/json" in response.headers["content-type"]:
|
||||
return response.json()
|
||||
return response
|
||||
except requests.exceptions.Timeout:
|
||||
self.logger.error('HTTP request to %s timed out', final_url)
|
||||
self.logger.error("HTTP request to %s timed out", final_url)
|
||||
raise
|
||||
|
||||
def req(self, *args, **kwargs):
|
||||
self.__req = lambda : self(*args, **kwargs)
|
||||
self.__req = lambda: self(*args, **kwargs)
|
||||
return self
|
||||
|
||||
def retry(self, n, delay=5):
|
||||
"""Try to send request n times. If after n times it fails then
|
||||
we finally raise exception"""
|
||||
for i in range(0,n-1):
|
||||
for i in range(0, n - 1):
|
||||
try:
|
||||
return self.__req()
|
||||
except Exception:
|
||||
time.sleep(delay)
|
||||
return self.__req()
|
||||
|
||||
|
||||
class RequestProvider:
|
||||
""" Creates the available ApiRequest instance that can be read from
|
||||
a config file """
|
||||
"""Creates the available ApiRequest instance that can be read from
|
||||
a config file"""
|
||||
|
||||
def __init__(self, cfg, endpoints):
|
||||
self.config = cfg
|
||||
self.requests = {}
|
||||
|
@ -123,27 +142,29 @@ class RequestProvider:
|
|||
self.config["general"]["base_dir"] = self.config["general"]["base_dir"][1:]
|
||||
|
||||
protocol = get_protocol(self.config)
|
||||
base_port = self.config['general']['base_port']
|
||||
base_url = self.config['general']['base_url']
|
||||
base_dir = self.config['general']['base_dir']
|
||||
api_base = self.config['api_base']
|
||||
base_port = self.config["general"]["base_port"]
|
||||
base_url = self.config["general"]["base_url"]
|
||||
base_dir = self.config["general"]["base_dir"]
|
||||
api_base = self.config["api_base"]
|
||||
api_url = "{protocol}://{base_url}:{base_port}/{base_dir}{api_base}/{action}".format_map(
|
||||
UrlParamDict(protocol=protocol,
|
||||
base_url=base_url,
|
||||
base_port=base_port,
|
||||
base_dir=base_dir,
|
||||
api_base=api_base
|
||||
))
|
||||
UrlParamDict(
|
||||
protocol=protocol,
|
||||
base_url=base_url,
|
||||
base_port=base_port,
|
||||
base_dir=base_dir,
|
||||
api_base=api_base,
|
||||
)
|
||||
)
|
||||
self.url = ApcUrl(api_url)
|
||||
|
||||
# Now we must discover the possible actions
|
||||
for action_name, action_value in endpoints.items():
|
||||
new_url = self.url.params(action=action_value)
|
||||
if '{api_key}' in action_value:
|
||||
new_url = new_url.params(api_key=self.config["general"]['api_key'])
|
||||
self.requests[action_name] = ApiRequest(action_name,
|
||||
new_url,
|
||||
api_key=self.config['general']['api_key'])
|
||||
if "{api_key}" in action_value:
|
||||
new_url = new_url.params(api_key=self.config["general"]["api_key"])
|
||||
self.requests[action_name] = ApiRequest(
|
||||
action_name, new_url, api_key=self.config["general"]["api_key"]
|
||||
)
|
||||
|
||||
def available_requests(self):
|
||||
return list(self.requests.keys())
|
||||
|
@ -157,15 +178,20 @@ class RequestProvider:
|
|||
else:
|
||||
return super(RequestProvider, self).__getattribute__(attr)
|
||||
|
||||
|
||||
def time_in_seconds(time):
|
||||
return time.hour * 60 * 60 + \
|
||||
time.minute * 60 + \
|
||||
time.second + \
|
||||
time.microsecond / 1000000.0
|
||||
return (
|
||||
time.hour * 60 * 60
|
||||
+ time.minute * 60
|
||||
+ time.second
|
||||
+ time.microsecond / 1000000.0
|
||||
)
|
||||
|
||||
|
||||
def time_in_milliseconds(time):
|
||||
return time_in_seconds(time) * 1000
|
||||
|
||||
|
||||
def fromisoformat(time_string):
|
||||
"""
|
||||
This is required for Python 3.6 support. datetime.time.fromisoformat was
|
||||
|
|
|
@ -26,58 +26,112 @@ api_config = {}
|
|||
api_endpoints = {}
|
||||
|
||||
# URL to get the version number of the server API
|
||||
api_endpoints['version_url'] = 'version/api_key/{api_key}'
|
||||
#URL to register a components IP Address with the central web server
|
||||
api_endpoints['register_component'] = 'register-component/format/json/api_key/{api_key}/component/{component}'
|
||||
api_endpoints["version_url"] = "version/api_key/{api_key}"
|
||||
# URL to register a components IP Address with the central web server
|
||||
api_endpoints[
|
||||
"register_component"
|
||||
] = "register-component/format/json/api_key/{api_key}/component/{component}"
|
||||
|
||||
#media-monitor
|
||||
api_endpoints['media_setup_url'] = 'media-monitor-setup/format/json/api_key/{api_key}'
|
||||
api_endpoints['upload_recorded'] = 'upload-recorded/format/json/api_key/{api_key}/fileid/{fileid}/showinstanceid/{showinstanceid}'
|
||||
api_endpoints['update_media_url'] = 'reload-metadata/format/json/api_key/{api_key}/mode/{mode}'
|
||||
api_endpoints['list_all_db_files'] = 'list-all-files/format/json/api_key/{api_key}/dir_id/{dir_id}/all/{all}'
|
||||
api_endpoints['list_all_watched_dirs'] = 'list-all-watched-dirs/format/json/api_key/{api_key}'
|
||||
api_endpoints['add_watched_dir'] = 'add-watched-dir/format/json/api_key/{api_key}/path/{path}'
|
||||
api_endpoints['remove_watched_dir'] = 'remove-watched-dir/format/json/api_key/{api_key}/path/{path}'
|
||||
api_endpoints['set_storage_dir'] = 'set-storage-dir/format/json/api_key/{api_key}/path/{path}'
|
||||
api_endpoints['update_fs_mount'] = 'update-file-system-mount/format/json/api_key/{api_key}'
|
||||
api_endpoints['reload_metadata_group'] = 'reload-metadata-group/format/json/api_key/{api_key}'
|
||||
api_endpoints['handle_watched_dir_missing'] = 'handle-watched-dir-missing/format/json/api_key/{api_key}/dir/{dir}'
|
||||
#show-recorder
|
||||
api_endpoints['show_schedule_url'] = 'recorded-shows/format/json/api_key/{api_key}'
|
||||
api_endpoints['upload_file_url'] = 'rest/media'
|
||||
api_endpoints['upload_retries'] = '3'
|
||||
api_endpoints['upload_wait'] = '60'
|
||||
#pypo
|
||||
api_endpoints['export_url'] = 'schedule/api_key/{api_key}'
|
||||
api_endpoints['get_media_url'] = 'get-media/file/{file}/api_key/{api_key}'
|
||||
api_endpoints['update_item_url'] = 'notify-schedule-group-play/api_key/{api_key}/schedule_id/{schedule_id}'
|
||||
api_endpoints['update_start_playing_url'] = 'notify-media-item-start-play/api_key/{api_key}/media_id/{media_id}/'
|
||||
api_endpoints['get_stream_setting'] = 'get-stream-setting/format/json/api_key/{api_key}/'
|
||||
api_endpoints['update_liquidsoap_status'] = 'update-liquidsoap-status/format/json/api_key/{api_key}/msg/{msg}/stream_id/{stream_id}/boot_time/{boot_time}'
|
||||
api_endpoints['update_source_status'] = 'update-source-status/format/json/api_key/{api_key}/sourcename/{sourcename}/status/{status}'
|
||||
api_endpoints['check_live_stream_auth'] = 'check-live-stream-auth/format/json/api_key/{api_key}/username/{username}/password/{password}/djtype/{djtype}'
|
||||
api_endpoints['get_bootstrap_info'] = 'get-bootstrap-info/format/json/api_key/{api_key}'
|
||||
api_endpoints['get_files_without_replay_gain'] = 'get-files-without-replay-gain/api_key/{api_key}/dir_id/{dir_id}'
|
||||
api_endpoints['update_replay_gain_value'] = 'update-replay-gain-value/format/json/api_key/{api_key}'
|
||||
api_endpoints['notify_webstream_data'] = 'notify-webstream-data/api_key/{api_key}/media_id/{media_id}/format/json'
|
||||
api_endpoints['notify_liquidsoap_started'] = 'rabbitmq-do-push/api_key/{api_key}/format/json'
|
||||
api_endpoints['get_stream_parameters'] = 'get-stream-parameters/api_key/{api_key}/format/json'
|
||||
api_endpoints['push_stream_stats'] = 'push-stream-stats/api_key/{api_key}/format/json'
|
||||
api_endpoints['update_stream_setting_table'] = 'update-stream-setting-table/api_key/{api_key}/format/json'
|
||||
api_endpoints['get_files_without_silan_value'] = 'get-files-without-silan-value/api_key/{api_key}'
|
||||
api_endpoints['update_cue_values_by_silan'] = 'update-cue-values-by-silan/api_key/{api_key}'
|
||||
api_endpoints['update_metadata_on_tunein'] = 'update-metadata-on-tunein/api_key/{api_key}'
|
||||
api_config['api_base'] = 'api'
|
||||
api_config['bin_dir'] = '/usr/lib/airtime/api_clients/'
|
||||
# media-monitor
|
||||
api_endpoints["media_setup_url"] = "media-monitor-setup/format/json/api_key/{api_key}"
|
||||
api_endpoints[
|
||||
"upload_recorded"
|
||||
] = "upload-recorded/format/json/api_key/{api_key}/fileid/{fileid}/showinstanceid/{showinstanceid}"
|
||||
api_endpoints[
|
||||
"update_media_url"
|
||||
] = "reload-metadata/format/json/api_key/{api_key}/mode/{mode}"
|
||||
api_endpoints[
|
||||
"list_all_db_files"
|
||||
] = "list-all-files/format/json/api_key/{api_key}/dir_id/{dir_id}/all/{all}"
|
||||
api_endpoints[
|
||||
"list_all_watched_dirs"
|
||||
] = "list-all-watched-dirs/format/json/api_key/{api_key}"
|
||||
api_endpoints[
|
||||
"add_watched_dir"
|
||||
] = "add-watched-dir/format/json/api_key/{api_key}/path/{path}"
|
||||
api_endpoints[
|
||||
"remove_watched_dir"
|
||||
] = "remove-watched-dir/format/json/api_key/{api_key}/path/{path}"
|
||||
api_endpoints[
|
||||
"set_storage_dir"
|
||||
] = "set-storage-dir/format/json/api_key/{api_key}/path/{path}"
|
||||
api_endpoints[
|
||||
"update_fs_mount"
|
||||
] = "update-file-system-mount/format/json/api_key/{api_key}"
|
||||
api_endpoints[
|
||||
"reload_metadata_group"
|
||||
] = "reload-metadata-group/format/json/api_key/{api_key}"
|
||||
api_endpoints[
|
||||
"handle_watched_dir_missing"
|
||||
] = "handle-watched-dir-missing/format/json/api_key/{api_key}/dir/{dir}"
|
||||
# show-recorder
|
||||
api_endpoints["show_schedule_url"] = "recorded-shows/format/json/api_key/{api_key}"
|
||||
api_endpoints["upload_file_url"] = "rest/media"
|
||||
api_endpoints["upload_retries"] = "3"
|
||||
api_endpoints["upload_wait"] = "60"
|
||||
# pypo
|
||||
api_endpoints["export_url"] = "schedule/api_key/{api_key}"
|
||||
api_endpoints["get_media_url"] = "get-media/file/{file}/api_key/{api_key}"
|
||||
api_endpoints[
|
||||
"update_item_url"
|
||||
] = "notify-schedule-group-play/api_key/{api_key}/schedule_id/{schedule_id}"
|
||||
api_endpoints[
|
||||
"update_start_playing_url"
|
||||
] = "notify-media-item-start-play/api_key/{api_key}/media_id/{media_id}/"
|
||||
api_endpoints[
|
||||
"get_stream_setting"
|
||||
] = "get-stream-setting/format/json/api_key/{api_key}/"
|
||||
api_endpoints[
|
||||
"update_liquidsoap_status"
|
||||
] = "update-liquidsoap-status/format/json/api_key/{api_key}/msg/{msg}/stream_id/{stream_id}/boot_time/{boot_time}"
|
||||
api_endpoints[
|
||||
"update_source_status"
|
||||
] = "update-source-status/format/json/api_key/{api_key}/sourcename/{sourcename}/status/{status}"
|
||||
api_endpoints[
|
||||
"check_live_stream_auth"
|
||||
] = "check-live-stream-auth/format/json/api_key/{api_key}/username/{username}/password/{password}/djtype/{djtype}"
|
||||
api_endpoints["get_bootstrap_info"] = "get-bootstrap-info/format/json/api_key/{api_key}"
|
||||
api_endpoints[
|
||||
"get_files_without_replay_gain"
|
||||
] = "get-files-without-replay-gain/api_key/{api_key}/dir_id/{dir_id}"
|
||||
api_endpoints[
|
||||
"update_replay_gain_value"
|
||||
] = "update-replay-gain-value/format/json/api_key/{api_key}"
|
||||
api_endpoints[
|
||||
"notify_webstream_data"
|
||||
] = "notify-webstream-data/api_key/{api_key}/media_id/{media_id}/format/json"
|
||||
api_endpoints[
|
||||
"notify_liquidsoap_started"
|
||||
] = "rabbitmq-do-push/api_key/{api_key}/format/json"
|
||||
api_endpoints[
|
||||
"get_stream_parameters"
|
||||
] = "get-stream-parameters/api_key/{api_key}/format/json"
|
||||
api_endpoints["push_stream_stats"] = "push-stream-stats/api_key/{api_key}/format/json"
|
||||
api_endpoints[
|
||||
"update_stream_setting_table"
|
||||
] = "update-stream-setting-table/api_key/{api_key}/format/json"
|
||||
api_endpoints[
|
||||
"get_files_without_silan_value"
|
||||
] = "get-files-without-silan-value/api_key/{api_key}"
|
||||
api_endpoints[
|
||||
"update_cue_values_by_silan"
|
||||
] = "update-cue-values-by-silan/api_key/{api_key}"
|
||||
api_endpoints[
|
||||
"update_metadata_on_tunein"
|
||||
] = "update-metadata-on-tunein/api_key/{api_key}"
|
||||
api_config["api_base"] = "api"
|
||||
api_config["bin_dir"] = "/usr/lib/airtime/api_clients/"
|
||||
|
||||
|
||||
################################################################################
|
||||
# Airtime API Version 1 Client
|
||||
################################################################################
|
||||
class AirtimeApiClient(object):
|
||||
def __init__(self, logger=None,config_path='/etc/airtime/airtime.conf'):
|
||||
if logger is None: self.logger = logging
|
||||
else: self.logger = logger
|
||||
def __init__(self, logger=None, config_path="/etc/airtime/airtime.conf"):
|
||||
if logger is None:
|
||||
self.logger = logging
|
||||
else:
|
||||
self.logger = logger
|
||||
|
||||
# loading config file
|
||||
try:
|
||||
|
@ -85,16 +139,18 @@ class AirtimeApiClient(object):
|
|||
self.config.update(api_config)
|
||||
self.services = RequestProvider(self.config, api_endpoints)
|
||||
except Exception as e:
|
||||
self.logger.exception('Error loading config file: %s', config_path)
|
||||
self.logger.exception("Error loading config file: %s", config_path)
|
||||
sys.exit(1)
|
||||
|
||||
def __get_airtime_version(self):
|
||||
try: return self.services.version_url()['airtime_version']
|
||||
except Exception: return -1
|
||||
try:
|
||||
return self.services.version_url()["airtime_version"]
|
||||
except Exception:
|
||||
return -1
|
||||
|
||||
def __get_api_version(self):
|
||||
try:
|
||||
return self.services.version_url()['api_version']
|
||||
return self.services.version_url()["api_version"]
|
||||
except Exception as e:
|
||||
self.logger.exception(e)
|
||||
return -1
|
||||
|
@ -105,25 +161,30 @@ class AirtimeApiClient(object):
|
|||
# logger.info('Airtime version found: ' + str(version))
|
||||
if api_version == -1:
|
||||
if verbose:
|
||||
logger.info('Unable to get Airtime API version number.\n')
|
||||
logger.info("Unable to get Airtime API version number.\n")
|
||||
return False
|
||||
elif api_version[0:3] != AIRTIME_API_VERSION[0:3]:
|
||||
if verbose:
|
||||
logger.info('Airtime API version found: ' + str(api_version))
|
||||
logger.info('pypo is only compatible with API version: ' + AIRTIME_API_VERSION)
|
||||
logger.info("Airtime API version found: " + str(api_version))
|
||||
logger.info(
|
||||
"pypo is only compatible with API version: " + AIRTIME_API_VERSION
|
||||
)
|
||||
return False
|
||||
else:
|
||||
if verbose:
|
||||
logger.info('Airtime API version found: ' + str(api_version))
|
||||
logger.info('pypo is only compatible with API version: ' + AIRTIME_API_VERSION)
|
||||
logger.info("Airtime API version found: " + str(api_version))
|
||||
logger.info(
|
||||
"pypo is only compatible with API version: " + AIRTIME_API_VERSION
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
def get_schedule(self):
|
||||
# TODO : properly refactor this routine
|
||||
# For now the return type is a little messed up for compatibility reasons
|
||||
try: return (True, self.services.export_url())
|
||||
except: return (False, None)
|
||||
try:
|
||||
return (True, self.services.export_url())
|
||||
except:
|
||||
return (False, None)
|
||||
|
||||
def notify_liquidsoap_started(self):
|
||||
try:
|
||||
|
@ -132,9 +193,9 @@ class AirtimeApiClient(object):
|
|||
self.logger.exception(e)
|
||||
|
||||
def notify_media_item_start_playing(self, media_id):
|
||||
""" This is a callback from liquidsoap, we use this to notify
|
||||
"""This is a callback from liquidsoap, we use this to notify
|
||||
about the currently playing *song*. We get passed a JSON string
|
||||
which we handed to liquidsoap in get_liquidsoap_data(). """
|
||||
which we handed to liquidsoap in get_liquidsoap_data()."""
|
||||
try:
|
||||
return self.services.update_start_playing_url(media_id=media_id)
|
||||
except Exception as e:
|
||||
|
@ -150,7 +211,7 @@ class AirtimeApiClient(object):
|
|||
|
||||
def upload_recorded_show(self, files, show_id):
|
||||
logger = self.logger
|
||||
response = ''
|
||||
response = ""
|
||||
|
||||
retries = int(self.config["upload_retries"])
|
||||
retries_wait = int(self.config["upload_wait"])
|
||||
|
@ -165,7 +226,9 @@ class AirtimeApiClient(object):
|
|||
logger.debug(ApiRequest.API_HTTP_REQUEST_TIMEOUT)
|
||||
|
||||
try:
|
||||
request = requests.post(url, files=files, timeout=float(ApiRequest.API_HTTP_REQUEST_TIMEOUT))
|
||||
request = requests.post(
|
||||
url, files=files, timeout=float(ApiRequest.API_HTTP_REQUEST_TIMEOUT)
|
||||
)
|
||||
response = request.json()
|
||||
logger.debug(response)
|
||||
|
||||
|
@ -199,7 +262,7 @@ class AirtimeApiClient(object):
|
|||
except Exception as e:
|
||||
self.logger.exception(e)
|
||||
|
||||
#wait some time before next retry
|
||||
# wait some time before next retry
|
||||
time.sleep(retries_wait)
|
||||
|
||||
return response
|
||||
|
@ -207,42 +270,49 @@ class AirtimeApiClient(object):
|
|||
def check_live_stream_auth(self, username, password, dj_type):
|
||||
try:
|
||||
return self.services.check_live_stream_auth(
|
||||
username=username, password=password, djtype=dj_type)
|
||||
username=username, password=password, djtype=dj_type
|
||||
)
|
||||
except Exception as e:
|
||||
self.logger.exception(e)
|
||||
return {}
|
||||
|
||||
def construct_url(self,config_action_key):
|
||||
def construct_url(self, config_action_key):
|
||||
"""Constructs the base url for every request"""
|
||||
# TODO : Make other methods in this class use this this method.
|
||||
if self.config["general"]["base_dir"].startswith("/"):
|
||||
self.config["general"]["base_dir"] = self.config["general"]["base_dir"][1:]
|
||||
protocol = get_protocol(self.config)
|
||||
url = "%s://%s:%s/%s%s/%s" % \
|
||||
(protocol,
|
||||
self.config["general"]["base_url"], str(self.config["general"]["base_port"]),
|
||||
self.config["general"]["base_dir"], self.config["api_base"],
|
||||
self.config[config_action_key])
|
||||
url = "%s://%s:%s/%s%s/%s" % (
|
||||
protocol,
|
||||
self.config["general"]["base_url"],
|
||||
str(self.config["general"]["base_port"]),
|
||||
self.config["general"]["base_dir"],
|
||||
self.config["api_base"],
|
||||
self.config[config_action_key],
|
||||
)
|
||||
url = url.replace("%%api_key%%", self.config["general"]["api_key"])
|
||||
return url
|
||||
|
||||
def construct_rest_url(self,config_action_key):
|
||||
def construct_rest_url(self, config_action_key):
|
||||
"""Constructs the base url for RESTful requests"""
|
||||
if self.config["general"]["base_dir"].startswith("/"):
|
||||
self.config["general"]["base_dir"] = self.config["general"]["base_dir"][1:]
|
||||
protocol = get_protocol(self.config)
|
||||
url = "%s://%s:@%s:%s/%s/%s" % \
|
||||
(protocol, self.config["general"]["api_key"],
|
||||
self.config["general"]["base_url"], str(self.config["general"]["base_port"]),
|
||||
self.config["general"]["base_dir"],
|
||||
self.config[config_action_key])
|
||||
url = "%s://%s:@%s:%s/%s/%s" % (
|
||||
protocol,
|
||||
self.config["general"]["api_key"],
|
||||
self.config["general"]["base_url"],
|
||||
str(self.config["general"]["base_port"]),
|
||||
self.config["general"]["base_dir"],
|
||||
self.config[config_action_key],
|
||||
)
|
||||
return url
|
||||
|
||||
|
||||
"""
|
||||
Caller of this method needs to catch any exceptions such as
|
||||
ValueError thrown by json.loads or URLError by urllib2.urlopen
|
||||
"""
|
||||
|
||||
def setup_media_monitor(self):
|
||||
return self.services.media_setup_url()
|
||||
|
||||
|
@ -264,49 +334,55 @@ class AirtimeApiClient(object):
|
|||
# filter but here we prefer a little more verbosity to help
|
||||
# debugging
|
||||
for action in action_list:
|
||||
if not 'mode' in action:
|
||||
self.logger.debug("Warning: Trying to send a request element without a 'mode'")
|
||||
self.logger.debug("Here is the the request: '%s'" % str(action) )
|
||||
if not "mode" in action:
|
||||
self.logger.debug(
|
||||
"Warning: Trying to send a request element without a 'mode'"
|
||||
)
|
||||
self.logger.debug("Here is the the request: '%s'" % str(action))
|
||||
else:
|
||||
# We alias the value of is_record to true or false no
|
||||
# matter what it is based on if it's absent in the action
|
||||
if 'is_record' not in action:
|
||||
action['is_record'] = 0
|
||||
if "is_record" not in action:
|
||||
action["is_record"] = 0
|
||||
valid_actions.append(action)
|
||||
# Note that we must prefix every key with: mdX where x is a number
|
||||
# Is there a way to format the next line a little better? The
|
||||
# parenthesis make the code almost unreadable
|
||||
md_list = dict((("md%d" % i), json.dumps(md)) \
|
||||
for i,md in enumerate(valid_actions))
|
||||
md_list = dict(
|
||||
(("md%d" % i), json.dumps(md)) for i, md in enumerate(valid_actions)
|
||||
)
|
||||
# For testing we add the following "dry" parameter to tell the
|
||||
# controller not to actually do any changes
|
||||
if dry: md_list['dry'] = 1
|
||||
if dry:
|
||||
md_list["dry"] = 1
|
||||
self.logger.info("Pumping out %d requests..." % len(valid_actions))
|
||||
return self.services.reload_metadata_group(_post_data=md_list)
|
||||
|
||||
#returns a list of all db files for a given directory in JSON format:
|
||||
#{"files":["path/to/file1", "path/to/file2"]}
|
||||
#Note that these are relative paths to the given directory. The full
|
||||
#path is not returned.
|
||||
# returns a list of all db files for a given directory in JSON format:
|
||||
# {"files":["path/to/file1", "path/to/file2"]}
|
||||
# Note that these are relative paths to the given directory. The full
|
||||
# path is not returned.
|
||||
def list_all_db_files(self, dir_id, all_files=True):
|
||||
logger = self.logger
|
||||
try:
|
||||
all_files = "1" if all_files else "0"
|
||||
response = self.services.list_all_db_files(dir_id=dir_id,
|
||||
all=all_files)
|
||||
response = self.services.list_all_db_files(dir_id=dir_id, all=all_files)
|
||||
except Exception as e:
|
||||
response = {}
|
||||
logger.error("Exception: %s", e)
|
||||
try:
|
||||
return response["files"]
|
||||
except KeyError:
|
||||
self.logger.error("Could not find index 'files' in dictionary: %s",
|
||||
str(response))
|
||||
self.logger.error(
|
||||
"Could not find index 'files' in dictionary: %s", str(response)
|
||||
)
|
||||
return []
|
||||
|
||||
"""
|
||||
Caller of this method needs to catch any exceptions such as
|
||||
ValueError thrown by json.loads or URLError by urllib2.urlopen
|
||||
"""
|
||||
|
||||
def list_all_watched_dirs(self):
|
||||
return self.services.list_all_watched_dirs()
|
||||
|
||||
|
@ -314,6 +390,7 @@ class AirtimeApiClient(object):
|
|||
Caller of this method needs to catch any exceptions such as
|
||||
ValueError thrown by json.loads or URLError by urllib2.urlopen
|
||||
"""
|
||||
|
||||
def add_watched_dir(self, path):
|
||||
return self.services.add_watched_dir(path=base64.b64encode(path))
|
||||
|
||||
|
@ -321,6 +398,7 @@ class AirtimeApiClient(object):
|
|||
Caller of this method needs to catch any exceptions such as
|
||||
ValueError thrown by json.loads or URLError by urllib2.urlopen
|
||||
"""
|
||||
|
||||
def remove_watched_dir(self, path):
|
||||
return self.services.remove_watched_dir(path=base64.b64encode(path))
|
||||
|
||||
|
@ -328,6 +406,7 @@ class AirtimeApiClient(object):
|
|||
Caller of this method needs to catch any exceptions such as
|
||||
ValueError thrown by json.loads or URLError by urllib2.urlopen
|
||||
"""
|
||||
|
||||
def set_storage_dir(self, path):
|
||||
return self.services.set_storage_dir(path=base64.b64encode(path))
|
||||
|
||||
|
@ -335,15 +414,16 @@ class AirtimeApiClient(object):
|
|||
Caller of this method needs to catch any exceptions such as
|
||||
ValueError thrown by json.loads or URLError by urllib2.urlopen
|
||||
"""
|
||||
|
||||
def get_stream_setting(self):
|
||||
return self.services.get_stream_setting()
|
||||
|
||||
def register_component(self, component):
|
||||
""" Purpose of this method is to contact the server with a "Hey its
|
||||
"""Purpose of this method is to contact the server with a "Hey its
|
||||
me!" message. This will allow the server to register the component's
|
||||
(component = media-monitor, pypo etc.) ip address, and later use it
|
||||
to query monit via monit's http service, or download log files via a
|
||||
http server. """
|
||||
http server."""
|
||||
return self.services.register_component(component=component)
|
||||
|
||||
def notify_liquidsoap_status(self, msg, stream_id, time):
|
||||
|
@ -351,24 +431,24 @@ class AirtimeApiClient(object):
|
|||
try:
|
||||
post_data = {"msg_post": msg}
|
||||
|
||||
#encoded_msg is no longer used server_side!!
|
||||
encoded_msg = urllib.parse.quote('dummy')
|
||||
self.services.update_liquidsoap_status.req(post_data,
|
||||
msg=encoded_msg,
|
||||
stream_id=stream_id,
|
||||
boot_time=time).retry(5)
|
||||
# encoded_msg is no longer used server_side!!
|
||||
encoded_msg = urllib.parse.quote("dummy")
|
||||
self.services.update_liquidsoap_status.req(
|
||||
post_data, msg=encoded_msg, stream_id=stream_id, boot_time=time
|
||||
).retry(5)
|
||||
except Exception as e:
|
||||
self.logger.exception(e)
|
||||
|
||||
def notify_source_status(self, sourcename, status):
|
||||
try:
|
||||
return self.services.update_source_status.req(sourcename=sourcename,
|
||||
status=status).retry(5)
|
||||
return self.services.update_source_status.req(
|
||||
sourcename=sourcename, status=status
|
||||
).retry(5)
|
||||
except Exception as e:
|
||||
self.logger.exception(e)
|
||||
|
||||
def get_bootstrap_info(self):
|
||||
""" Retrieve infomations needed on bootstrap time """
|
||||
"""Retrieve infomations needed on bootstrap time"""
|
||||
return self.services.get_bootstrap_info()
|
||||
|
||||
def get_files_without_replay_gain_value(self, dir_id):
|
||||
|
@ -377,7 +457,7 @@ class AirtimeApiClient(object):
|
|||
calculated. This list of files is downloaded into a file and the path
|
||||
to this file is the return value.
|
||||
"""
|
||||
#http://localhost/api/get-files-without-replay-gain/dir_id/1
|
||||
# http://localhost/api/get-files-without-replay-gain/dir_id/1
|
||||
try:
|
||||
return self.services.get_files_without_replay_gain(dir_id=dir_id)
|
||||
except Exception as e:
|
||||
|
@ -401,25 +481,31 @@ class AirtimeApiClient(object):
|
|||
'pairs' is a list of pairs in (x, y), where x is the file's database
|
||||
row id and y is the file's replay_gain value in dB
|
||||
"""
|
||||
self.logger.debug(self.services.update_replay_gain_value(
|
||||
_post_data={'data': json.dumps(pairs)}))
|
||||
|
||||
self.logger.debug(
|
||||
self.services.update_replay_gain_value(
|
||||
_post_data={"data": json.dumps(pairs)}
|
||||
)
|
||||
)
|
||||
|
||||
def update_cue_values_by_silan(self, pairs):
|
||||
"""
|
||||
'pairs' is a list of pairs in (x, y), where x is the file's database
|
||||
row id and y is the file's cue values in dB
|
||||
"""
|
||||
return self.services.update_cue_values_by_silan(_post_data={'data': json.dumps(pairs)})
|
||||
|
||||
return self.services.update_cue_values_by_silan(
|
||||
_post_data={"data": json.dumps(pairs)}
|
||||
)
|
||||
|
||||
def notify_webstream_data(self, data, media_id):
|
||||
"""
|
||||
Update the server with the latest metadata we've received from the
|
||||
external webstream
|
||||
"""
|
||||
self.logger.info( self.services.notify_webstream_data.req(
|
||||
_post_data={'data':data}, media_id=str(media_id)).retry(5))
|
||||
self.logger.info(
|
||||
self.services.notify_webstream_data.req(
|
||||
_post_data={"data": data}, media_id=str(media_id)
|
||||
).retry(5)
|
||||
)
|
||||
|
||||
def get_stream_parameters(self):
|
||||
response = self.services.get_stream_parameters()
|
||||
|
@ -428,12 +514,16 @@ class AirtimeApiClient(object):
|
|||
|
||||
def push_stream_stats(self, data):
|
||||
# TODO : users of this method should do their own error handling
|
||||
response = self.services.push_stream_stats(_post_data={'data': json.dumps(data)})
|
||||
response = self.services.push_stream_stats(
|
||||
_post_data={"data": json.dumps(data)}
|
||||
)
|
||||
return response
|
||||
|
||||
def update_stream_setting_table(self, data):
|
||||
try:
|
||||
response = self.services.update_stream_setting_table(_post_data={'data': json.dumps(data)})
|
||||
response = self.services.update_stream_setting_table(
|
||||
_post_data={"data": json.dumps(data)}
|
||||
)
|
||||
return response
|
||||
except Exception as e:
|
||||
self.logger.exception(e)
|
||||
|
|
|
@ -18,17 +18,18 @@ LIBRETIME_API_VERSION = "2.0"
|
|||
api_config = {}
|
||||
api_endpoints = {}
|
||||
|
||||
api_endpoints['version_url'] = 'version/'
|
||||
api_endpoints['schedule_url'] = 'schedule/'
|
||||
api_endpoints['webstream_url'] = 'webstreams/{id}/'
|
||||
api_endpoints['show_instance_url'] = 'show-instances/{id}/'
|
||||
api_endpoints['show_url'] = 'shows/{id}/'
|
||||
api_endpoints['file_url'] = 'files/{id}/'
|
||||
api_endpoints['file_download_url'] = 'files/{id}/download/'
|
||||
api_config['api_base'] = 'api/v2'
|
||||
api_endpoints["version_url"] = "version/"
|
||||
api_endpoints["schedule_url"] = "schedule/"
|
||||
api_endpoints["webstream_url"] = "webstreams/{id}/"
|
||||
api_endpoints["show_instance_url"] = "show-instances/{id}/"
|
||||
api_endpoints["show_url"] = "shows/{id}/"
|
||||
api_endpoints["file_url"] = "files/{id}/"
|
||||
api_endpoints["file_download_url"] = "files/{id}/download/"
|
||||
api_config["api_base"] = "api/v2"
|
||||
|
||||
|
||||
class AirtimeApiClient:
|
||||
def __init__(self, logger=None, config_path='/etc/airtime/airtime.conf'):
|
||||
def __init__(self, logger=None, config_path="/etc/airtime/airtime.conf"):
|
||||
if logger is None:
|
||||
self.logger = logging
|
||||
else:
|
||||
|
@ -39,87 +40,89 @@ class AirtimeApiClient:
|
|||
self.config.update(api_config)
|
||||
self.services = RequestProvider(self.config, api_endpoints)
|
||||
except Exception as e:
|
||||
self.logger.exception('Error loading config file: %s', config_path)
|
||||
self.logger.exception("Error loading config file: %s", config_path)
|
||||
sys.exit(1)
|
||||
|
||||
def get_schedule(self):
|
||||
current_time = datetime.datetime.utcnow()
|
||||
end_time = current_time + datetime.timedelta(hours=1)
|
||||
|
||||
str_current = current_time.isoformat(timespec='seconds')
|
||||
str_end = end_time.isoformat(timespec='seconds')
|
||||
data = self.services.schedule_url(params={
|
||||
'ends__range': ('{}Z,{}Z'.format(str_current, str_end)),
|
||||
})
|
||||
result = {'media': {} }
|
||||
for item in data:
|
||||
start = isoparse(item['starts'])
|
||||
key = start.strftime('%YYYY-%mm-%dd-%HH-%MM-%SS')
|
||||
end = isoparse(item['ends'])
|
||||
|
||||
show_instance = self.services.show_instance_url(id=item['instance_id'])
|
||||
show = self.services.show_url(id=show_instance['show_id'])
|
||||
|
||||
result['media'][key] = {
|
||||
'start': start.strftime('%Y-%m-%d-%H-%M-%S'),
|
||||
'end': end.strftime('%Y-%m-%d-%H-%M-%S'),
|
||||
'row_id': item['id']
|
||||
str_current = current_time.isoformat(timespec="seconds")
|
||||
str_end = end_time.isoformat(timespec="seconds")
|
||||
data = self.services.schedule_url(
|
||||
params={
|
||||
"ends__range": ("{}Z,{}Z".format(str_current, str_end)),
|
||||
}
|
||||
current = result['media'][key]
|
||||
if item['file']:
|
||||
current['independent_event'] = False
|
||||
current['type'] = 'file'
|
||||
current['id'] = item['file_id']
|
||||
)
|
||||
result = {"media": {}}
|
||||
for item in data:
|
||||
start = isoparse(item["starts"])
|
||||
key = start.strftime("%YYYY-%mm-%dd-%HH-%MM-%SS")
|
||||
end = isoparse(item["ends"])
|
||||
|
||||
fade_in = time_in_milliseconds(fromisoformat(item['fade_in']))
|
||||
fade_out = time_in_milliseconds(fromisoformat(item['fade_out']))
|
||||
show_instance = self.services.show_instance_url(id=item["instance_id"])
|
||||
show = self.services.show_url(id=show_instance["show_id"])
|
||||
|
||||
cue_in = time_in_seconds(fromisoformat(item['cue_in']))
|
||||
cue_out = time_in_seconds(fromisoformat(item['cue_out']))
|
||||
result["media"][key] = {
|
||||
"start": start.strftime("%Y-%m-%d-%H-%M-%S"),
|
||||
"end": end.strftime("%Y-%m-%d-%H-%M-%S"),
|
||||
"row_id": item["id"],
|
||||
}
|
||||
current = result["media"][key]
|
||||
if item["file"]:
|
||||
current["independent_event"] = False
|
||||
current["type"] = "file"
|
||||
current["id"] = item["file_id"]
|
||||
|
||||
current['fade_in'] = fade_in
|
||||
current['fade_out'] = fade_out
|
||||
current['cue_in'] = cue_in
|
||||
current['cue_out'] = cue_out
|
||||
fade_in = time_in_milliseconds(fromisoformat(item["fade_in"]))
|
||||
fade_out = time_in_milliseconds(fromisoformat(item["fade_out"]))
|
||||
|
||||
info = self.services.file_url(id=item['file_id'])
|
||||
current['metadata'] = info
|
||||
current['uri'] = item['file']
|
||||
current['filesize'] = info['filesize']
|
||||
elif item['stream']:
|
||||
current['independent_event'] = True
|
||||
current['id'] = item['stream_id']
|
||||
info = self.services.webstream_url(id=item['stream_id'])
|
||||
current['uri'] = info['url']
|
||||
current['type'] = 'stream_buffer_start'
|
||||
cue_in = time_in_seconds(fromisoformat(item["cue_in"]))
|
||||
cue_out = time_in_seconds(fromisoformat(item["cue_out"]))
|
||||
|
||||
current["fade_in"] = fade_in
|
||||
current["fade_out"] = fade_out
|
||||
current["cue_in"] = cue_in
|
||||
current["cue_out"] = cue_out
|
||||
|
||||
info = self.services.file_url(id=item["file_id"])
|
||||
current["metadata"] = info
|
||||
current["uri"] = item["file"]
|
||||
current["filesize"] = info["filesize"]
|
||||
elif item["stream"]:
|
||||
current["independent_event"] = True
|
||||
current["id"] = item["stream_id"]
|
||||
info = self.services.webstream_url(id=item["stream_id"])
|
||||
current["uri"] = info["url"]
|
||||
current["type"] = "stream_buffer_start"
|
||||
# Stream events are instantaneous
|
||||
current['end'] = current['start']
|
||||
current["end"] = current["start"]
|
||||
|
||||
result['{}_0'.format(key)] = {
|
||||
'id': current['id'],
|
||||
'type': 'stream_output_start',
|
||||
'start': current['start'],
|
||||
'end': current['start'],
|
||||
'uri': current['uri'],
|
||||
'row_id': current['row_id'],
|
||||
'independent_event': current['independent_event'],
|
||||
result["{}_0".format(key)] = {
|
||||
"id": current["id"],
|
||||
"type": "stream_output_start",
|
||||
"start": current["start"],
|
||||
"end": current["start"],
|
||||
"uri": current["uri"],
|
||||
"row_id": current["row_id"],
|
||||
"independent_event": current["independent_event"],
|
||||
}
|
||||
|
||||
result[end.isoformat()] = {
|
||||
'type': 'stream_buffer_end',
|
||||
'start': current['end'],
|
||||
'end': current['end'],
|
||||
'uri': current['uri'],
|
||||
'row_id': current['row_id'],
|
||||
'independent_event': current['independent_event'],
|
||||
"type": "stream_buffer_end",
|
||||
"start": current["end"],
|
||||
"end": current["end"],
|
||||
"uri": current["uri"],
|
||||
"row_id": current["row_id"],
|
||||
"independent_event": current["independent_event"],
|
||||
}
|
||||
|
||||
result['{}_0'.format(end.isoformat())] = {
|
||||
'type': 'stream_output_end',
|
||||
'start': current['end'],
|
||||
'end': current['end'],
|
||||
'uri': current['uri'],
|
||||
'row_id': current['row_id'],
|
||||
'independent_event': current['independent_event'],
|
||||
result["{}_0".format(end.isoformat())] = {
|
||||
"type": "stream_output_end",
|
||||
"start": current["end"],
|
||||
"end": current["end"],
|
||||
"uri": current["uri"],
|
||||
"row_id": current["row_id"],
|
||||
"independent_event": current["independent_event"],
|
||||
}
|
||||
return result
|
||||
|
|
|
@ -9,17 +9,19 @@ script_path = os.path.dirname(os.path.realpath(__file__))
|
|||
print(script_path)
|
||||
os.chdir(script_path)
|
||||
|
||||
setup(name='api_clients',
|
||||
version='2.0.0',
|
||||
description='LibreTime API Client',
|
||||
url='http://github.com/LibreTime/Libretime',
|
||||
author='LibreTime Contributors',
|
||||
license='AGPLv3',
|
||||
packages=['api_clients'],
|
||||
scripts=[],
|
||||
install_requires=[
|
||||
'configobj',
|
||||
'python-dateutil',
|
||||
],
|
||||
zip_safe=False,
|
||||
data_files=[])
|
||||
setup(
|
||||
name="api_clients",
|
||||
version="2.0.0",
|
||||
description="LibreTime API Client",
|
||||
url="http://github.com/LibreTime/Libretime",
|
||||
author="LibreTime Contributors",
|
||||
license="AGPLv3",
|
||||
packages=["api_clients"],
|
||||
scripts=[],
|
||||
install_requires=[
|
||||
"configobj",
|
||||
"python-dateutil",
|
||||
],
|
||||
zip_safe=False,
|
||||
data_files=[],
|
||||
)
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
import unittest
|
||||
from api_clients.utils import ApcUrl, UrlBadParam, IncompleteUrl
|
||||
|
||||
|
||||
class TestApcUrl(unittest.TestCase):
|
||||
def test_init(self):
|
||||
url = "/testing"
|
||||
|
@ -10,22 +11,23 @@ class TestApcUrl(unittest.TestCase):
|
|||
|
||||
def test_params_1(self):
|
||||
u = ApcUrl("/testing/{key}")
|
||||
self.assertEqual(u.params(key='val').url(), '/testing/val')
|
||||
self.assertEqual(u.params(key="val").url(), "/testing/val")
|
||||
|
||||
def test_params_2(self):
|
||||
u = ApcUrl('/testing/{key}/{api}/more_testing')
|
||||
full_url = u.params(key="AAA",api="BBB").url()
|
||||
self.assertEqual(full_url, '/testing/AAA/BBB/more_testing')
|
||||
u = ApcUrl("/testing/{key}/{api}/more_testing")
|
||||
full_url = u.params(key="AAA", api="BBB").url()
|
||||
self.assertEqual(full_url, "/testing/AAA/BBB/more_testing")
|
||||
|
||||
def test_params_ex(self):
|
||||
u = ApcUrl("/testing/{key}")
|
||||
with self.assertRaises(UrlBadParam):
|
||||
u.params(bad_key='testing')
|
||||
u.params(bad_key="testing")
|
||||
|
||||
def test_url(self):
|
||||
u = "one/two/three"
|
||||
self.assertEqual( ApcUrl(u).url(), u )
|
||||
self.assertEqual(ApcUrl(u).url(), u)
|
||||
|
||||
def test_url_ex(self):
|
||||
u = ApcUrl('/{one}/{two}/three').params(two='testing')
|
||||
with self.assertRaises(IncompleteUrl): u.url()
|
||||
u = ApcUrl("/{one}/{two}/three").params(two="testing")
|
||||
with self.assertRaises(IncompleteUrl):
|
||||
u.url()
|
||||
|
|
|
@ -4,39 +4,43 @@ import json
|
|||
from mock import MagicMock, patch
|
||||
from api_clients.utils import ApcUrl, ApiRequest
|
||||
|
||||
|
||||
class ResponseInfo:
|
||||
@property
|
||||
def headers(self):
|
||||
return {'content-type': 'application/json'}
|
||||
return {"content-type": "application/json"}
|
||||
|
||||
def json(self):
|
||||
return {'ok', 'ok'}
|
||||
return {"ok", "ok"}
|
||||
|
||||
|
||||
class TestApiRequest(unittest.TestCase):
|
||||
def test_init(self):
|
||||
u = ApiRequest('request_name', ApcUrl('/test/ing'))
|
||||
u = ApiRequest("request_name", ApcUrl("/test/ing"))
|
||||
self.assertEqual(u.name, "request_name")
|
||||
|
||||
def test_call_json(self):
|
||||
ret = {'ok':'ok'}
|
||||
ret = {"ok": "ok"}
|
||||
read = MagicMock()
|
||||
read.headers = {'content-type': 'application/json'}
|
||||
read.headers = {"content-type": "application/json"}
|
||||
read.json = MagicMock(return_value=ret)
|
||||
u = 'http://localhost/testing'
|
||||
with patch('requests.get') as mock_method:
|
||||
u = "http://localhost/testing"
|
||||
with patch("requests.get") as mock_method:
|
||||
mock_method.return_value = read
|
||||
request = ApiRequest('mm', ApcUrl(u))()
|
||||
request = ApiRequest("mm", ApcUrl(u))()
|
||||
self.assertEqual(request, ret)
|
||||
|
||||
def test_call_html(self):
|
||||
ret = '<html><head></head><body></body></html>'
|
||||
ret = "<html><head></head><body></body></html>"
|
||||
read = MagicMock()
|
||||
read.headers = {'content-type': 'application/html'}
|
||||
read.headers = {"content-type": "application/html"}
|
||||
read.text = MagicMock(return_value=ret)
|
||||
u = 'http://localhost/testing'
|
||||
with patch('requests.get') as mock_method:
|
||||
u = "http://localhost/testing"
|
||||
with patch("requests.get") as mock_method:
|
||||
mock_method.return_value = read
|
||||
request = ApiRequest('mm', ApcUrl(u))()
|
||||
request = ApiRequest("mm", ApcUrl(u))()
|
||||
self.assertEqual(request.text(), ret)
|
||||
|
||||
if __name__ == '__main__': unittest.main()
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
@ -6,18 +6,19 @@ from configobj import ConfigObj
|
|||
from api_clients.version1 import api_config
|
||||
from api_clients.utils import RequestProvider
|
||||
|
||||
|
||||
class TestRequestProvider(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.cfg = api_config
|
||||
self.cfg['general'] = {}
|
||||
self.cfg['general']['base_dir'] = '/test'
|
||||
self.cfg['general']['base_port'] = 80
|
||||
self.cfg['general']['base_url'] = 'localhost'
|
||||
self.cfg['general']['api_key'] = 'TEST_KEY'
|
||||
self.cfg['api_base'] = 'api'
|
||||
self.cfg["general"] = {}
|
||||
self.cfg["general"]["base_dir"] = "/test"
|
||||
self.cfg["general"]["base_port"] = 80
|
||||
self.cfg["general"]["base_url"] = "localhost"
|
||||
self.cfg["general"]["api_key"] = "TEST_KEY"
|
||||
self.cfg["api_base"] = "api"
|
||||
|
||||
def test_test(self):
|
||||
self.assertTrue('general' in self.cfg)
|
||||
self.assertTrue("general" in self.cfg)
|
||||
|
||||
def test_init(self):
|
||||
rp = RequestProvider(self.cfg, {})
|
||||
|
@ -25,12 +26,14 @@ class TestRequestProvider(unittest.TestCase):
|
|||
|
||||
def test_contains(self):
|
||||
methods = {
|
||||
'upload_recorded': '/1/',
|
||||
'update_media_url': '/2/',
|
||||
'list_all_db_files': '/3/',
|
||||
"upload_recorded": "/1/",
|
||||
"update_media_url": "/2/",
|
||||
"list_all_db_files": "/3/",
|
||||
}
|
||||
rp = RequestProvider(self.cfg, methods)
|
||||
for meth in methods:
|
||||
self.assertTrue(meth in rp.requests)
|
||||
|
||||
if __name__ == '__main__': unittest.main()
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
@ -4,13 +4,14 @@ import configparser
|
|||
import unittest
|
||||
from api_clients import utils
|
||||
|
||||
|
||||
def get_force_ssl(value, useConfigParser):
|
||||
config = {}
|
||||
if useConfigParser:
|
||||
config = configparser.ConfigParser()
|
||||
config['general'] = {
|
||||
'base_port': 80,
|
||||
'force_ssl': value,
|
||||
config["general"] = {
|
||||
"base_port": 80,
|
||||
"force_ssl": value,
|
||||
}
|
||||
return utils.get_protocol(config)
|
||||
|
||||
|
@ -27,65 +28,65 @@ class TestTime(unittest.TestCase):
|
|||
|
||||
class TestGetProtocol(unittest.TestCase):
|
||||
def test_dict_config_empty_http(self):
|
||||
config = {'general': {}}
|
||||
config = {"general": {}}
|
||||
protocol = utils.get_protocol(config)
|
||||
self.assertEqual(protocol, 'http')
|
||||
self.assertEqual(protocol, "http")
|
||||
|
||||
def test_dict_config_http(self):
|
||||
config = {
|
||||
'general': {
|
||||
'base_port': 80,
|
||||
"general": {
|
||||
"base_port": 80,
|
||||
},
|
||||
}
|
||||
protocol = utils.get_protocol(config)
|
||||
self.assertEqual(protocol, 'http')
|
||||
self.assertEqual(protocol, "http")
|
||||
|
||||
def test_dict_config_https(self):
|
||||
config = {
|
||||
'general': {
|
||||
'base_port': 443,
|
||||
"general": {
|
||||
"base_port": 443,
|
||||
},
|
||||
}
|
||||
protocol = utils.get_protocol(config)
|
||||
self.assertEqual(protocol, 'https')
|
||||
self.assertEqual(protocol, "https")
|
||||
|
||||
def test_dict_config_force_https(self):
|
||||
postive_values = ['yes', 'Yes', 'True', 'true', True]
|
||||
negative_values = ['no', 'No', 'False', 'false', False]
|
||||
postive_values = ["yes", "Yes", "True", "true", True]
|
||||
negative_values = ["no", "No", "False", "false", False]
|
||||
for value in postive_values:
|
||||
self.assertEqual(get_force_ssl(value, False), 'https')
|
||||
self.assertEqual(get_force_ssl(value, False), "https")
|
||||
for value in negative_values:
|
||||
self.assertEqual(get_force_ssl(value, False), 'http')
|
||||
self.assertEqual(get_force_ssl(value, False), "http")
|
||||
|
||||
def test_configparser_config_empty_http(self):
|
||||
config = configparser.ConfigParser()
|
||||
config['general'] = {}
|
||||
config["general"] = {}
|
||||
protocol = utils.get_protocol(config)
|
||||
self.assertEqual(protocol, 'http')
|
||||
self.assertEqual(protocol, "http")
|
||||
|
||||
def test_configparser_config_http(self):
|
||||
config = configparser.ConfigParser()
|
||||
config['general'] = {
|
||||
'base_port': 80,
|
||||
config["general"] = {
|
||||
"base_port": 80,
|
||||
}
|
||||
protocol = utils.get_protocol(config)
|
||||
self.assertEqual(protocol, 'http')
|
||||
self.assertEqual(protocol, "http")
|
||||
|
||||
def test_configparser_config_https(self):
|
||||
config = configparser.ConfigParser()
|
||||
config['general'] = {
|
||||
'base_port': 443,
|
||||
config["general"] = {
|
||||
"base_port": 443,
|
||||
}
|
||||
protocol = utils.get_protocol(config)
|
||||
self.assertEqual(protocol, 'https')
|
||||
self.assertEqual(protocol, "https")
|
||||
|
||||
def test_configparser_config_force_https(self):
|
||||
postive_values = ['yes', 'Yes', 'True', 'true', True]
|
||||
negative_values = ['no', 'No', 'False', 'false', False]
|
||||
postive_values = ["yes", "Yes", "True", "true", True]
|
||||
negative_values = ["no", "No", "False", "false", False]
|
||||
for value in postive_values:
|
||||
self.assertEqual(get_force_ssl(value, True), 'https')
|
||||
self.assertEqual(get_force_ssl(value, True), "https")
|
||||
for value in negative_values:
|
||||
self.assertEqual(get_force_ssl(value, True), 'http')
|
||||
self.assertEqual(get_force_ssl(value, True), "http")
|
||||
|
||||
def test_fromisoformat(self):
|
||||
time = {
|
||||
|
@ -96,4 +97,6 @@ class TestGetProtocol(unittest.TestCase):
|
|||
result = utils.fromisoformat(time_string)
|
||||
self.assertEqual(result, expected)
|
||||
|
||||
if __name__ == '__main__': unittest.main()
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue