Skip to content
This repository has been archived by the owner on Dec 23, 2017. It is now read-only.

Commit

Permalink
Merge pull request #269 from jmcarp/feature/respect-caching-headers
Browse files Browse the repository at this point in the history
Respect caching headers from API.
  • Loading branch information
LindsayYoung committed Jun 15, 2015
2 parents 905b363 + ba435fb commit a853da9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 20 deletions.
5 changes: 1 addition & 4 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
from openfecwebapp import utils
from openfecwebapp import config
from openfecwebapp.views import render_search_results, render_candidate, render_committee
from openfecwebapp.api_caller import load_search_results, load_with_nested, install_cache
from openfecwebapp.api_caller import load_search_results, load_with_nested

import jinja2
import json
import locale
import logging
import re
import sys


locale.setlocale(locale.LC_ALL, '')
Expand Down Expand Up @@ -273,7 +272,5 @@ def restrict_cycles(value):


if __name__ == '__main__':
if '--cached' in sys.argv:
install_cache()
files = ['./rev-manifest.json']
app.run(host=config.host, port=int(config.port), debug=config.debug, extra_files=files)
1 change: 1 addition & 0 deletions manifest_prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ env:
FEC_WEB_API_URL: https://api.open.fec.gov/
FEC_WEB_SERVER_NAME: open.fec.gov
FEC_FORCE_HTTPS: true
FEC_WEB_CACHE: true
29 changes: 14 additions & 15 deletions openfecwebapp/api_caller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@
from urllib import parse

import requests
import cachecontrol

from openfecwebapp import utils
from openfecwebapp.config import api_location, api_version, api_key
from openfecwebapp import config


MAX_FINANCIALS_COUNT = 4


session = requests.Session()

if config.cache:
cachecontrol.CacheControl(session, cache=utils.LRUCache(config.cache_size))


def _call_api(*path_parts, **filters):
if api_key:
filters['api_key'] = api_key
if config.api_key:
filters['api_key'] = config.api_key

path = os.path.join(api_version, *[x.strip('/') for x in path_parts])
url = parse.urljoin(api_location, path)
path = os.path.join(config.api_version, *[x.strip('/') for x in path_parts])
url = parse.urljoin(config.api_location, path)

results = requests.get(url, params=filters)
results = session.get(url, params=filters)

if results.status_code == requests.codes.ok:
return results.json()
else:
return {}
return results.json() if results.ok else {}


def load_search_results(query, query_type='candidates'):
Expand Down Expand Up @@ -69,8 +73,3 @@ def load_cmte_financials(committee_id, **filters):
'reports': reports['results'],
'totals': totals['results'],
}


def install_cache():
import requests_cache
requests_cache.install_cache()
2 changes: 2 additions & 0 deletions openfecwebapp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
api_key = os.getenv('FEC_WEB_API_KEY', '')
api_key_public = os.getenv('FEC_WEB_API_KEY_PUBLIC', '')
server_name = os.getenv('FEC_WEB_SERVER_NAME')
cache = os.getenv('FEC_WEB_CACHE')
cache_size = int(os.getenv('FEC_WEB_CACHE_SIZE', 1000))

# the username and password should be the same for both the
# web app and API
Expand Down
24 changes: 24 additions & 0 deletions openfecwebapp/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import datetime
import threading

import cachetools
import cachecontrol


def current_cycle():
year = datetime.datetime.now().year
return year + year % 2


class LRUCache(cachecontrol.cache.BaseCache):
"""A thread-safe least recently updated cache adapted to work with
Cache-Control.
"""
def __init__(self, maxsize):
self.lock = threading.Lock()
self.data = cachetools.LRUCache(maxsize)

def get(self, key):
return self.data.get(key, None)

def set(self, key, value):
with self.lock:
self.data[key] = value

def delete(self, key):
with self.lock:
self.data.clear()
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ pytest>=2.7.0
pytest-cov>=1.8.1
python-dateutil>=2.4.2
requests==2.5.1
requests-cache==0.4.9
selenium>=2.45.0
newrelic==2.50.0.39
gunicorn
webargs>=0.13.0
Flask-SSLify
CacheControl>=0.11.5
cachetools>=1.0.1

git+https://github.com/jmcarp/flask-compress@cache

0 comments on commit a853da9

Please sign in to comment.