Exemplo 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)
Exemplo 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
Exemplo 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)
Exemplo 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 []
Exemplo 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 []
Exemplo 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']
Exemplo 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']
Exemplo n.º 8
0
Arquivo: api.py Projeto: 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)
Exemplo 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']
Exemplo 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()
Exemplo n.º 11
0
Arquivo: models.py Projeto: 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
Exemplo n.º 12
0
 def api_request(self, *args, **kwargs):
     response = utils.api_request(*args, **kwargs)
     sleep(10)
     return response
Exemplo n.º 13
0
 def setUp(self):
     self.responses = []
     for i in range(AP_API_LIMIT):
         self.responses.append(utils.api_request('/'))
Exemplo n.º 14
0
 def api_request(self, *args, **kwargs):
     response = utils.api_request(*args, **kwargs)
     sleep(10)
     return response
Exemplo n.º 15
0
 def setUp(self):
     self.responses = []
     for i in range(AP_API_LIMIT):
         self.responses.append(utils.api_request('/'))
Exemplo 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)