Ejemplo n.º 1
0
def test_fetch_market_history(config=CONFIG):
    """test `fetch_market_history` utility"""

    data = crest_utils.fetch_market_history(config.get('TEST', 'region_id'),
                                            config.get('TEST', 'type_id'),
                                            config=ROOT_CONFIG)

    assert isinstance(data, pd.DataFrame)
    expected_cols = [
        'date', 'avgPrice', 'highPrice', 'lowPrice', 'volume', 'orders'
        #extra keys:
        #'volume_str',
        #'orderCountStr'
    ]
    for key in expected_cols:
        assert key in data.columns.values

    ohlc = crest_utils.data_to_ohlc(data)

    assert ohlc['date'].equals(data['date'])
    assert ohlc['open'].equals(data['avgPrice'])
    assert ohlc['high'].equals(data['highPrice'])
    assert ohlc['low'].equals(data['lowPrice'])
    assert ohlc['volume'].equals(data['volume'])

    assert data['avgPrice'].shift(1).equals(ohlc['close'])
Ejemplo n.º 2
0
def test_fetch_market_history_esi(config=CONFIG):
    """test `fetch_market_history` utility"""

    data = crest_utils.fetch_market_history(
        config.get('TEST', 'region_id'),
        config.get('TEST', 'type_id'),
        config=ROOT_CONFIG,
    )

    assert isinstance(data, pd.DataFrame)
    expected_cols = [
        'date',
        'avgPrice',
        'highPrice',
        'lowPrice',
        'volume',
        'orders'
        #extra keys:
        #'volume_str',
        #'orderCountStr'
    ]
    for key in expected_cols:
        assert key in data.columns.values

    ohlc = crest_utils.data_to_ohlc(data)

    assert ohlc['date'].equals(data['date'])
    assert ohlc['open'].equals(data['avgPrice'])
    assert ohlc['high'].equals(data['highPrice'])
    assert ohlc['low'].equals(data['lowPrice'])
    assert ohlc['volume'].equals(data['volume'])

    assert data['avgPrice'].shift(1).equals(ohlc['close'])
Ejemplo n.º 3
0
    def get(self, return_type):
        """GET data from CREST and send out OHLC info"""
        args = self.reqparse.parse_args()
        #TODO: info archive
        LOGGER.info('OHLC {0} Request: {1}'.format(return_type, args))

        if return_type not in return_supported_types():
            return 'INVALID RETURN FORMAT', 405

        mode = api_config.SwitchCCPSource(
            api_config.CONFIG.get('GLOBAL', 'crest_or_esi'))
        ## Validate inputs ##
        try:
            crest_utils.validate_id('map_regions',
                                    args.get('regionID'),
                                    mode=mode,
                                    config=api_config.CONFIG,
                                    logger=LOGGER)
            crest_utils.validate_id('inventory_types',
                                    args.get('typeID'),
                                    mode=mode,
                                    config=api_config.CONFIG,
                                    logger=LOGGER)
        except exceptions.ValidatorException as err:
            LOGGER.warning('ERROR: unable to validate type/region ids' +
                           '\n\targs={0}'.format(args),
                           exc_info=True)
            return err.message, err.status
        except Exception:  #pragma: no cover
            LOGGER.error('ERROR: unable to validate type/region ids' +
                         'args={0}'.format(args),
                         exc_info=True)
            return 'UNHANDLED EXCEPTION', 500

        ## Fetch CREST ##
        try:
            #LOGGER.info(api_config.SPLIT_INFO)
            if args.get('typeID') in api_config.SPLIT_INFO:
                LOGGER.info('FORK: using split utility')
                data = split_utils.fetch_split_history(
                    args.get('regionID'),
                    args.get('typeID'),
                    mode,
                    config=api_config.CONFIG,
                    logger=LOGGER)
            else:
                data = crest_utils.fetch_market_history(
                    args.get('regionID'),
                    args.get('typeID'),
                    config=api_config.CONFIG,
                    mode=mode,
                    logger=LOGGER)
            data = crest_utils.data_to_ohlc(data)
        except exceptions.ValidatorException as err:  #pragma: no cover
            LOGGER.error('ERROR: unable to parse CREST data' +
                         '\n\targs={0}'.format(args),
                         exc_info=True)
            return err.message, err.status
        except Exception:  #pragma: no cover
            #except Exception as err:    #pragma: no cover
            LOGGER.error('ERROR: unhandled issue in parsing CREST data' +
                         'args={0}'.format(args),
                         exc_info=True)
            return 'UNHANDLED EXCEPTION', 500

        ## Format output ##
        if return_type == AcceptedDataFormat.JSON.value:
            LOGGER.info('rolling json response')
            data_str = data.to_json(path_or_buf=None, orient='records')
            message = json.loads(data_str)
        elif return_type == AcceptedDataFormat.CSV.value:
            LOGGER.info('rolling csv response')
            data_str = data.to_csv(
                path_or_buf=None,
                header=True,
                index=False,
                columns=['date', 'open', 'high', 'low', 'close', 'volume'])
            message = output_csv(data_str, 200)
        else:  #pragma: no cover
            #TODO: CUT?
            LOGGER.error('invalid format requested' +
                         '\n\targs={0}'.format(args) +
                         '\n\treturn_type={0}'.format(return_type),
                         exc_info=True)
            return 'UNSUPPORTED FORMAT', 500

        return message
