Beispiel #1
0
    def query(self, product_type=None, auth=None, count=True, **kwargs):
        """Perform a search on a OGC/CSW-like interface

        .. versionchanged::
            1.0

                * ``product_type`` is no longer mandatory
        """
        product_type = kwargs.get("productType")
        if product_type is None:
            return [], 0
        if auth is not None:
            self.__init_catalog(**getattr(auth.config, "credentials", {}))
        else:
            self.__init_catalog()
        results = []
        if self.catalog:
            provider_product_type = self.config.products[product_type][
                "productType"]
            for product_type_def in self.config.search_definition[
                    "product_type_tags"]:
                product_type_search_tag = product_type_def["name"]
                logger.debug(
                    "Querying <%s> tag for product type %s",
                    product_type_search_tag,
                    provider_product_type,
                )
                constraints = self.__convert_query_params(
                    product_type_def, provider_product_type, kwargs)
                with patch_owslib_requests(verify=True):
                    try:
                        self.catalog.getrecords2(constraints=constraints,
                                                 esn="full",
                                                 maxrecords=10)
                    except ExceptionReport:
                        import traceback as tb

                        logger.warning(
                            "Failed to query %s for product type %s : %s",
                            product_type_search_tag,
                            product_type,
                            tb.format_exc(),
                        )
                        continue
                partial_results = [
                    self.__build_product(record, product_type, **kwargs)
                    for record in self.catalog.records.values()
                ]
                logger.info(
                    "Found %s results querying %s",
                    len(partial_results),
                    product_type_search_tag,
                )
                results.extend(partial_results)
        logger.info("Found %s overall results", len(results))
        total_results = len(results) if count else None
        return results, total_results
Beispiel #2
0
 def __init_catalog(self, username=None, password=None):
     """Initializes a catalogue by performing a GetCapabilities request on the url"""
     if not self.catalog:
         api_endpoint = self.config.api_endpoint
         version = getattr(self.config, "version", "2.0.2")
         logger.debug("Initialising CSW catalog at %s", api_endpoint)
         with patch_owslib_requests(verify=True):
             try:
                 self.catalog = CatalogueServiceWeb(
                     api_endpoint,
                     version=version,
                     username=username,
                     password=password,
                 )
             except Exception as e:
                 logger.warning(
                     "Initialization of catalog failed due to error: (%s: %s)",
                     type(e),
                     e,
                 )