예제 #1
0
    def post(self,
             url,
             params: dict = None,
             data: bytes = b'',
             timeout: int = 20000,
             content_type: str = None,
             **kwargs) -> Reply:
        '''
        posts data to given url (POST)

        asynchronous posts are not implemented yet

        Parameters
        ----------
        url : str
            the url to post to
        params : dict, optional
            query parameters with the parameters as keys and the values as
            values, defaults to no query parameters
        data : bytes, optional
            the data to post as a byte-string, defaults to no data posted
        timeout : int, optional
            the timeout of synchronous requests in milliseconds, will be ignored
            when making asynchronous requests, defaults to 20000 ms
        content_type : str, optional
            the content type of the data, puts content type into header of
            request
        **kwargs :
            additional parameters matching the requests-interface will
            be ignored (e.g. verify is not supported)

        Returns
        ----------
        Reply
           the response is returned in case of synchronous calls, if you are
           using asynchronous calls retrieve the response via the finished-
           signal instead
        '''
        qurl = QUrl(url)

        if params:
            query = QUrlQuery()
            for param, value in params.items():
                query.addQueryItem(param, str(value))
            qurl.setQuery(query.query())

        if self.synchronous:
            return self._post_sync(qurl,
                                   timeout=timeout,
                                   data=data,
                                   content_type=content_type)

        return self._post_async(qurl, content_type=content_type)
    def url(self):
        query = QUrlQuery()
        query.addQueryItem('application', CoopsApplicationName)
        query.addQueryItem('begin_date',
                           self.startTime.toString('yyyyMMdd hh:mm'))
        query.addQueryItem('end_date',
                           self.endTime.addSecs(-1).toString('yyyyMMdd hh:mm'))
        query.addQueryItem('units', 'english')
        query.addQueryItem('time_zone', 'gmt')
        query.addQueryItem('product', self.productName)
        query.addQueryItem('format', 'xml')
        self.addQueryItems(query)

        return self.baseUrl + '?' + query.query()
예제 #3
0
    def get(self,
            url: str,
            params: dict = None,
            timeout: int = 20000,
            **kwargs) -> Reply:
        '''
        queries given url (GET)

        Parameters
        ----------
        url : str
            the url to request
        params : dict, optional
            query parameters with the parameters as keys and the values as
            values, defaults to no query parameters
        timeout : int, optional
            the timeout of synchronous requests in milliseconds, will be ignored
            when making asynchronous requests, defaults to 20000 ms
        **kwargs :
            additional parameters matching the requests interface will
            be ignored (e.g. verify is not supported)

        Returns
        ----------
        Reply
           the response is returned in case of synchronous calls, if you are
           using asynchronous calls retrieve the response via the finished-
           signal instead
        '''
        qurl = QUrl(url)

        if params:
            query = QUrlQuery()
            for param, value in params.items():
                query.addQueryItem(param, str(value))
            qurl.setQuery(query.query())

        if self.synchronous:
            return self._get_sync(qurl, timeout=timeout)

        return self._get_async(qurl)
예제 #4
0
    def wfs(self):
        conn = QgsOwsConnection("wfs", self.connectionCombo.currentText())
        uri = conn.uri().param('url')
        version = conn.uri().param('version')
        if version == "auto":
            # detect version
            u = QUrlQuery(uri)
            u.addQueryItem("request", "GetCapabilities")
            u.addQueryItem("acceptversions", "2.0.0,1.1.0,1.0.0")

            xml, ns_map = xml_parse(remote_open_from_qgis(u.query()))
            root = xml.getroot()
            versions = [
                v.text for v in root.findall(
                    "./ows:ServiceIdentification/ows:ServiceTypeVersion",
                    ns_map)
            ]
            # take the greatest version, if more than one
            version = sorted(versions)[-1]

        with qgis_proxy_settings():
            return WebFeatureService(url=uri, version=version)
예제 #5
0
    def query(self, *args: object, max_retries: int = 2, **kwargs: object
              ) -> Reply:
        '''
        query the service

        Parameters
        ----------
        *args
            query parameters without keyword
        **kwargs
            query parameters with keyword and value
        max_retries: int, optional
            maximum number of retries after connection error, defaults to 2
            retries

        Returns
        ----------
        Reply
            the reply of the geocoding API, contains a list of geojson features
            with "geometry" attribute of the matched address "properties"
            containing "text" (description of found address in BKG database),
            "typ", "treffer" and "score" (the higher the better the match)

        Raises
        ----------
        RuntimeError
            critical error (no parameters, no access to service/url),
            it is recommended to abort geocoding
        ValueError
            request got through but parameters were malformed,
            may still work for different features
        '''
        self.params = {}
        retries = 0
        if self.rs:
            self.params['filter'] = f'rs:{self.rs}'
        self.params['srsname'] = self.crs
        query = self._build_params(*args, **kwargs)
        if not query:
            raise ValueError('keine Suchparameter gefunden')
        self.params['query'] = query
        do_post = self.area_wkt is not None
        if self.area_wkt:
            self.params['geometry'] = self.area_wkt
        while True:
            try:
                if not do_post:
                    self.reply = requests.get(self.url, params=self.params)
                else:
                    content_type = 'application/x-www-form-urlencoded'
                    data = QUrlQuery()
                    for k, v in self.params.items():
                        data.addQueryItem(k, v)
                    self.reply = requests.post(
                        self.url, data=data.query().encode('utf-8'),
                        content_type=content_type
                    )
            except ConnectionError:
                if retries >= max_retries:
                    raise RuntimeError(
                        f'Anfrage nach {retries + 1} gescheiterten '
                        'Verbindungsversuchen abgebrochen.')
                retries += 1
                continue
            break
        self.raise_on_error(self.reply)
        return self.reply