From: Michael Tremer Date: Fri, 27 Jun 2025 09:17:06 +0000 (+0000) Subject: xfer: Put the API response into a struct X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=7e5e94dbd6095458f85ca1eaa6e49b455a8eba24;p=pakfire.git xfer: Put the API response into a struct This will allow us to type a bit less and easily extend this in the future. Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/repo_delete.c b/src/cli/lib/repo_delete.c index 9724aaa7..4dd35070 100644 --- a/src/cli/lib/repo_delete.c +++ b/src/cli/lib/repo_delete.c @@ -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) { diff --git a/src/cli/lib/upload_list.c b/src/cli/lib/upload_list.c index 0e35fbc8..b3bbdc82 100644 --- a/src/cli/lib/upload_list.c +++ b/src/cli/lib/upload_list.c @@ -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) { diff --git a/src/pakfire/client.c b/src/pakfire/client.c index b75ea735..e02c58dc 100644 --- a/src/pakfire/client.c +++ b/src/pakfire/client.c @@ -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; diff --git a/src/pakfire/xfer.c b/src/pakfire/xfer.c index dc56c119..bda1b60f 100644 --- a/src/pakfire/xfer.c +++ b/src/pakfire/xfer.c @@ -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; } diff --git a/src/pakfire/xfer.h b/src/pakfire/xfer.h index c9809390..57bb6449 100644 --- a/src/pakfire/xfer.h +++ b/src/pakfire/xfer.h @@ -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 #include #include @@ -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);