-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a669e3
commit c754d11
Showing
8 changed files
with
424 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters