Sample Code – Python

The following code fragment illustrates how to call the Cruvee API using query string authentication in Python.

import urllib2
import time
import hashlib
import simplejson
 
# baseurl = /search/brands
# argsurl = ?q=oregon&fmt=json&  <-- note trailing &
def cruvee_fetch_json(baseurl, argsurl):
 
    site = CRUVEE_SERVER  # http://apiv1.cruvee.com or http://apiv1.test.cruvee.com
    timestamp = str(int(time.time())*1000)
    sig = CRUVEE_API_KEY + '\nGET\n' + CRUVEE_SECRET_KEY + '\n' + timestamp + '\n' + baseurl + '\n'
    sig = sig.lower()
    m = hashlib.md5()
    m.update(sig)
    hexsig = m.hexdigest()
    url = site + baseurl + argsurl + 'appId=' + CRUVEE_API_KEY + '&sig=' + hexsig + '&timestamp=' + timestamp
    try:
        req = urllib2.Request(url)
        sock = urllib2.urlopen(req)
        jsonstring = unicode(sock.read(), 'utf-8')
        sock.close()
    except IOError, e:
        logging.debug('IO ERROR: ' + str(e.code))
        return None
    try:
        json = simplejson.loads(jsonstring)
    except ValueError, e:
        logging.debug('BAD JSON: ' + str(e))
        return None
 
    return json

Leave a Comment/Question

Please leave these two fields as-is: