[libvirt] [jenkins-ci PATCH v2 06/20] quayadmin: Build endpoint, params and payload separately

Andrea Bolognani posted 20 patches 6 years, 5 months ago
[libvirt] [jenkins-ci PATCH v2 06/20] quayadmin: Build endpoint, params and payload separately
Posted by Andrea Bolognani 6 years, 5 months ago
Instead of building all arguments for a request at the same
time as the request itself is prepared, build them beforehand
and then pass them to the corresponding function.

This makes the code more readable, especially when complex
params or long endpoints are involved.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
---
 guests/quayadmin | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/guests/quayadmin b/guests/quayadmin
index 6eeacd1..7ddf7a4 100755
--- a/guests/quayadmin
+++ b/guests/quayadmin
@@ -66,18 +66,26 @@ def has_error(quiet, res, expected, message):
 
 
 def run_list_repos(args):
-    res = get("/repository", params={"namespace": args.namespace})
+    endpoint = "/repository"
+    params = {
+        "namespace": args.namespace,
+    }
+
+    res = get(endpoint, params=params)
 
     if has_error(args.quiet, res, 200, "Cannot list repositories"):
         return 1
 
     info = res.json()
+
     for repo in info["repositories"]:
         print ("{}".format(repo["name"]))
 
 
 def run_show_repo(args):
-    res = get("/repository/{}/{}".format(args.namespace, args.repo))
+    endpoint = "/repository/{}/{}".format(args.namespace, args.repo)
+
+    res = get(endpoint)
 
     if has_error(args.quiet, res, 200, "Cannot query repository {}/{}"
                  .format(args.namespace, args.repo)):
@@ -95,13 +103,16 @@ def run_show_repo(args):
 
 
 def run_create_repo(args):
-    res = post("/repository", payload={
+    endpoint = "/repository"
+    payload = {
         "repo_kind": "image",
         "namespace": args.namespace,
         "visibility": "public",
         "repository": args.repo,
         "description": args.desc,
-    })
+    }
+
+    res = post(endpoint, payload=payload)
 
     if has_error(args.quiet, res, 201, "Cannot create repository {}/{}"
                  .format(args.namespace, args.repo)):
@@ -114,7 +125,9 @@ def run_create_repo(args):
 
 
 def run_delete_repo(args):
-    res = delete("/repository/{}/{}".format(args.namespace, args.repo))
+    endpoint = "/repository/{}/{}".format(args.namespace, args.repo)
+
+    res = delete(endpoint)
 
     if has_error(args.quiet, res, 204, "Cannot delete repository {}/{}"
                  .format(args.namespace, args.repo)):
-- 
2.21.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [jenkins-ci PATCH v2 06/20] quayadmin: Build endpoint, params and payload separately
Posted by Daniel P. Berrangé 6 years, 5 months ago
On Wed, Jul 17, 2019 at 03:49:15PM +0200, Andrea Bolognani wrote:
> Instead of building all arguments for a request at the same
> time as the request itself is prepared, build them beforehand
> and then pass them to the corresponding function.
> 
> This makes the code more readable, especially when complex
> params or long endpoints are involved.
> 
> Signed-off-by: Andrea Bolognani <abologna@redhat.com>
> ---
>  guests/quayadmin | 23 ++++++++++++++++++-----
>  1 file changed, 18 insertions(+), 5 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list