newsdev / elex

A wrapper for the AP v2 Elections API.
Other
170 stars 53 forks source link

APAPIKeyException: AP API key is not set. #318

Open actongorton opened 6 years ago

actongorton commented 6 years ago

API Key is being set but not passed.

Example:

e = api.Election(
    electiondate='2016-11-08', 
    datafile=None, 
    testresults=False, 
    liveresults=True, 
    is_test=False,
    api_key={api_key_as_string}
)
raw_races = e.get_raw_races()
race_objs = e.get_race_objects(raw_races)

Other classes work:

ballot_measures = e.ballot_measures
candidate_reporting_units = e.candidate_reporting_units
candidates = e.candidates
races = e.races
reporting_units = e.reporting_units
results = e.results

Stack Trace:

---------------------------------------------------------------------------
APAPIKeyException                         Traceback (most recent call last)
<ipython-input-17-4946a8f26018> in <module>()
----> 1 raw_races = e.get_raw_races()
      2 race_objs = e.get_race_objects(raw_races)

~/venv-science/lib/python3.6/site-packages/elex/api/models.py in get_raw_races(self, **params)
    963                 return payload
    964         else:
--> 965             payload = self.get(self.electiondate, **params)
    966             return payload
    967 

~/venv-science/lib/python3.6/site-packages/elex/api/models.py in get(self, path, **params)
    904             A dict of optional parameters to be included in API request.
    905         """
--> 906         self._response = utils.api_request('/elections/{0}'.format(path), **params)
    907         return self._response.json()
    908 

~/venv-science/lib/python3.6/site-packages/elex/api/utils.py in api_request(path, **params)
     92 
     93     if not params['apiKey']:
---> 94         raise APAPIKeyException()
     95 
     96     params['format'] = 'json'

APAPIKeyException: AP API key is not set.
mileswwatkins commented 6 years ago

It looks like the races method is a convenience property on top of get_raw_races. The rest of the methods you mentioned are also convenience properties. get_raw_races isn't aware of the API key unless it's passed in via one of those convenience properties.

eg, https://github.com/newsdev/elex/blob/master/elex/api/models.py#L1052

palewire commented 6 years ago

Does e.races not get you what you want here?