def test_happypath_regions(self): """make sure behavior is good for regions too""" region_info = crest_utils.validate_id( 'map_regions', self.region_id, config=ROOT_CONFIG ) assert region_info['name'] == 'The Forge' region_info_retry = crest_utils.validate_id( 'map_regions', self.region_id, config=ROOT_CONFIG ) assert region_info_retry == region_info region_info_esi = crest_utils.validate_id( 'map_regions', self.region_id, cache_buster=True, config=ROOT_CONFIG, ) assert region_info_esi['name'] == region_info['name'] assert region_info_esi['region_id'] == region_info['region_id'] assert region_info_esi['description'] == region_info['description']
def test_happypath_types(self): """make sure behavior is expected for direct use""" type_info = crest_utils.validate_id('inventory_types', self.type_id, config=ROOT_CONFIG) assert type_info['name'] == 'Tritanium' type_info_retry = crest_utils.validate_id('inventory_types', self.type_id, config=ROOT_CONFIG) assert type_info_retry == type_info type_info_esi = crest_utils.validate_id( 'inventory_types', self.type_id, cache_buster=True, config=ROOT_CONFIG, mode=api_config.SwitchCCPSource.ESI) assert type_info_esi['name'] == type_info['name'] assert type_info_esi['description'] == type_info['description'] assert type_info_esi['published'] == type_info['published'] assert type_info_esi['radius'] == type_info['radius'] assert type_info_esi['icon_id'] == type_info['iconID'] assert type_info_esi['capacity'] == type_info['capacity'] assert type_info_esi['type_id'] == type_info['id']
def test_happypath_types(self): """make sure behavior is expected for direct use""" #pytest.skip('CREST endpoint deprecated') type_info = crest_utils.validate_id( 'inventory_types', self.type_id, config=ROOT_CONFIG ) assert type_info['name'] == 'Tritanium' type_info_retry = crest_utils.validate_id( 'inventory_types', self.type_id, config=ROOT_CONFIG ) assert type_info_retry == type_info type_info_esi = crest_utils.validate_id( 'inventory_types', self.type_id, cache_buster=True, config=ROOT_CONFIG, ) print(type_info) print(type_info_esi) assert type_info == type_info_esi
def test_happypath_regions(self): """make sure behavior is good for regions too""" region_info = crest_utils.validate_id('map_regions', self.region_id, config=ROOT_CONFIG) assert region_info['name'] == 'The Forge' region_info_retry = crest_utils.validate_id('map_regions', self.region_id, config=ROOT_CONFIG) assert region_info_retry == region_info
def test_happypath_types(self): """make sure behavior is expected for direct use""" type_info = crest_utils.validate_id('inventory_types', self.type_id, config=ROOT_CONFIG) assert type_info['name'] == 'Tritanium' type_info_retry = crest_utils.validate_id('inventory_types', self.type_id, config=ROOT_CONFIG) assert type_info_retry == type_info
def test_validate_bad_ids(self): """check behavior with bad id's""" with pytest.raises(exceptions.IDValidationError): data = crest_utils.validate_id('inventory_types', self.bad_typeid, config=ROOT_CONFIG) try: data = crest_utils.validate_id('inventory_types', self.bad_typeid, config=ROOT_CONFIG) except exceptions.IDValidationError as err_msg: assert err_msg.status == 404 assert isinstance(err_msg.message, str)
def test_validate_bad_ids(self): """check behavior with bad id's""" with pytest.raises(exceptions.IDValidationError): data = crest_utils.validate_id( 'inventory_types', self.bad_typeid, config=ROOT_CONFIG ) try: data = crest_utils.validate_id( 'inventory_types', self.bad_typeid, config=ROOT_CONFIG ) except exceptions.IDValidationError as err_msg: assert err_msg.status == 404 assert isinstance(err_msg.message, str)
def test_validate_region_cache(self): """make sure cachebuster and stash agree""" type_info = crest_utils.validate_id( 'map_regions', self.region_id, config=ROOT_CONFIG, cache_buster=True, ) tdb_handle = crest_utils.setup_cache_file('map_regions') type_cache = tdb_handle.search(Query().index_key == self.region_id)[0] assert type_info == type_cache['payload']
def test_happypath_regions(self): """make sure behavior is good for regions too""" region_info = crest_utils.validate_id('map_regions', self.region_id, config=ROOT_CONFIG) assert region_info['name'] == 'The Forge' region_info_retry = crest_utils.validate_id('map_regions', self.region_id, config=ROOT_CONFIG) assert region_info_retry == region_info region_info_esi = crest_utils.validate_id( 'map_regions', self.region_id, cache_buster=True, config=ROOT_CONFIG, mode=api_config.SwitchCCPSource.ESI) assert region_info_esi['name'] == region_info['name'] assert region_info_esi['region_id'] == region_info['id'] assert region_info_esi['description'] == region_info['description']
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
def get(self, return_type): args = self.reqparse.parse_args() LOGGER.info('Prophet {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')) forecast_range = api_config.DEFAULT_RANGE if 'range' in args: forecast_range = args.get('range') ## Validate inputs ## try: api_utils.check_key(args.get('api'), throw_on_fail=True, logger=LOGGER) crest_utils.validate_id('map_regions', args.get('regionID'), config=api_config.CONFIG, mode=mode, logger=LOGGER) crest_utils.validate_id('inventory_types', args.get('typeID'), config=api_config.CONFIG, mode=mode, logger=LOGGER) forecast_range = forecast_utils.check_requested_range( forecast_range, max_range=api_config.MAX_RANGE, raise_for_status=True) 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 ## check cache ## cache_data = forecast_utils.check_prediction_cache( args.get('regionID'), args.get('typeID')) LOGGER.debug(cache_data) if cache_data is not None: LOGGER.info('returning cached forecast') message = forecast_reporter(cache_data, forecast_range, return_type, LOGGER) return message ## No cache, get data ## try: 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, data_range=api_config.MAX_RANGE, config=api_config.CONFIG, logger=LOGGER) data.sort_values(by='date', ascending=True, inplace=True) else: data = forecast_utils.fetch_extended_history( args.get('regionID'), args.get('typeID'), mode=mode, data_range=api_config.MAX_RANGE, config=api_config.CONFIG, logger=LOGGER) data = forecast_utils.build_forecast(data, api_config.MAX_RANGE) except exceptions.ValidatorException as err: #FIX ME: testing? LOGGER.warning('ERROR: unable to generate forecast' + '\n\targs={0}'.format(args), exc_info=True) return err.message, err.status except Exception: #pragma: no cover LOGGER.error('ERROR: unable to generate forecast' + '\n\targs={0}'.format(args), exc_info=True) return 'UNHANDLED EXCEPTION', 500 ## Update cache ## forecast_utils.write_prediction_cache(args.get('regionID'), args.get('typeID'), data, logger=LOGGER) try: message = forecast_reporter(data, forecast_range, return_type, LOGGER) except Exception as err_msg: #pragma: no cover LOGGER.error('invalid format requested' + '\n\targs={0}'.format(args) + '\n\treturn_type={0}'.format(return_type), exc_info=True) return 'UNABLE TO GENERATE REPORT', 500 return message
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
def get(self, return_type): args = self.reqparse.parse_args() self.logger.info('Prophet %s Request: %s', return_type, args) if return_type not in return_supported_types(): return 'INVALID RETURN FORMAT', 405 forecast_range = api_config.DEFAULT_RANGE if 'range' in args: forecast_range = args.get('range') ## Validate inputs ## try: api_utils.check_key( args.get('api'), throw_on_fail=True, logger=self.logger, ) 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, ) forecast_range = forecast_utils.check_requested_range( forecast_range, max_range=api_config.MAX_RANGE, raise_for_status=True ) except exceptions.ValidatorException as err: self.logger.warning( 'ERROR: unable to validate type/region ids\n\targs=%s', args, exc_info=True ) return err.message, err.status except Exception: #pragma: no cover self.logger.error( 'ERROR: unable to validate type/region ids\n\targs=%s', args, exc_info=True ) return 'UNHANDLED EXCEPTION', 500 ## check cache ## cache_data = forecast_utils.check_prediction_cache( args.get('regionID'), args.get('typeID') ) self.logger.debug(cache_data) if cache_data is not None: self.logger.info('returning cached forecast') message = forecast_reporter( cache_data, forecast_range, return_type, self.logger, ) return message ## No cache, get data ## try: 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'), data_range=api_config.MAX_RANGE, config=api_config.CONFIG, logger=self.logger, ) data.sort_values( by='date', ascending=True, inplace=True ) else: data = forecast_utils.fetch_extended_history( args.get('regionID'), args.get('typeID'), data_range=api_config.MAX_RANGE, config=api_config.CONFIG, logger=self.logger, ) data = forecast_utils.build_forecast( data, api_config.MAX_RANGE ) except exceptions.ValidatorException as err: #FIX ME: testing? self.logger.warning( 'ERROR: unable to generate forecast\n\targs=%s', args, exc_info=True ) return err.message, err.status except Exception: #pragma: no cover LOGGER.error( 'ERROR: unable to generate forecast\n\targs=%s', args, exc_info=True ) return 'UNHANDLED EXCEPTION', 500 ## Update cache ## forecast_utils.write_prediction_cache( args.get('regionID'), args.get('typeID'), data, logger=self.logger, ) try: message = forecast_reporter( data, forecast_range, return_type, self.logger, ) except Exception as err_msg: #pragma: no cover LOGGER.error( 'invalid format requested' '\n\targs=%s' '\n\treturn_type=%s', args, return_type, exc_info=True ) return 'UNABLE TO GENERATE REPORT', 500 return message