Esempio n. 1
0
 def test_nonexistent_param(self):
     r = utils.api_request('/', foo='bar')
     self.assertTrue('errorCode' in r)
     self.assertTrue('errorMessage' in r)
     self.assertEqual(r['errorCode'], 400)
     self.assertEqual(r['errorMessage'], 'Specified parameter(s) \'foo\' is invalid')
     self.assertFalse('races' in r)
Esempio n. 2
0
    def get_elections(self, datafile=None):
        """
        Get election data from API or cached file.

        :param datafile:
            If datafile is specified, use instead of making an API call.
        """
        if not datafile:
            elections = list(
                utils.api_request('/elections').json().get('elections'))
        else:
            with open(datafile) as f:
                elections = list(json.load(f).get('elections'))

        # Developer API expects to give lowercase kwargs to an Election
        # object, but initializing from the API / file will have camelCase
        # kwargs instead. So, for just this object, lowercase the kwargs.
        payload = []
        for e in elections:
            init_dict = OrderedDict()
            for k, v in e.items():
                init_dict[k.lower()] = v
            payload.append(Election(**init_dict))

        return payload
Esempio n. 3
0
 def test_bad_date(self):
     r = utils.api_request('/9999-99-99')
     self.assertTrue('errorCode' in r)
     self.assertTrue('errorMessage' in r)
     self.assertEqual(r['errorCode'], 400)
     self.assertEqual(r['errorMessage'], 'String was not recognized as a valid DateTime.')
     self.assertFalse('races' in r)
Esempio n. 4
0
def _get_reports(params={}):
    """
    Get reports
    """
    resp = utils.api_request('/reports', **params)
    if resp.ok:
        return resp.json().get('reports')
    else:
        return []
Esempio n. 5
0
def _get_reports(params={}):
    """
    Use percache to dump a report response to disk
    """
    resp = utils.api_request('/reports', **params)
    if resp.ok:
        return resp.json().get('reports')
    else:
        cache.clear()
        return []
Esempio n. 6
0
 def get_ap_report(self, key, params={}):
     """
     Given a report number and a key for indexing, returns a list
     of delegate counts by party. Makes a request from the AP
     using requests. Formats that request with env vars.
     """
     reports = utils.get_reports(params=params)
     report_id = self.get_report_id(reports, key)
     if report_id:
         r = utils.api_request('/reports/{0}'.format(report_id), **params)
         return r.json()['trendtable']
Esempio n. 7
0
 def get_ap_report(self, key, params={}):
     """
     Given a report number and a key for indexing, returns a list
     of delegate counts by party. Makes a request from the AP
     using requests. Formats that request with env vars.
     """
     reports = utils.get_reports(params=params)
     report_id = self.get_report_id(reports, key)
     if report_id:
         r = utils.api_request('/reports/{0}'.format(report_id))
         return r.json()['trendtable']
Esempio n. 8
0
File: api.py Progetto: eads/elex
    def get(self, path, **params):
        """
        Farms out request to api_request.
        Could possibly handle choosing which
        parser backend to use -- API-only right now.
        Also the entry point for recording, which
        is set via environment variable.

        :param path:
            API url path.
        :param **params:
            A dict of optional parameters to be included in API request.
        """
        return utils.api_request(path, **params)
Esempio n. 9
0
    def get_ap_report(self, params={}):
        """
        Given a report number, returns a list of counts by party.

        Makes a request from the AP using requests. Formats that request
        with env vars.
        """
        reports = utils.get_reports(params=params)
        report_id = self.get_report_id(reports)
        if report_id:
            r = utils.api_request(
                '/reports/{0}'.format(report_id),
                **self.format_api_request_params()
            )
            return r.json()['trendtable']
Esempio n. 10
0
    def get(self, path, **params):
        """
        Farms out request to api_request.
        Could possibly handle choosing which
        parser backend to use -- API-only right now.
        Also the entry point for recording, which
        is set via environment variable.

        :param path:
            API url path.
        :param **params:
            A dict of optional parameters to be included in API request.
        """
        self._response = utils.api_request('/elections/{0}'.format(path), **params)
        return self._response.json()
Esempio n. 11
0
File: models.py Progetto: SYU88/elex
    def get_elections(self, datafile=None):
        """
        Get election data from API or cached file.

        :param datafile:
            If datafile is specified, use instead of making an API call.
        """
        if not datafile:
            elections = list(utils.api_request('/elections').json().get('elections'))
        else:
            with open(datafile) as f:
                elections = list(json.load(f).get('elections'))

        # Developer API expects to give lowercase kwargs to an Election
        # object, but initializing from the API / file will have camelCase
        # kwargs instead. So, for just this object, lowercase the kwargs.
        payload = []
        for e in elections:
            init_dict = OrderedDict()
            for k, v in e.items():
                init_dict[k.lower()] = v
            payload.append(Election(**init_dict))

        return payload
Esempio n. 12
0
 def api_request(self, *args, **kwargs):
     response = utils.api_request(*args, **kwargs)
     sleep(10)
     return response
Esempio n. 13
0
 def setUp(self):
     self.responses = []
     for i in range(AP_API_LIMIT):
         self.responses.append(utils.api_request('/'))
Esempio n. 14
0
 def api_request(self, *args, **kwargs):
     response = utils.api_request(*args, **kwargs)
     sleep(10)
     return response
Esempio n. 15
0
 def setUp(self):
     self.responses = []
     for i in range(AP_API_LIMIT):
         self.responses.append(utils.api_request('/'))
Esempio n. 16
0
 def test_nonexistent_date(self):
     r = utils.api_request('/1965-01-01')
     self.assertTrue('races' in r)
     self.assertEqual(len(r['races']), 0)