Esempio n. 1
0
    def _get_data(self, url_path, params=None):
        url = '{0}/{1}/{2}'.format(self.BASE_URL, self.API_VERSION, url_path)

        try:
            response = requests.get(url, params=params, headers=self.header)
        except requests.HTTPError as http_error:
            if http_error.code == 404:
                raise exceptions.PyChartsRequestUrlNotFoundException()
            elif http_error.code == 401:
                raise exceptions.PyChartsRequestUnauthorizedException()
            elif http_error.code == 400:
                raise exceptions.PyChartsRequestException()
            else:
                raise

        parsed_rsp = response.json()

        # raise any payload level errors
        if parsed_rsp['meta']['status'] == 'error':
            error_code = parsed_rsp['meta']['error_code']
            error_message = parsed_rsp['meta']['error_message']
            if error_code == 400:
                raise exceptions.PyChartsRequestException(
                    error_message=error_message)
            elif error_code == 414:
                raise exceptions.PyChartsRequestTooLongException(
                    error_message=error_message)

        return parsed_rsp
Esempio n. 2
0
 def _format_query_date_for_url(self, query_date):
     if isinstance(query_date, datetime.datetime):
         return query_date.isoformat().split('T')[0]
     elif isinstance(query_date, int) and query_date < 0:
         return query_date
     else:
         error_message = 'Invalid Date paramter. Date should be a datetime object or a negative integer.'
         raise exceptions.PyChartsRequestException(error_message=error_message)
Esempio n. 3
0
    def _parse_response(self, req):
        try:
            response = urlopen(req).read().decode('utf-8')
        except HTTPError as http_error:
            if http_error.code == 404:
                raise exceptions.PyChartsRequestUrlNotFoundException()
            elif http_error.code == 401:
                raise exceptions.PyChartsRequestUnauthorizedException()
            elif http_error.code == 400:
                raise exceptions.PyChartsRequestException()
            else:
                raise

        parsed_rsp = json.loads(response)
        # raise any payload level errors
        if parsed_rsp['meta']['status'] == 'error':
            error_code = parsed_rsp['meta']['error_code']
            error_message = parsed_rsp['meta']['error_message']
            if error_code == 400:
                raise exceptions.PyChartsRequestException(error_message=error_message)
            elif error_code == 414:
                raise exceptions.PyChartsRequestTooLongException(error_message=error_message)

        return parsed_rsp
Esempio n. 4
0
    def get_securities(self, page=1, **filter_param):
        """
        Queries /<security_type> endpoint to return a paged
        list of securities.
        """
        url_path = self._build_url_path(None, None)
        params = {'page': page}
        # the endpoints respond just fine to invaliid query params,
        # they just ignore them, but the the real value of the endpoints
        # is only revealed when using the filters, so let's not waste
        # requests on filters that don't do anything.
        if filter_param:
            query_filter = filter_param.popitem()
            if query_filter[0] in self.VALID_SECURITY_FILTERS:
                params[query_filter[0]] = query_filter[1]
            else:
                error_msg = 'Invalid filter param. Must be one of: {0}'.format(','.join(self.VALID_SECURITY_FILTERS))
                raise exceptions.PyChartsRequestException(error_msg)

        return self._get_data(url_path, params)