From 187cd935dfea0ade80d775967b4945e83dd37b1b Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Thu, 23 Aug 2018 15:29:26 -0700 Subject: [PATCH] Add a function to get_url_json_unlimited to retrieve the total The blueprints/changes API is a bit different from the others, the total that it includes is for each blueprint, not one total for all of them, since there will be a different number of commits for each. The function is passed the dict, and it can be used to select the total to use for retrieving all of the results. If it isn't included it will use data["total"] which works fine in most cases. (cherry picked from commit 0a76d635cab975f2df1c06153e8ad00189cfc035) --- src/composer/http_client.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/composer/http_client.py b/src/composer/http_client.py index b9544519..f64642f7 100644 --- a/src/composer/http_client.py +++ b/src/composer/http_client.py @@ -93,7 +93,7 @@ def get_url_json(socket_path, url): r = http.request("GET", url) return json.loads(r.data.decode('utf-8')) -def get_url_json_unlimited(socket_path, url): +def get_url_json_unlimited(socket_path, url, total_fn=None): """Return the JSON results of a GET request For URLs that use offset/limit arguments, this command will @@ -106,6 +106,10 @@ def get_url_json_unlimited(socket_path, url): :returns: The json response from the server :rtype: dict """ + def default_total_fn(data): + """Return the total number of available results""" + return data["total"] + http = UnixHTTPConnectionPool(socket_path) # Start with limit=0 to just get the number of objects @@ -113,8 +117,12 @@ def get_url_json_unlimited(socket_path, url): r_total = http.request("GET", total_url) json_total = json.loads(r_total.data.decode('utf-8')) + # Where to get the total from + if not total_fn: + total_fn = default_total_fn + # Add the "total" returned by limit=0 as the new limit - unlimited_url = append_query(url, "limit=%d" % json_total["total"]) + unlimited_url = append_query(url, "limit=%d" % total_fn(json_total)) r_unlimited = http.request("GET", unlimited_url) return json.loads(r_unlimited.data.decode('utf-8'))