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) {
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) {
}
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);
}
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");
}
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;
// 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:
}
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;
}
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;
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;
}
// 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;
}
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>
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);