Skip to content

Commit

Permalink
Add ProPublica Api Adapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomaszKolek authored and brylie committed May 6, 2019
1 parent 8a669e3 commit c754d11
Show file tree
Hide file tree
Showing 8 changed files with 424 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env_sample
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
SUNLIGHT_API_KEY=""
GOOGLE_MAP_API_KEY=<google_map_key>
DJANGO_SECRET_KEY=<django_secret_key>
PROPUBLICA_API_KEY=<pro_publica_api_key>

POSTGRES_HOST=db
POSTGRES_PORT=5432
Expand Down
17 changes: 17 additions & 0 deletions project/api/propublica.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import requests
from django.conf import settings


class ProPublicaAPI(object):
URL = 'https://api.propublica.org/congress/v1/bills/search.json?query="{query}"'

def __init__(self, api_key=None):
self.api_key = api_key or settings.PROPUBLICA_API_KEY
self.auth_headers = {'X-API-Key': self.api_key}

def search(self, query):
if not self.api_key:
return {}
response = requests.get(self.URL.format(query=query), headers=self.auth_headers)
response.raise_for_status()
return response.json()
Empty file added project/api/tests/__init__.py
Empty file.
367 changes: 367 additions & 0 deletions project/api/tests/propublica_responses.py

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions project/api/tests/test_propublica.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from mock import patch, MagicMock

from rest_framework.test import APITestCase, override_settings

from ..propublica import ProPublicaAPI
from .propublica_responses import PROPUBLICA_CORRECT_RESPONSE


@override_settings(PROPUBLICA_API_KEY='test-token')
class ProPublicaAPITestCase(APITestCase):
def setUp(self):
self.api_instance = ProPublicaAPI()

def test_headers_are_set_correctly(self):
self.assertDictEqual(self.api_instance.auth_headers, {'X-API-Key': 'test-token'})

@patch('api.propublica.requests.get')
def test_if_search_returns_correct_results(self, get_mock):
get_mock.return_value = MagicMock(json=MagicMock(return_value=PROPUBLICA_CORRECT_RESPONSE))
query = 'query'
data = self.api_instance.search(query)
self.assertEqual(data, PROPUBLICA_CORRECT_RESPONSE)
get_mock.assert_called_once_with(
self.api_instance.URL.format(query=query),
headers=self.api_instance.auth_headers
)
1 change: 1 addition & 0 deletions project/civiwiki/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,4 @@ def get_env_variable(environment_variable, optional=False):
}
# CORS Settings
CORS_ORIGIN_ALLOW_ALL = True
PROPUBLICA_API_KEY = get_env_variable("PROPUBLICA_API_KEY", optional=True)
12 changes: 11 additions & 1 deletion readmes/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

#### Run tests

aloe .
cd project && aloe .


## Run unit tests (docker setup required)

#### Login to docker shell

docker exec -it civiwiki-backend /bin/bash

#### Run tests

cd project && python manage.py test

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ flake8==3.4.1
gunicorn==19.7.1
honcho==1.0.1
kombu==4.1.0
mock==2.0.0
Pillow==4.3.0
psycopg2==2.7.3.2
sunlight==1.2.9
Expand Down

0 comments on commit c754d11

Please sign in to comment.