def __process_response_update_alarms_action(self, response: Response) -> bool: """ Procesa las respuestas de la acción alarm-update que permite actualizar las alarmas :param response: Respuesta obtenida del servidor después de consultar las alarmas :type response: Response :return: La cantidad de alarmas encontradas, si no existen, se retorna None :rtype: dict """ updated: bool = False if response is None: self.debug("La respuesta de la API es nula") return updated # Si la respuesta es válida, retorno el resultado en formato JSON. # Sino, imprimo el error y retorno un None if response.status_code == 200: response_json: dict = convert_bytes_to_json(response.content) data: dict = response_json.get('alarm-update-response-list') response: dict = data.get('alarm-responses').get('alarm') updated = response.get('@error') == 'Success' if not updated: self.error(f'Se presentó un error realizando la actualización') self.debug(response) return updated self.error(f'Consultando la API de SPECTRUM: {response.status_code}') return updated
def _process_api_response(self, response: Response, commands: List[str], raw_text: bool = False) -> List[Any]: """ Normalize the API response including handling errors; adding the sent command into the returned data strucutre; make response structure consistent for raw_text and structured data. """ response_list = json.loads(response.text) if isinstance(response_list, dict): response_list = [response_list] # Add the 'command' that was executed to the response dictionary for i, response_dict in enumerate(response_list): response_dict["command"] = commands[i] new_response = [] for response in response_list: # Detect errors self._error_check(response) # Some commands like "show run" can have a None result cmd_response = response.get("result") if cmd_response is None: cmd_response = {} # Normalize the response data structure response_dict = {"command": response["command"]} if response and raw_text: response_dict["result"] = cmd_response.get("msg") elif response and not raw_text: response_dict["result"] = cmd_response.get("body") else: raise NXAPIError( "Unexpected value encountered processing response.") new_response.append(response_dict) return new_response
def format_response(self, response: requests.Response, query: dict): """Return the formatted response as defined in wrapper/output_format.py. Args: response: The requests response returned by `call_api`. query: The query dict used as defined in wrapper/input_format.py. Returns: The formatted response. """ if self.result_format == "json" or self.result_format == "jsonld": # Load into dict response = response.json() # Modify response to fit the defined wrapper output format response["dbQuery"] = response.get("query", {}) response["query"] = query if ("result" in response) and (len(response["result"]) > 0): response["result"] = response.pop("result")[0] else: response["result"] = { "total": -1, "start": -1, "pageLength": -1, "recordsDisplayed": len(response.get("records", [])), } for record in response.get("records") or []: if ("url" in record) and (len(record["url"]) > 0) and ("value" in record["url"][0]): record["uri"] = record["url"][0]["value"] authors = [] for author in record.get("creators") or []: authors.append(author["creator"]) record["authors"] = authors record["pages"] = { "first": record.get("startingPage"), "last": record.get("endingPage"), } if self.collection == "openaccess": record["openAccess"] = True elif "openaccess" in record: record["openAccess"] = (record.pop("openaccess") == "true") # Delete all undefined fields utils.clean_output(record, OUTPUT_FORMAT["records"][0]) ''' Convert from springers format to a more usable format. "facets": [{ "name": "Name of the category", "values": [{ "value": "Value in the category", "count": "Number of occurrences of this value", }], }], to: "facets": { "category": { "name": "int: counter", }, }, See wrapper/output_format.py. ''' new_facets = { "countries": {}, "keywords": [], } for facet in response.get("facets") or []: facet_name = facet.get("name") if not facet_name: continue for value in facet["values"]: val_name = value.get("value") if not val_name: continue if facet_name == "country": # Convert to ISO 3166-1 alpha-2 codes try: iso = utils.get( pycountry.countries.search_fuzzy(val_name), 0) except LookupError: iso = None # If no match was found the full name is readd. val_name = iso.alpha_2 if iso else val_name new_facets["countries"][val_name] = int( value.get("count", 0)) # elif facet_name in ["keyword", "subject"]: elif facet_name == "keyword": keyword = { "text": val_name.lower(), "value": int(value.get("count", 0)), } new_facets["keywords"].append(keyword) response["facets"] = new_facets # Delete all undefined fields utils.clean_output(response) return response else: print( f"No formatter defined for {self.result_format}. Returning raw response." ) return response.text
def format_response(self, response: requests.Response, query: dict, db_query: Union[dict, str]): """Return the formatted response as defined in wrapper/output_format.py. Args: response: The requests response returned by `call_api`. query: The query dict used as defined in wrapper/input_format.py. body: The HTTP body of the query. Returns: The formatted response. """ if self.result_format == "application/json": # Load into dict response = response.json() if self.collection == "search/sciencedirect": # Modify response to fit the defined wrapper output format response["query"] = query response["dbQuery"] = db_query response["apiKey"] = self.api_key response["result"] = { "total": response.get("resultsFound", -1), "start": self.__start_record + 1, "pageLength": self.show_num, "recordsDisplayed": len(response.get("results", [])) } response["records"] = response.pop( "results") if "results" in response else [] for record in response.get("records") or []: authors = [] for author in record.get("authors") or []: authors.append(author["name"]) record["authors"] = authors if "sourceTitle" in record: record["publicationName"] = record.pop("sourceTitle") record["publisher"] = "ScienceDirect" # Delete all undefined fields utils.clean_output(record, OUTPUT_FORMAT["records"][0]) elif self.collection == "metadata/article": # TODO! raise NotImplementedError( "No formatter defined for the metadata collection yet.") elif self.collection == "search/scopus": response = response.get("search-results") if not response: # We need to only fill the error message. The rest is filled like normal and the # records loop will not be executed. response = utils.invalid_output( *[None] * 3, "Scopus returned unknown format.", None, None) response["query"] = query response["dbQuery"] = utils.get( response, "opensearch:Query", "@searchTerms", default=db_query, ) response["apiKey"] = self.api_key response["records"] = response.pop( "entry") if "entry" in response else [] # Elsevier returns an object with an error field stating that the results are empty. if len(response["records"] ) == 1 and "error" in response["records"][0]: response["records"] = [] response["result"] = { "total": response.get("opensearch:totalResults", -1), "start": self.__start_record + 1, "pageLength": self.show_num, "recordsDisplayed": len(response.get("records", [])), } countries = {} all_titles = "" for record in response.get("records"): record["contentType"] = record.get("subtypeDescription") record["title"] = record.get("dc:title") all_titles += record["title"] + " " if record[ "title"] else "" record["authors"] = [record.get("dc:creator")] record["publicationName"] = record.get( "prism:publicationName") record["openAccess"] = record.get("openaccess") record["doi"] = record.get("prism:doi") record["publisher"] = "Elsevier" record["publicationDate"] = record.get("prism:coverDate") record["publicationType"] = record.get( "prism:aggregationType") record["issn"] = record.get("prism:issn") record["volume"] = record.get("prism:volume") page_range = record.get("prism:pageRange") page_range = page_range.split("-") if page_range else [] record["pages"] = { "first": page_range[0] if len(page_range) > 0 else None, "last": page_range[1] if len(page_range) > 1 else None, } for link_dict in record.get("link") or []: if link_dict.get("@ref") == "scopus": record["uri"] = link_dict.get("@href") break # Extract countries and their counter country = utils.get(record, "affiliation", 0, "affiliation-country") if country: # Convert to ISO 3166-1 alpha-2 codes try: iso = utils.get( pycountry.countries.search_fuzzy(country), 0) except LookupError: iso = None # If no match was found the full name is readd. country = iso.alpha_2 if iso else country if country in countries: countries[country] += 1 else: countries[country] = 1 # Delete all undefined fields utils.clean_output(record, OUTPUT_FORMAT["records"][0]) keywords = utils.titles_to_keywords(all_titles) response["facets"] = { "countries": countries, "keywords": keywords, } # Delete all undefined fields utils.clean_output(response) return response else: print( f"No formatter defined for {self.result_format}. Returning response body." ) return response.text