From: Michael Tremer Date: Tue, 1 Jul 2025 14:47:35 +0000 (+0000) Subject: client: Process response when listing repositories X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0d66f3e6227d70df9eaadc57303b4cef1eeb1c20;p=pakfire.git client: Process response when listing repositories Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/repo_list.c b/src/cli/lib/repo_list.c index ff3af4c1..7b0a4367 100644 --- a/src/cli/lib/repo_list.c +++ b/src/cli/lib/repo_list.c @@ -21,7 +21,9 @@ #include #include +#include +#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[]) { diff --git a/src/pakfire/client.c b/src/pakfire/client.c index 3bfacbd8..ce95ec74 100644 --- a/src/pakfire/client.c +++ b/src/pakfire/client.c @@ -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); diff --git a/src/pakfire/client.h b/src/pakfire/client.h index cb523421..1274f4a9 100644 --- a/src/pakfire/client.h +++ b/src/pakfire/client.h @@ -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,