From 8f4395634471d8e5d0ba543f5885db586cae98e6 Mon Sep 17 00:00:00 2001 From: Julian Schliwinski Date: Fri, 17 Jan 2025 09:53:56 +0100 Subject: [PATCH] Enhancement: Add optional arguments to MetricsQuery --- ads/metrics.py | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/ads/metrics.py b/ads/metrics.py index 7036286..e8c5979 100644 --- a/ads/metrics.py +++ b/ads/metrics.py @@ -33,16 +33,34 @@ class MetricsQuery(BaseQuery): HTTP_ENDPOINT = METRICS_URL - def __init__(self, bibcodes): - """ - :param bibcodes: Bibcodes to send to in the metrics query - :type bibcodes: list or string - """ - self.response = None # current MetricsResponse object - if isinstance(bibcodes, six.string_types): - bibcodes = [bibcodes] - self.bibcodes = bibcodes - self.json_payload = json.dumps({"bibcodes": bibcodes}) + def __init__(self, bibcodes, types=None, histograms=None): + """ + :param bibcodes: Bibcodes to send to in the metrics query + :type bibcodes: list or string + :param types: Optional keyword to specify the metrics type to retrieve. + :type types: list or string + :param histograms: Optional keyword can be set to specify the histograms to + return, if `histograms` is specified under types. + :type histograms: list or string + """ + self.response = None # current MetricsResponse object + if isinstance(bibcodes, six.string_types): + bibcodes = [bibcodes] + self.bibcodes = bibcodes + + # Handle optionals + optionals = {} + for opt in ["types", "histograms"]: + par = locals()[opt] + if isinstance(par, six.string_types): + par = [par] + setattr(self, opt, par) + if types is not None: + optionals["types"] = self.types + if "histograms" in types and histograms is not None: + optionals["histograms"] = self.histograms + + self.json_payload = json.dumps({"bibcodes": bibcodes, **optionals}) def execute(self): """