Beispiel #1
0
def get(baseurl,
        params={},
        private_keys_to_ignore=["api_key"],
        permanent_cache_file=PERMANENT_CACHE_FNAME,
        temp_cache_file=TEMP_CACHE_FNAME):
    full_url = requests.requestURL(baseurl, params)
    cache_key = make_cache_key(baseurl, params, private_keys_to_ignore)
    # Load the permanent and page-specific caches from files
    permanent_cache = _read_from_file(permanent_cache_file)
    temp_cache = _read_from_file(temp_cache_file)
    if cache_key in temp_cache:
        print("found in temp_cache")
        # make a Response object containing text from the change, and the full_url that would have been fetched
        return requests.Response(temp_cache[cache_key], full_url)
    elif cache_key in permanent_cache:
        print("found in permanent_cache")
        # make a Response object containing text from the change, and the full_url that would have been fetched
        return requests.Response(permanent_cache[cache_key], full_url)
    else:
        print("new; adding to cache")
        # actually request it
        resp = requests.get(baseurl, params)
        # save it
        add_to_cache(temp_cache_file, cache_key, resp.text)
        return resp
Beispiel #2
0
def get(base_url,
        dict_params={},
        private_keys=['api_key'],
        permanent_cache_file=PERM_CACHE_FNAME):
    full_url = requests.requestURL(base_url, dict_params)
    cache_key = make_cache_key(base_url, dictionary, private_keys)
    # Load the permanent caches from files
    permanent_cache = _read_from_file(permanent_cache_file)
    if cache_key in permanent_cache:
        # Make a Response object containing text from the change, and the full_url
        # that would have been fetched
        return requests.Response(permanent_cache[cache_key], full_url)
    else:
        # Actually request the query
        resp = request.get(base_url, dict_params)
        # Save it
        permanent_cache[cache_key] = resp.text
        _write_to_file(permanent_cache, PERM_CACHE_FNAME)
        return resp
def get(baseurl, params={}, private_keys_to_ignore=["api_key", "apikey"], permanent_cache_file=PERMANENT_CACHE_FNAME, temp_cache_file=TEMP_CACHE_FNAME):
    full_url = requests.requestURL(baseurl, params)
    cache_key = make_cache_key(baseurl, params, private_keys_to_ignore)
    # Load the permanent and page-specific caches from files
    permanent_cache = _read_from_file(permanent_cache_file)
    temp_cache = _read_from_file(temp_cache_file)
    if cache_key in temp_cache:
        print("found in page-specific cache")
        # make a Response object containing text from the change, and the full_url that would have been fetched
        return requests.Response(temp_cache[cache_key], full_url)
    elif cache_key in permanent_cache:
        print("found in permanent_cache")
        # make a Response object containing text from the change, and the full_url that would have been fetched
        return requests.Response(permanent_cache[cache_key], full_url)
    else:
        print("new; adding to cache")
        # actually request it
        resp = requests.get(baseurl, params)
        # save it
        add_to_cache(temp_cache_file, cache_key, resp.text)
        return resp