#ifndef TRANSTUI_RPC_H
#define TRANSTUI_RPC_H

#include <stddef.h>
#include "../third_party/cJSON.h"

typedef struct RpcClient {
    char host[256];
    int port;
    char user[128];
    char pass[128];
    char session_id[128];
    int last_http_status; /* 0 if the last call never got an HTTP response at all */
} RpcClient;

void rpc_init(RpcClient *c, const char *host, int port, const char *user, const char *pass);

/* Performs a single Transmission JSON-RPC call, transparently handling the
 * X-Transmission-Session-Id CSRF handshake (one retry on HTTP 409).
 *
 * `arguments` ownership is transferred to this call (it will be freed
 * internally) - pass NULL if the method takes no arguments.
 *
 * On success returns 0 and, if out_args is non-NULL, sets *out_args to the
 * "arguments" object of the response (caller must cJSON_Delete it), or NULL
 * if the response had none.
 *
 * On failure returns -1 and fills err with a human-readable message. */
int rpc_call(RpcClient *c, const char *method, cJSON *arguments,
             cJSON **out_args, char *err, size_t errlen);

#endif
