]> git.ipfire.org Git - pakfire.git/commitdiff
xfer: Put the API response into a struct
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 27 Jun 2025 09:17:06 +0000 (09:17 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 27 Jun 2025 09:17:06 +0000 (09:17 +0000)
This will allow us to type a bit less and easily extend this in the
future.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/cli/lib/repo_delete.c
src/cli/lib/upload_list.c
src/pakfire/client.c
src/pakfire/xfer.c
src/pakfire/xfer.h

index 9724aaa7cfc9a5434536648b576851db8e5aeae0..4dd35070892ff574a5bf70bfc5edabae202e1996 100644 (file)
@@ -60,15 +60,12 @@ static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
        return 0;
 }
 
-static int response_callback(struct pakfire_xfer* xfer, pakfire_xfer_error_code_t code,
-               int status, struct json_object* response, void* data) {
-       switch (code) {
-               case PAKFIRE_XFER_OK:
-                       return cli_dump_json(response);
+static int response_callback(struct pakfire_xfer* xfer,
+               const pakfire_xfer_response* response, void* data) {
+       if (!response->error)
+               return cli_dump_json(response->data);
 
-               default:
-                       return code;
-       }
+       return response->error;
 }
 
 static int ready_callback(struct pakfire_client* client, void* data) {
index 0e35fbc8204f4fc59281ab813a8dce1aca19bfe8..b3bbdc821adb127591f958b93a5c94fb96560db6 100644 (file)
@@ -30,9 +30,9 @@
 static const char* doc = "Lists all uploads";
 
 static int list_callback(struct pakfire_xfer* xfer,
-               pakfire_xfer_error_code_t code, int status, struct json_object* response, void* data) {
+               const pakfire_xfer_response* response, void* data) {
        // XXX For now, just dump the entire JSON object
-       return cli_dump_json(response);
+       return cli_dump_json(response->data);
 }
 
 static int ready_callback(struct pakfire_client* client, void* data) {
index b75ea7353bea45e33104a6c3e1cf9cc2eb196506..e02c58dcb5925f8e90d1d59e2ac858e66c64467b 100644 (file)
@@ -167,7 +167,7 @@ static int pakfire_client_ready(struct pakfire_client* self) {
 }
 
 static int pakfire_client_auth_response(struct pakfire_xfer* xfer,
-       pakfire_xfer_error_code_t code, int status, json_object* response, void* data);
+       const pakfire_xfer_response* response, void* data);
 
 static int pakfire_client_auth_required(struct pakfire_client* self);
 
@@ -649,12 +649,12 @@ static int pakfire_client_auth_successful(
 }
 
 static int pakfire_client_auth_response(struct pakfire_xfer* xfer,
-               pakfire_xfer_error_code_t code, int status, json_object* response, void* data) {
+               const pakfire_xfer_response* response, void* data) {
        struct pakfire_client* client = data;
 
-       switch (code) {
+       switch (response->error) {
                case PAKFIRE_XFER_OK:
-                       return pakfire_client_auth_successful(client, response);
+                       return pakfire_client_auth_successful(client, response->data);
 
                default:
                        ERROR(client->ctx, "Authentication failed\n");
@@ -893,7 +893,7 @@ static void pakfire_client_build_free(struct pakfire_client_build* self) {
 }
 
 static int pakfire_client_build_response(struct pakfire_xfer* xfer,
-               pakfire_xfer_error_code_t code, int status, struct json_object* response, void* data) {
+               const pakfire_xfer_response* response, void* data) {
        struct pakfire_client_build* build = data;
        const char* uuid = NULL;
        int r = 0;
@@ -901,12 +901,12 @@ static int pakfire_client_build_response(struct pakfire_xfer* xfer,
        // Only process this if we have a callback
        if (build->callback) {
                // Extract the UUID from the response
-               r = pakfire_json_get_string(response, "uuid", &uuid);
+               r = pakfire_json_get_string(response->data, "uuid", &uuid);
                if (r < 0)
                        goto ERROR;
 
                // Call the callback
-               r = build->callback(build->client, code, uuid, build->data);
+               r = build->callback(build->client, response->error, uuid, build->data);
        }
 
 ERROR:
@@ -1095,13 +1095,13 @@ ERROR:
 }
 
 static int pakfire_upload_payload_callback(struct pakfire_xfer* xfer,
-               pakfire_xfer_error_code_t code, int status, struct json_object* response, void* data) {
+               const pakfire_xfer_response* response, void* data) {
        pakfire_client_upload_status s = PAKFIRE_CLIENT_UPLOAD_SUCCESSFUL;
        struct pakfire_client_upload* upload = data;
        int r = 0;
 
        // Determine the status
-       switch (code) {
+       switch (response->error) {
                case PAKFIRE_XFER_OK:
                        break;
 
@@ -1163,29 +1163,24 @@ ERROR:
 }
 
 static int pakfire_client_upload_response(struct pakfire_xfer* xfer,
-               pakfire_xfer_error_code_t code, int status, struct json_object* response, void* data) {
+               const pakfire_xfer_response* response, void* data) {
        struct pakfire_client_upload* self = data;
        const char* uuid = NULL;
        int r;
 
-       switch (code) {
-               case PAKFIRE_XFER_OK:
-                       // Fetch the UUID from the response
-                       r = pakfire_json_get_string(response, "uuid", &uuid);
-                       if (r < 0)
-                               return r;
-
-                       // Store the UUID
-                       r = pakfire_string_set(self->uuid, uuid);
-                       if (r < 0)
-                               return r;
+       if (!response->error) {
+               // Fetch the UUID from the response
+               r = pakfire_json_get_string(response->data, "uuid", &uuid);
+               if (r < 0)
+                       return r;
 
-                       // Upload the payload
-                       return pakfire_client_upload_payload(self);
+               // Store the UUID
+               r = pakfire_string_set(self->uuid, uuid);
+               if (r < 0)
+                       return r;
 
-               // XXX Handle any errors
-               default:
-                       break;
+               // Upload the payload
+               return pakfire_client_upload_payload(self);
        }
 
        return -EINVAL;
index dc56c11937229ce9173fb2b6eddefd4b21ff2fec..bda1b60f3cac0a931fbffd6c704204fc4a11a0e7 100644 (file)
@@ -934,18 +934,23 @@ ERROR:
        return r;
 }
 
-static int pakfire_xfer_call_response_callback(struct pakfire_xfer* self, pakfire_xfer_error_code_t code, int status) {
-       struct json_object* response = NULL;
-       char* error = NULL;
+static int pakfire_xfer_call_response_callback(struct pakfire_xfer* self, pakfire_xfer_error_code_t error, int status) {
+       char* e = NULL;
        int r;
 
+       // Compose the response
+       pakfire_xfer_response response = {
+               .error  = error,
+               .status = status,
+       };
+
        // Parse the API response
        if (self->output.length) {
-               switch (code) {
+               switch (error) {
                        case PAKFIRE_XFER_OK:
-                               r = pakfire_json_parse(&response, &error, self->output.buffer, self->output.length);
+                               r = pakfire_json_parse(&response.data, &e, self->output.buffer, self->output.length);
                                if (r < 0) {
-                                       ERROR(self->ctx, "Failed to parse JSON response: %s\n", error);
+                                       ERROR(self->ctx, "Failed to parse JSON response: %s\n", e);
                                        goto ERROR;
                                }
                                break;
@@ -956,13 +961,13 @@ static int pakfire_xfer_call_response_callback(struct pakfire_xfer* self, pakfir
        }
 
        // Run the callback
-       r = self->callbacks.response(self, code, status, response, self->callbacks.data);
+       r = self->callbacks.response(self, &response, self->callbacks.data);
 
 ERROR:
-       if (response)
-               json_object_put(response);
-       if (error)
-               free(error);
+       if (response.data)
+               json_object_put(response.data);
+       if (e)
+               free(e);
 
        return r;
 }
index c9809390dcc73712a6e535cbdfcbcd96c3de8bbb..57bb64493188d226bca0da56fb25f57f10cd8edd 100644 (file)
@@ -68,6 +68,17 @@ typedef enum pakfire_xfer_error_code {
        PAKFIRE_XFER_DIGEST_MISMATCH,
 } pakfire_xfer_error_code_t;
 
+typedef struct pakfire_xfer_response {
+       // Error Code
+       pakfire_xfer_error_code_t error;
+
+       // HTTP Status Code
+       int status;
+
+       // Body
+       json_object* data;
+} pakfire_xfer_response;
+
 #include <pakfire/ctx.h>
 #include <pakfire/hasher.h>
 #include <pakfire/hashes.h>
@@ -127,8 +138,8 @@ int pakfire_xfer_set_output(struct pakfire_xfer* xfer, FILE* f);
 int pakfire_xfer_set_output_buffer(struct pakfire_xfer* xfer, char** buffer, size_t* length);
 int pakfire_xfer_set_output_path(struct pakfire_xfer* xfer, const char* path);
 
-typedef int (*pakfire_xfer_response_callback)(struct pakfire_xfer* xfer,
-       pakfire_xfer_error_code_t error, int code, struct json_object* response, void* data);
+typedef int (*pakfire_xfer_response_callback)
+       (struct pakfire_xfer* xfer, const pakfire_xfer_response* response, void* data);
 
 int pakfire_xfer_set_response_callback(struct pakfire_xfer* xfer,
        pakfire_xfer_response_callback callback, void* data);