def __call__(self, query=None, filters=None, **kwargs): """ :param filters: filters to apply to search :param query: searched string :return: a list of mixed items (individuals, ontologies, terms, properties) """ if filters is None: filters = {} if query is None: raise exceptions.BadParameter({ 'error': "Bad Request", 'message': 'Missing query', 'status': 400, 'path': 'search', 'timestamp': time.time() }) self.query = query call_filters = filters or { key: value for key, value in kwargs.items() } or {} obj = super().__call__(call_filters) obj.query = query return obj
def __init__(self, uri, elem_class, document=None, page_size=500, filters=None, index=0): """ Initialize a list object :param uri: the OLS api base source uri :param elem_class: the expected class items objects :param: coreapi.Document from api (used to avoid double call to api if already loade elsewhere """ if filters is None: filters = {} self.current_filters = filters self.page_size = page_size super().__init__(document.url if document is not None else uri, elem_class) try: if document is not None: assert (isinstance(document, coreapi.document.Document)) self.document = document else: self.document = self._parse_response( self.client.get(uri, force_codec=True)) logger.debug('ListClientMixin init[%s][%s][%s]', self.elem_class, self.document.url, self.page_size) except coreapi.exceptions.ErrorMessage as e: if 'status' in e.error: if e.error['status'] == 404: raise exceptions.NotFoundException(e.error) elif 400 < e.error['status'] < 499: raise exceptions.BadParameter(e.error) elif e.error['status'] >= 500: raise exceptions.ServerError(e.error) raise exceptions.OlsException(e.error) except coreapi.exceptions.CoreAPIException as e: raise e self.index = index
def fetch_page(self, page): """ Fetch document page from paginated results :param page: expected page :return Document: fetched page document fro api """ uri = '/'.join([self.uri, self.path]) + '?page={}&size={}'.format( page, self.page_size) logger.debug('Fetch page "%s"', uri) try: return self._parse_response(self.client.get(uri, force_codec=True)) except coreapi.exceptions.ErrorMessage as e: if 'status' in e.error: if e.error['status'] == 404: raise exceptions.NotFoundException(e.error) elif 400 < e.error['status'] < 499: raise exceptions.BadParameter(e.error) elif e.error['status'] >= 500: raise exceptions.ServerError(e.error) raise exceptions.OlsException(e.error) except coreapi.exceptions.CoreAPIException as e: raise e
def __call__(self, identifier, silent=True, unique=True): """ Check one element from OLS API according to specified identifier In cas API returns multiple element return either: - the one which is defining_ontology (flag True) - The first one if none (Should not happen) """ iri = self.make_uri(identifier) path = "/".join([self.uri, iri]) logger_id = '[identifier:{}, path:{}]'.format(iri, path) logger.debug('Detail client %s [silent:%s, unique:%s]', logger_id, silent, unique) try: document = self._parse_response( self.client.get(path, force_codec=True), iri) if self.elem_class.path in document.data: # the request returned a list of object if not silent: logger.warning('OLS returned multiple {}s for {}'.format( self.elem_class.__name__, logger_id)) # return a list instead elms = ListClientMixin(self.uri, self.elem_class, document, 100) if not unique: return elms else: return next((x for x in elms if x.is_defining_ontology), elms[0]) return self.elem_class_instance(**document.data) except coreapi.exceptions.ErrorMessage as e: if 'status' in e.error: if e.error['status'] == 404: raise exceptions.NotFoundException(e.error) elif 400 < e.error['status'] < 499: raise exceptions.BadParameter(e.error) elif e.error['status'] >= 500: raise exceptions.ServerError(e.error) raise exceptions.OlsException(e.error) except coreapi.exceptions.CoreAPIException as e: raise e
def __call__(self, filters=None, action=None): """ Allow to search for a list of helpers, retrieve self, wich is now a iterator on the actual list of related helpers """ if filters is None: filters = {} page_size = self.page_size if filters: try: check_fn = getattr(self, 'filters_' + self.path, None) if callable(check_fn): filters = check_fn(filters) except AssertionError as e: raise exceptions.BadFilters(str(e)) page_size = filters.get('size', self.page_size) params = {'page': 0, 'size': page_size} params.update(filters) path = action if action else self.path try: document = self.fetch_document(path, params, filters) except coreapi.exceptions.ErrorMessage as e: if 'status' in e.error: if e.error['status'] == 404: raise exceptions.NotFoundException(e.error) elif 400 < e.error['status'] < 499: raise exceptions.BadParameter(e.error) elif e.error['status'] >= 500: raise exceptions.ServerError(e.error) raise exceptions.OlsException(e.error) except coreapi.exceptions.CoreAPIException as e: raise e obj = self.__class__(path, self.elem_class, document, page_size, filters) obj.uri = urllib.parse.urljoin( obj.uri, os.path.dirname(urllib.parse.urlparse(obj.uri).path)) return obj