0

Summary of the problem:

  1. I'm running a requests call on an API endpoint, whose request params are hidden in a config file and I decided to try out hydra to retrieve those params [Reason being the request params do change as I'm working on collecting custom dataset using RapidAPI]

  2. I have created a class called QueryParamsLocations which implements the getter methods to fetch the parameters to be later used by run_query method.

class QueryParamsLocations(QueryParams):
    
    @hydra.main(config_path='configs', config_name='location_query')
    def get_params_query_string(self, cfg: DictConfig) -> dict():
        return {
            'query': cfg.location_params.query,
            'locale': cfg.location_params.locale,
            'currency': cfg.location_params.currency
        }
    
    @hydra.main(config_path='configs', config_name='location_query')
    def get_url(self, cfg: DictConfig) -> str():
        return cfg.urls.location_url

    @hydra.main(config_path='configs', config_name='location_query')
    def get_headers(self, cfg: DictConfig) -> dict():
        return {
            'X-RapidAPI-Host': cfg.headers.x_rapidapi_host, 
            'X-RapidAPI-Key': cfg.headers.x_rapidapi_key
        }
  1. class QueryParams is an abstract class which has these 3 getter templates. run_query method is an external call to run the request.
@hydra.main(config_path='configs', config_name='location_query')
def run_query(cfg: DictConfig) -> None:
    try:
        LoggerFactory.get_logger('logs/logger.log', 'INFO').info('Running query for location')
        qpl = QueryParamsLocations()    
        response = requests.request("GET", qpl.get_url(cfg), headers=qpl.get_headers(cfg), params=qpl.get_params_query_string(cfg))
        print(response.json())
    except Exception as e:
        LoggerFactory.get_logger('logs/logger.log',
                                 'ERROR').error(f'Error in running query: {e}')


run_query()
  1. While running run_query without if name == 'main': and with it as well , the following error is encountered :
[2022-05-16 13:43:32,614][logs/logger.log][ERROR] - Error in running query: **decorated_main()** takes from 0 to 1 positional arguments but 2 were given

Although newer version of hydra (I'm using hydra-core==1.1.2) uses two arguments while creating cfg object however , I'm not sure as to whether there's other way of handling this as such.

Also, by searching through other threads, following was also tried - Compose API

however, from the docs, it requires an override parameter , which is not needed atm.

Would like to know if any other approach can be tried out. Happy to provide more details if needed.

Soulpianist96
  • 21
  • 1
  • 5

1 Answers1

1

Definitely use the Compose API and not hydra.main() for this use case. You can just pass an empty array for your override list if you have nothing to override.

Omry Yadan
  • 31,280
  • 18
  • 64
  • 87