示例#1
0
def primitive_read(query, credentials=credentials, escape=escape, cursor=False):
   # Put the query in an envelope
   envelope = {'query':query, "cursor":cursor}

   # Add escape if needed
#    if escape != 'html':
#        envelope['escape'] = False #        if not escape else escape

   # Encode the result
   encoded = urllib.urlencode({'query': json.encode(envelope)})

   # Build the URL and create a Request object for it
   url = 'http://%s%s?%s' % (host, readservice, encoded)
   headers = {}
   if credentials is not None: headers["Cookie"] = credentials
   resp = fetch(url, method=GET, headers=headers)
   inner = json.decode(resp.content)      # Parse JSON response to an object

   # If anything was wrong with the invocation, mqlread will return an HTTP
   # error, and the code above with raise urllib2.HTTPError.
   # If anything was wrong with the query, we won't get an HTTP error, but
   # will get an error status code in the response envelope.  In this case
   # we raise our own MQLError exception.
   if not inner['code'].startswith('/api/status/ok'):
       if debug: 
           print "\n".join(map(str, q, inner, resp.headers['X-Metaweb-Cost'], resp.headers['X-Metaweb-TID']))
       error = inner['messages'][0]
       raise MQLError('%s: %s' % (error['code'], error['message']), error['message'])

   # If there was no error, then just return the result from the envelope
   return inner
示例#2
0
 def pretty_query(self):
     return json.encode(json.decode(self.query))