Example #1
0
 def get(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     try:
         metric_name = api_request.query_params["name"]
         metric_definition = self.metric_repo.get_metric_by_name(
             metric_name)
         if metric_definition:
             api_response = ApiResponse(
                 status_code=HttpStatusCode.ok,
                 payload=metric_definition.to_serializable())
         else:
             api_response = ApiResponse(
                 status_code=HttpStatusCode.not_found,
                 payload={
                     "Could not find metric definition named: {}".format(
                         metric_name)
                 })
     except KeyError:
         api_response = ApiResponse(
             HttpStatusCode.bad_request, {
                 "message":
                 "Could not find metric definition name in URL: {}".format(
                     api_request.url)
             })
     log_api_call(api_request, api_response)
     return build_response(api_response)
Example #2
0
 def get(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     audio_file_name = api_request.query_params.get("file_name")
     if audio_file_name is not None:
         audio_file_absolute_path = concatenate_paths(
             AUDIO_FILES_DIR, audio_file_name)
         if file_exists(audio_file_absolute_path):
             id3_tag = read_audio_tag(audio_file_absolute_path)
             if id3_tag:
                 api_response = ApiResponse(HttpStatusCode.ok,
                                            id3_tag.to_serializable())
             else:
                 api_response = ApiResponse(HttpStatusCode.no_content,
                                            payload=None)
         else:
             api_response = ApiResponse(HttpStatusCode.not_found, {
                 "message":
                 "File does not exist: {}".format(audio_file_name)
             })
     else:
         api_response = ApiResponse(
             HttpStatusCode.bad_request,
             {"message": "Parameter file_name was not provided"})
     log_api_call(api_request, api_response)
     return build_response(api_response)
Example #3
0
 def get(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     definition_name = api_request.query_params.get("name")
     task_id = api_request.query_params.get("task_id")
     if definition_name is None and task_id is None:
         message = "Could not find either metric definition name or task_id in URL: {}".format(
             api_request.url)
         api_response = ApiResponse(HttpStatusCode.bad_request,
                                    {"message": message})
     elif definition_name is not None:
         values = self.metric_repo.get_values_by_name(
             metric_name=definition_name)
         if values is None:
             api_response = ApiResponse(
                 HttpStatusCode.not_found, {
                     "message":
                     "Could not find metric definition named {}".format(
                         definition_name)
                 })
         else:
             api_response = ApiResponse(HttpStatusCode.ok,
                                        [v.task_id for v in values])
     elif task_id is not None:
         values = self.metric_repo.get_by_task_id(task_id=task_id)
         api_response = ApiResponse(HttpStatusCode.ok,
                                    [v.definition.name for v in values])
     else:
         api_response = ApiResponse(
             HttpStatusCode.bad_request, {
                 "message":
                 "Listing API does not support querying by task_id and metric name at the same time"
             })
     log_api_call(api_request, api_response)
     return build_response(api_response)
Example #4
0
 def get(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     try:
         metric_def_name = api_request.query_params["name"]
         task_id = api_request.query_params["task_id"]
         metric_definition = self.metric_repo.get_by_name_and_task_id(
             metric_name=metric_def_name, task_id=task_id)
         if metric_definition is not None:
             api_response = ApiResponse(
                 status_code=HttpStatusCode.ok,
                 payload=metric_definition.to_serializable())
         else:
             message = "Could not find metric value for definition named {} with task_id {}".format(
                 metric_def_name, task_id)
             api_response = ApiResponse(
                 status_code=HttpStatusCode.not_found,
                 payload={"message": message})
     except KeyError:
         api_response = ApiResponse(
             HttpStatusCode.bad_request, {
                 "message":
                 "Could not find metric definition name or task_id in URL: {}"
                 .format(api_request.url)
             })
     log_api_call(api_request, api_response)
     return build_response(api_response)
Example #5
0
 def get(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     try:
         plugin_vendor = api_request.query_params["vendor"]
         plugin_name = api_request.query_params["name"]
         plugin_output = api_request.query_params["output"]
         vampy_plugin = self.plugin_provider.build_plugin_from_params(
             plugin_vendor, plugin_name, plugin_output)
         if vampy_plugin is not None:
             api_response = ApiResponse(
                 status_code=HttpStatusCode.ok,
                 payload=vampy_plugin.to_serializable())
         else:
             message = "Could not find VAMP plugin with key: {}:{}:{}".format(
                 plugin_vendor, plugin_name, plugin_output)
             api_response = ApiResponse(
                 status_code=HttpStatusCode.not_found,
                 payload={"message": message})
     except KeyError:
         api_response = ApiResponse(
             HttpStatusCode.bad_request, {
                 "message":
                 "Could not find vendor, name or output in request URL: {}".
                 format(api_request.url)
             })
     log_api_call(api_request, api_response)
     return build_response(api_response)
Example #6
0
 def get(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     try:
         task_id = api_request.query_params["task_id"]
         data_model = self.request_repo.get_by_id(task_id)
         if data_model is not None:
             api_response = ApiResponse(HttpStatusCode.ok,
                                        data_model.to_serializable())
         else:
             api_response = ApiResponse(
                 HttpStatusCode.not_found,
                 payload={
                     "message":
                     "Could not find result with id: {}".format(task_id)
                 })
     except KeyError:
         api_response = ApiResponse(
             HttpStatusCode.bad_request,
             payload={
                 "message":
                 "Could not find task_id parameter in URL: {}".format(
                     api_request.url)
             })
     log_api_call(api_request, api_response)
     return build_response(api_response)
Example #7
0
 def get(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     try:
         api_response = ApiResponse(status_code=HttpStatusCode.ok, payload=self.plugin_config_provider.get_plugin_names())
     except Exception:
         api_response = ApiResponse(HttpStatusCode.not_found,
                                    {"message": "Could not find config file: {}".format(PLUGIN_CONFIG_FILE_NAME)})
     log_api_call(api_request, api_response)
     return build_response(api_response)
Example #8
0
 def get(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     try:
         metric_name = api_request.query_params["name"]
         api_response = ApiResponse(status_code=HttpStatusCode.ok,
                                    payload=self.metric_config_provider.get_by_name(metric_name))
     except KeyError:
         api_response = ApiResponse(HttpStatusCode.bad_request,
                                    payload={"message": "Could not find metric name parameter in URL: {}".format(
                                        api_request.url)})
     except Exception:
         api_response = ApiResponse(HttpStatusCode.not_found,
                                    {"message": "Could not find config file: {}".format(PLUGIN_CONFIG_FILE_NAME)})
     log_api_call(api_request, api_response)
     return build_response(api_response)
Example #9
0
 def get(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     api_response = ApiResponse(
         status_code=HttpStatusCode.ok,
         payload=self.plugin_provider.list_full_plugin_keys())
     log_api_call(api_request, api_response)
     return build_response(api_response)
Example #10
0
 def get(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     api_response = ApiResponse(
         status_code=HttpStatusCode.ok,
         payload={"status": COORDINATOR_STATUS_RESPONSE})
     log_api_call(api_request, api_response)
     return build_response(api_response)
Example #11
0
 def get(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     file_name = api_request.query_params.get("file_name")
     if file_name is None:
         api_response = ApiResponse(
             HttpStatusCode.bad_request,
             {"message": "Parameter file_name was not provided"})
     elif self.file_store.exists(file_name):
         api_response = ApiResponse(
             HttpStatusCode.ok,
             self.file_store.meta(file_name).to_serializable())
     else:
         api_response = ApiResponse(
             HttpStatusCode.not_found,
             {"message": "Can't find file with name: {}".format(file_name)})
     log_api_call(api_request, api_response)
     return build_response(api_response)
Example #12
0
 def get(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     try:
         task_id = api_request.query_params["task_id"]
         task_result = retrieve_result(task_id)
         if task_result.status == TaskStatus.not_known:
             api_response = ApiResponse(HttpStatusCode.not_found,
                                        task_result.to_serializable())
         else:
             api_response = ApiResponse(HttpStatusCode.ok,
                                        task_result.to_serializable())
     except KeyError:
         api_response = ApiResponse(
             HttpStatusCode.bad_request,
             {"message": "Parameter task_id was not provided in URL"})
     log_api_call(api_request, api_response)
     return build_response(api_response)
Example #13
0
 def get(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     all_definitions = self.metric_repo.get_all(
     )  # type: List[MetricDefinition]
     api_response = ApiResponse(HttpStatusCode.ok,
                                [d.name for d in all_definitions])
     log_api_call(api_request, api_response)
     return build_response(api_response)
Example #14
0
 def get(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     all_results = self.request_repo.get_all(
     )  # type: List[AnalysisRequest]
     api_response = ApiResponse(HttpStatusCode.ok,
                                [r.to_serializable() for r in all_results])
     log_api_call(api_request, api_response)
     return build_response(api_response)
Example #15
0
def log_api_call(api_request: ApiRequest, api_response: ApiResponse) -> None:
    serving_time = seconds_between(api_request.creation_time)
    logger.info("Served {} @ {} with {} ({} -> {}) in {}s.".format(api_request.method,
                                                                   api_request.url,
                                                                   api_response.status_code,
                                                                   api_request.size_humanized(),
                                                                   api_response.size_humanized(),
                                                                   serving_time))
Example #16
0
def prepare_method_not_supported_response(method: HttpMethod) -> str:
    payload = {
        "error":
        "URL {} does not support {} method".format(request.full_path,
                                                   method.value)
    }
    return build_response(
        ApiResponse(HttpStatusCode.method_not_allowed, payload))
Example #17
0
 def get(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     try:
         vendor = api_request.query_params["vendor"]
         name = api_request.query_params["name"]
         output = api_request.query_params["output"]
         plugin_key = "{}:{}:{}".format(vendor, name, output)
         api_response = ApiResponse(status_code=HttpStatusCode.ok,
                                    payload=self.metric_config_provider.get_metric_names_for_plugin(plugin_key))
     except KeyError:
         message = "Could not find metric plugin parameters in URL: {}".format(api_request.url)
         api_response = ApiResponse(HttpStatusCode.bad_request, payload={"message": message})
     except Exception:
         api_response = ApiResponse(HttpStatusCode.not_found,
                                    {"message": "Could not find config file: {}".format(PLUGIN_CONFIG_FILE_NAME)})
     log_api_call(api_request, api_response)
     return build_response(api_response)
Example #18
0
 def post(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     execution_request = self._parse_request(api_request)
     task_id = execution_request.task_id
     task_result = retrieve_result(task_id)
     if task_result.status in [
             TaskStatus.ignored, TaskStatus.in_progress, TaskStatus.done
     ]:
         message = "Could not send task #{}: is already in state {}".format(
             task_id, task_result.status)
         api_response = ApiResponse(HttpStatusCode.precondition_failed,
                                    {"message": message})
     else:
         async_result = run_task(
             task=extract_feature,
             task_id=task_id,
             extraction_request=execution_request.to_serializable())
         api_response = ApiResponse(HttpStatusCode.accepted,
                                    {"task_id": async_result.task_id})
     log_api_call(api_request, api_response)
     return build_response(api_response)
Example #19
0
    def post(self, **kwargs) -> str:
        api_request = build_request(request, **kwargs)
        audio_file_names = self.audio_file_store.list()
        plugins = self.plugin_provider.list_vampy_plugins()
        plugin_configs = self.plugin_config_provider.get_all() or {}

        if audio_file_names and plugins:
            extraction_requests = self._generate_extraction_requests(
                audio_file_names, plugins, plugin_configs)
            for task_request in extraction_requests:
                if not self.result_repo.exists_by_id(task_request.task_id):
                    run_task(task=extract_feature,
                             task_id=task_request.task_id,
                             extraction_request=task_request.to_serializable())
            api_response = ApiResponse(
                HttpStatusCode.accepted,
                [er.to_serializable() for er in extraction_requests])
        else:
            api_response = ApiResponse(
                status_code=HttpStatusCode.precondition_failed, payload=None)
        log_api_call(api_request, api_response)
        return build_response(api_response)
Example #20
0
 def delete(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     try:
         task_id = api_request.query_params["task_id"]
         self.feature_meta_repo.delete_by_id(task_id)
         api_response = ApiResponse(HttpStatusCode.ok, None)
     except KeyError:
         api_response = ApiResponse(
             HttpStatusCode.bad_request,
             payload={
                 "message":
                 "Could not find task_id parameter in URL: {}".format(
                     api_request.url)
             })
     except EntityNotFound:
         api_response = ApiResponse(
             HttpStatusCode.not_found,
             payload={
                 "message":
                 "Could not find result with id: {}".format(task_id)
             })
     log_api_call(api_request, api_response)
     return build_response(api_response)
Example #21
0
    def delete(self, **kwargs) -> str:
        api_request = build_request(request, **kwargs)
        try:
            task_id = api_request.query_params["task_id"]
            task_result = retrieve_result(task_id)
            if task_result.status in [TaskStatus.done, TaskStatus.failed]:
                self.request_repo.delete_by_id(task_id)
                delete_result(task_id)
                api_response = ApiResponse(HttpStatusCode.ok,
                                           {"task_id": task_id})
            else:
                api_response = ApiResponse(
                    HttpStatusCode.precondition_failed,
                    payload={
                        "message":
                        "Can not remove request that has not finished; status: {}"
                        .format(task_result.status.name)
                    })
        except KeyError:
            api_response = ApiResponse(
                HttpStatusCode.bad_request,
                payload={
                    "message":
                    "Could not find task_id parameter in URL: {}".format(
                        api_request.url)
                })
        except EntityNotFound:
            api_response = ApiResponse(
                HttpStatusCode.not_found,
                payload={
                    "message":
                    "Could not find result with id: {}".format(task_id)
                })

        log_api_call(api_request, api_response)
        return build_response(api_response)
Example #22
0
 def get(self, **kwargs) -> str:
     api_request = build_request(request, **kwargs)
     api_response = ApiResponse(HttpStatusCode.ok, self.file_store.list())
     log_api_call(api_request, api_response)
     return build_response(api_response)