1

How can I cache Mashape API calls. I got the code below but it does not seem to be doing the caching. Mashape is using unirest to get the API response.

def fetchMashape(url, headers):
    cached = unirest.get(url, headers=headers) #cache.get(url)
    content =""
    if not cached:
        response = unirest.get(url,
                    headers=headers)
    else:
        response = cached
    dictionary = json.loads(response.raw_body)

    return dictionary

I was able to do it with a URL where I can append the API key using the request http library http://docs.python-requests.org/en/latest/index.html e.g.

from django.core.cache import cache
from urllib2 import Request, urlopen, build_opener
from urllib import urlencode
import json, ast
import unirest
import requests
import xmltodict

test = fetchJson("http//www.myapi.com/v1/MY_API_KEY/query/json=blahblah")

#fetchJson with caching
def fetchJson(url):
    cached = cache.get(url)
    content = ""
    if not cached:
        r = requests.get(url)
        if(r.ok):
            cache.set(url, r.text)
            content = r.text
        else:
            content = None  
    else:
        # Return the cached content
        content = cached
    if content is not None:
        return json.loads(content)
    else:
        return None

I am using django 1.6.6. Cache are stored in a database. My settings.py file. The name of the database is dev_cache.

  CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
            'LOCATION': 'dev_cache',
            'TIMEOUT': 60*60*24*7*4,
            'VERSION': 1,
            'OPTIONS': {
                'MAX_ENTRIES': 1022000
            }


 }
}
cloudviz
  • 971
  • 4
  • 15
  • 40
  • 1
    Hi cloudviz, hope I can help you but please can you let me know a bit more on the below: * Did you install the Unirest library? I don't see it in your code * By caching do you mean saving the API response into a variable? Please keep in mind that if you run the script two times the variable will cease to exist after the first script has run, maybe you're better off writing it onto a DB or a static file on your disk somewhere for caching purposes? – API_sheriff_orlie Oct 04 '14 at 14:08
  • Thanks. I've updated my question @API_sheriff_orlie. I have installed Unirest library. Caching - I want to store the content of the response (e.g. JSON) into a database called dev_cache. It's all working fine, it's the caching of mashape api response that's not working properly. – cloudviz Oct 04 '14 at 15:01
  • did you try to define the "response" variable outside the if scope? – API_sheriff_orlie Oct 04 '14 at 18:36
  • I got it working (i think). The response happens within the scope of the if only if the url is not cached. If the url is cached, then return the cached content. – cloudviz Oct 05 '14 at 09:38
  • 1
    Yes I believe it was a scope error indeed. – API_sheriff_orlie Oct 05 '14 at 20:54

1 Answers1

1

This is my working solution (feel free to improve this code)

Example usage

url = "https://something_api.p.mashape.com/q=whateverquery",
headers={"X-Mashape-Key": "ABCDEFG12345"}
test = fetchMashape(url, headers)

def fetchMashape(url, headers):
    cached = cache.get(url)
    content = ""
    if not cached:
        response = unirest.get(url,
              headers=headers)
        print "not cached"
        if response is not None:
            cache.set(url, response.raw_body)
            content = response.raw_body
        else:
            content = None
    else:
        content = cached
        print "already cached"

    dictionary = json.loads(content)

    return dictionary
cloudviz
  • 971
  • 4
  • 15
  • 40