]> git.ipfire.org Git - people/ms/pakfire.git/commitdiff
client: Process response when listing repositories
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 1 Jul 2025 14:47:35 +0000 (14:47 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 1 Jul 2025 14:47:35 +0000 (14:47 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/repo_list.c
src/pakfire/client.c
src/pakfire/client.h

index ff3af4c1f0fd7580c343de193e891283eefc65b5..7b0a436752b1e46b922a7dd7a2e1f9dadb1e3297 100644 (file)
@@ -21,7 +21,9 @@
 #include <json.h>
 
 #include <pakfire/client.h>
+#include <pakfire/json.h>
 
+#include "api.h"
 #include "command.h"
 #include "dump.h"
 #include "pakfire.h"
@@ -52,13 +54,50 @@ static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
        return 0;
 }
 
-static int ready_callback(pakfire_client* client, void* data) {
-       const struct cli_local_args* local_args = data;
+static int response_callback(struct pakfire_xfer* xfer,
+               const pakfire_xfer_response* response, void* data) {
+       struct cli_local_args* local_args = data;
+       json_object* repo = NULL;
+       const char* name = NULL;
+       int64_t priority = 0;
+       int r;
+
+       // Handle errors
+       r = cli_api_error(response, "Failed to fetch repositories for %s", local_args->distro);
+       if (r < 0)
+               return 0;
+
+       // Show a header
+       printf("%-12s %-12s\n", "Name", "Priority");
+
+       // Loop through all repos
+       for (size_t i = 0; i < json_object_array_length(response->data); i++) {
+               repo = json_object_array_get_idx(response->data, i);
+               if (!repo)
+                       break;
+
+               // Fetch the name
+               r = pakfire_json_get_string(repo, "name", &name);
+               if (r < 0)
+                       break;
 
-       // XXX Needs a callback
+               // Fetch the priority
+               r = pakfire_json_get_int64(repo, "priority", &priority);
+               if (r < 0)
+                       break;
+
+               printf("%-12s %-12ld\n", name, priority);
+       }
+
+       return r;
+}
+
+static int ready_callback(pakfire_client* client, void* data) {
+       struct cli_local_args* local_args = data;
 
        // List repos
-       return pakfire_client_list_repos(client, local_args->distro);
+       return pakfire_client_list_repos(client,
+               local_args->distro, response_callback, local_args);
 }
 
 int cli_repo_list(void* data, int argc, char* argv[]) {
index 3bfacbd872e457097a39d0489ca11675402f7861..ce95ec743cc0edf49084c16668a4d2d33d19b90f 100644 (file)
@@ -1313,7 +1313,8 @@ ERROR:
 
 // Repositories
 
-int pakfire_client_list_repos(pakfire_client* self, const char* distro) {
+int pakfire_client_list_repos(pakfire_client* self, const char* distro,
+               pakfire_xfer_response_callback callback, void* data) {
        pakfire_xfer* xfer = NULL;
        int r;
 
@@ -1322,7 +1323,7 @@ int pakfire_client_list_repos(pakfire_client* self, const char* distro) {
                return -EINVAL;
 
        // Create a new xfer
-       r = pakfire_client_xfer_create(&xfer, self, "/api/v1/repos/%s", distro);
+       r = pakfire_client_xfer_create(&xfer, self, "/api/v1/distros/%s/repos", distro);
        if (r < 0)
                goto ERROR;
 
@@ -1331,7 +1332,10 @@ int pakfire_client_list_repos(pakfire_client* self, const char* distro) {
        if (r < 0)
                goto ERROR;
 
-       // XXX Needs a callback
+       // Register the callback
+       r = pakfire_xfer_set_response_callback(xfer, callback, data);
+       if (r < 0)
+               goto ERROR;
 
        // Enqueue the transfer
        r = pakfire_httpclient_enqueue(self->httpclient, xfer);
index cb523421a387f3c2f881e0f348f99d9327a10b53..1274f4a999987424f39069e54188b01e5cc352a1 100644 (file)
@@ -97,7 +97,8 @@ int pakfire_client_delete_upload(pakfire_client* self, const char* uuid,
 
 // Repositories
 
-int pakfire_client_list_repos(pakfire_client* client, const char* distro);
+int pakfire_client_list_repos(pakfire_client* client, const char* distro,
+       pakfire_xfer_response_callback callback, void* data);
 int pakfire_client_get_repo(pakfire_client* client,
        const char* distro, const char* name);
 int pakfire_client_create_repo(pakfire_client* client,