Exemple #1
0
 def queryd(self, query, initBindings={}):
     # initBindings keys can be Variable, but they should not
     # include the questionmark or else they'll get silently
     # ignored
     interpolated = interpolateSparql(query, initBindings)
     # print interpolated
     query = self.sparqlHeader + interpolated
     xml = self.root.get(
         "/" + self.repoName, query=query, queryLn="SPARQL", headers={"Accept": "application/sparql-results+xml"}
     ).body_string()
     return parseSparqlResults(xml.encode("utf-8"))
Exemple #2
0
 def queryd(self, query, initBindings={}):
     # initBindings keys can be Variable, but they should not
     # include the questionmark or else they'll get silently
     # ignored
     interpolated = interpolateSparql(query, initBindings)
     #print interpolated
     query = self.sparqlHeader + interpolated
     xml = self.root.get('/' + self.repoName,
                         query=query, queryLn='SPARQL',
                         headers={'Accept' :
                                  'application/sparql-results+xml'}
                         ).body_string()
     return parseSparqlResults(xml.encode('utf-8'))
Exemple #3
0
    def _getQuery(self, query, initBindings, headers=None, _postProcess=None):
        """send this query to the server, return deferred to the raw
        server result. This is where the prologue (@PREFIX lines) is added."""
        _checkQuerySyntax(query)
        try:
            interpolated = interpolateSparql(query, initBindings)
            log.debug("sparql request: %s", interpolated)
        except Exception:
            log.error("original query=%r, initBindings=%r" %
                      (query, initBindings))
            raise

        # for the stats system to group together all the queries that
        # vary only in their initBindings, I send a (reasonably)
        # unique value for the uninterpolated query too. If I could
        # send the query&bindings separately, this wouldn't be
        # necessary.
        xBindings = (" ".join(initBindings.keys())).encode('utf8')

        sendHeaders = {
            'x-uninterpolated-query-checksum': hashlib.md5(query).hexdigest(),
            'x-bindings': xBindings,
        }
        if self.resultFormat == 'json':
            sendHeaders[
                'Accept'] = 'application/sparql-results+json,text/boolean'
        else:
            sendHeaders['Accept'] = 'application/sparql-results+xml'

        if headers:
            sendHeaders.update(headers)

        def post(ret):
            ret = parseJsonResults(ret)
            if not isinstance(ret, bool):  # ASK result
                ret = _addOptionalVars(ret, query)
            if _postProcess is not None:
                ret = _postProcess(ret)
            return ret

        params = {'query': self.prologue + interpolated}
        params.update(self.getParams)
        return self._request(
            "GET",
            path='',
            queryParams=params,
            headers=sendHeaders,
            postProcess=post,
        )
Exemple #4
0
    def _getQuery(self, query, initBindings, headers=None, _postProcess=None):
        """send this query to the server, return deferred to the raw
        server result. This is where the prologue (@PREFIX lines) is added."""
        _checkQuerySyntax(query)
        try:
            interpolated = interpolateSparql(query, initBindings)
            log.debug("sparql request: %s", interpolated)
        except Exception:
            log.error("original query=%r, initBindings=%r" %
                      (query, initBindings))
            raise

        # for the stats system to group together all the queries that
        # vary only in their initBindings, I send a (reasonably)
        # unique value for the uninterpolated query too. If I could
        # send the query&bindings separately, this wouldn't be
        # necessary.
        xBindings = (" ".join(initBindings.keys())).encode('utf8')

        sendHeaders = {'x-uninterpolated-query-checksum' :
                       hashlib.md5(query).hexdigest(),
                       'x-bindings' : xBindings,
                       }
        if self.resultFormat == 'json':
            sendHeaders['Accept'] = 'application/sparql-results+json,text/boolean'
        else:
            sendHeaders['Accept'] = 'application/sparql-results+xml'
            
        if headers:
            sendHeaders.update(headers)

        def post(ret):
            ret = parseJsonResults(ret)
            if not isinstance(ret, bool): # ASK result
                ret = _addOptionalVars(ret, query)
            if _postProcess is not None:
                ret = _postProcess(ret)
            return ret
        params = {'query' : self.prologue + interpolated}
        params.update(self.getParams)
        return self._request(
            "GET", path='',
            queryParams=params,
            headers=sendHeaders,
            postProcess=post,
            )