Exemplo n.º 1
0
    def _open_url(self,
                  url_base,
                  data=None,
                  method='Get',
                  cookies=None,
                  username=None,
                  password=None,
                  timeout=30,
                  headers=None):
        headers = headers if headers is not None else {}
        kwargs = dict(timeout=timeout)

        kwargs['auth'] = self.auth

        method = method.split("}")[-1]

        if method.lower() == 'post':
            try:
                _ = etree.fromstring(data)
                headers['Content-Type'] = 'text/xml'
            except (ParseError, UnicodeEncodeError):
                pass

            kwargs['data'] = data

        elif method.lower() == 'get':
            kwargs['params'] = data

        else:
            raise ValueError(
                "Unknown method ('%s'), expected 'get' or 'post'" % method)

        if cookies is not None:
            kwargs['cookies'] = cookies

        req = requests.request(method.upper(),
                               url_base,
                               headers=headers,
                               **kwargs)

        if req.status_code in [400, 401]:
            raise ServiceException(req.text)

        if req.status_code in [404]:  # add more if needed
            req.raise_for_status()

        if 'Content-Type' in req.headers and req.headers['Content-Type'] in [
                'text/xml', 'application/xml'
        ]:
            se_tree = etree.fromstring(req.content)
            serviceException = se_tree.find(
                '{http://www.opengis.net/ows}Exception')
            if serviceException is None:
                serviceException = se_tree.find('ServiceException')
            if serviceException is not None:
                raise ServiceException(str(serviceException.text).strip())

        return ResponseWrapper(req)
Exemplo n.º 2
0
def _find_entries(response_wrapper: ResponseWrapper):
    """
    Find all atom:entry elements in a ResponseWrapper instance

    :param response_wrapper: the ResponseWrapper instance
    :return: a list of etree.Element whose tags are atom:entry
    """
    root_node = etree.fromstring(response_wrapper.read())
    return root_node.findall('atom:entry', namespaces=MUNDI_NAMESPACES)
Exemplo n.º 3
0
    def _openURL(*args, **kwargs):
        """Patch function for owslib.util.openURL using our custom pydov
        requests session.

        Parameters
        ----------
        url : str
            URL to open.

        Returns
        -------
        ResponseWrapper
            Wrapped response of the request.
        """
        url = args[0]
        return ResponseWrapper(pydov.session.get(url))
Exemplo n.º 4
0
def openURL(url_base,
            data=None,
            method='Get',
            cookies=None,
            username=None,
            password=None,
            timeout=config_loader(dataset="WMS_request_timeout"),
            headers=None,
            proxies=None):
    # (mss) added proxies
    # (mss) timeout default of 30secs set by the config_loader
    """
    Function to open URLs.

    Uses requests library but with additional checks for OGC service exceptions and url formatting.
    Also handles cookies and simple user password authentication.
    """
    headers = headers if headers is not None else {}
    rkwargs = {}

    rkwargs['timeout'] = timeout

    auth = None
    if username and password:
        auth = (username, password)

    rkwargs['auth'] = auth

    # FIXUP for WFS in particular, remove xml style namespace
    # @TODO does this belong here?
    method = method.split("}")[-1]

    if method.lower() == 'post':
        try:
            etree.fromstring(data)
            headers['Content-Type'] = 'text/xml'
        except (ParseError, UnicodeEncodeError) as error:
            # (mss)
            logging.debug("ParseError, UnicodeEncodeError %s", error)

        rkwargs['data'] = data

    elif method.lower() == 'get':
        rkwargs['params'] = data

    else:
        raise ValueError(
            f"Unknown method ('{method}'), expected 'get' or 'post'")

    if cookies is not None:
        rkwargs['cookies'] = cookies

    req = requests.request(
        method.upper(),
        url_base,
        headers=headers,
        # MSS
        proxies=proxies,
        **rkwargs)

    if req.status_code in [400, 401]:
        raise ServiceException(req.text)

    if req.status_code in [404, 500, 502, 503, 504]:  # add more if needed
        req.raise_for_status()

    # check for service exceptions without the http header set
    if 'Content-Type' in req.headers and req.headers['Content-Type'] in [
            'text/xml', 'application/xml', 'application/vnd.ogc.se_xml',
            'application/vnd.ogc.wms_xml'
    ]:
        # just in case 400 headers were not set, going to have to read the xml to see if it's an exception report.
        se_tree = etree.fromstring(req.content)

        # to handle the variety of namespaces and terms across services
        # and versions, especially for "legacy" responses like WMS 1.3.0
        possible_errors = [
            '{http://www.opengis.net/ows}Exception',
            '{http://www.opengis.net/ows/1.1}Exception',
            '{http://www.opengis.net/ogc}ServiceException', 'ServiceException'
        ]

        for possible_error in possible_errors:
            serviceException = se_tree.find(possible_error)
            if serviceException is not None:
                # and we need to deal with some message nesting
                raise ServiceException('\n'.join([
                    str(t).strip() for t in serviceException.itertext()
                    if str(t).strip()
                ]))

    return ResponseWrapper(req)
Exemplo n.º 5
0
def _findentries(ResponseWrapper):
    root_node = lxml.etree.fromstring(ResponseWrapper.read())
    entries = root_node.findall("atom:entry", namespaces=mundi_nsmap)
    return entries