Rewrite for callback

FossilOrigin-Name: 566b27167b069de545cad08fad87afeb48d3a183f2fb219cd6eaeb8d100f87c6
This commit is contained in:
me@ow.nekobit.net 2022-03-14 01:51:08 +00:00
parent 4ad9964ab4
commit d1e9993a6b
3 changed files with 87 additions and 27 deletions

View file

@ -29,7 +29,9 @@ struct mastodont_request_args
size_t params_post_len;
CURLoption request_type;
void* args;
int (*callback)(struct mstdnt_fetch_results* results, void*);
int (*callback)(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
void*);
};
int mastodont_request(mastodont_t* data, struct mastodont_request_args* args);

View file

@ -15,9 +15,39 @@
#include <stdlib.h>
#include <mastodont_account.h>
#include <mastodont_request.h>
#include <mastodont_json_helper.h>
#include "mastodont_fetch.h"
struct mastodont_account_args
{
struct mstdnt_account* acct;
size_t* size;
};
static int mastodont_account_callback(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
void* _args)
{
struct mastodont_account_args* args = _args;
struct mstdnt_account* acct = args->acct;
size_t* size = args->size;
cJSON* root;
/* TODO cleanup this */
root = cJSON_ParseWithLength(results->response, results->size);
if (root == NULL)
{
return 1;
}
storage->root = root;
storage->needs_cleanup = 1;
mstdnt_load_account_from_json(acct, root->child);
return 0;
}
int mastodont_account(mastodont_t* data,
int lookup, /* TODO move into separate function for consistancy */
char* id,
@ -25,36 +55,26 @@ int mastodont_account(mastodont_t* data,
struct mstdnt_storage* storage,
size_t* size)
{
int res = 0;
cJSON* root;
struct mastodont_account_args acct_args = { acct, size };
/* Url */
char url[MSTDNT_URLSIZE];
struct mstdnt_fetch_results results = { 0 };
snprintf(url, MSTDNT_URLSIZE,
lookup ? "api/v1/accounts/%s" : "api/v1/accounts/lookup?acct=%s",
id);
storage->needs_cleanup = 0;
if (mastodont_fetch_curl(data, url, &results, CURLOPT_HTTPGET) != CURLE_OK)
{
return 1;
}
/* TODO cleanup this */
root = cJSON_ParseWithLength(results.response, results.size);
if (root == NULL)
{
res = 1;
goto cleanup;
}
storage->root = root;
storage->needs_cleanup = 1;
mstdnt_load_account_from_json(acct, root->child);
cleanup:
mastodont_fetch_results_cleanup(&results);
return res;
struct mastodont_request_args args = {
storage,
url,
NULL,
0,
NULL,
0,
CURLOPT_HTTPGET,
&acct_args, /* args */
mastodont_account_callback, /* callback */
};
return mastodont_request(data, &args);
}

38
src/request.c Normal file
View file

@ -0,0 +1,38 @@
/*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <mastodont_request.h>
int mastodont_request(mastodont_t* data, struct mastodont_request_args* args)
{
int res = 0;
struct mstdnt_storage* storage = args->storage;
struct mstdnt_fetch_results results = { 0 };
if (mastodont_fetch_curl(data, args->url, &results, CURLOPT_HTTPGET) != CURLE_OK)
{
return 1;
}
/* TODO Check if error json */
args->callback(&results, storage, args->args);
cleanup:
mastodont_fetch_results_cleanup(&results);
return res;
}