Ejemplo n.º 4
0
    def get(self, return_type):
        """GET data from CREST and send out OHLC info"""
        args = self.reqparse.parse_args()
        #TODO: info archive
        self.logger.info('OHLC %s Request: %s', return_type, args)

        if return_type not in return_supported_types():
            return 'INVALID RETURN FORMAT', 405
        ## Validate inputs ##
        try:
            crest_utils.validate_id(
                'map_regions',
                args.get('regionID'),
                config=api_config.CONFIG,
                logger=self.logger,
            )
            crest_utils.validate_id(
                'inventory_types',
                args.get('typeID'),
                config=api_config.CONFIG,
                logger=self.logger,
            )
        except exceptions.ValidatorException as err:
            self.logger.warning(
                'ERROR: unable to validate type/region ids' +
                '\n\targs={0}'.format(args),
                exc_info=True
            )
            return err.message, err.status
        except Exception: #pragma: no cover
            self.logger.error(
                'ERROR: unable to validate type/region ids' +
                'args={0}'.format(args),
                exc_info=True
            )
            return 'UNHANDLED EXCEPTION', 500

        ## Fetch CREST ##
        try:
            #LOGGER.info(api_config.SPLIT_INFO)
            if args.get('typeID') in api_config.SPLIT_INFO:
                self.logger.info('FORK: using split utility')
                data = split_utils.fetch_split_history(
                    args.get('regionID'),
                    args.get('typeID'),
                    config=api_config.CONFIG,
                    logger=self.logger,
                )
            else:
                data = crest_utils.fetch_market_history(
                    args.get('regionID'),
                    args.get('typeID'),
                    config=api_config.CONFIG,
                    logger=LOGGER
                )
            data = crest_utils.data_to_ohlc(data)
        except exceptions.ValidatorException as err: #pragma: no cover
            self.logger.error(
                'ERROR: unable to parse CREST data\n\targs=%s',
                args,
                exc_info=True
            )
            return err.message, err.status
        except Exception: #pragma: no cover
            self.logger.error(
                'ERROR: unhandled issue in parsing CREST data\n\targs=%s',
                args,
                exc_info=True
            )
            return 'UNHANDLED EXCEPTION', 500

        ## Format output ##
        if return_type == AcceptedDataFormat.JSON.value:
            self.logger.info('rolling json response')
            data_str = data.to_json(
                path_or_buf=None,
                orient='records'
            )
            message = json.loads(data_str)
        elif return_type == AcceptedDataFormat.CSV.value:
            self.logger.info('rolling csv response')
            data_str = data.to_csv(
                path_or_buf=None,
                header=True,
                index=False,
                columns=[
                    'date',
                    'open',
                    'high',
                    'low',
                    'close',
                    'volume'
                ]
            )
            message = output_csv(data_str, 200)
        else:   #pragma: no cover
            #TODO: CUT?
            self.logger.error(
                'invalid format requested' +
                '\n\targs=%s' +
                '\n\treturn_type=%s',
                args, return_type,
                exc_info=True
            )
            return 'UNSUPPORTED FORMAT', 500

        return message