Bladeren bron

Delete channel request

master
Alexander Memer 11 maanden geleden
bovenliggende
commit
403ece68b4
5 gewijzigde bestanden met toevoegingen van 123 en 0 verwijderingen
  1. 2
    0
      examples/main.c
  2. 1
    0
      include/discord.h
  3. 2
    0
      include/requests.h
  4. 53
    0
      lib/requests.c
  5. 65
    0
      lib/rest.c

+ 2
- 0
examples/main.c Bestand weergeven

@@ -114,5 +114,7 @@ int main(int argc, char **argv)

free(m);

ld_delete_channel(ctx, "482874514124832779");

return 0;
}

+ 1
- 0
include/discord.h Bestand weergeven

@@ -266,6 +266,7 @@ int ld_create_message(struct ld_context *ctx, char *channel_id, char *content);
int ld_get_channel(struct ld_context *ctx, char *channel_id, guild_channel_t *target);
int ld_modify_channel_result(struct ld_context *ctx, char *channel_id, struct ld_modify_channel_obj *modify, guild_channel_t *result);
int ld_modify_channel(struct ld_context *ctx, char *channel_id, struct ld_modify_channel_obj *modify);
int ld_delete_channel(struct ld_context *ctx, char *channel_id);

void ld_channel_obj_to_type(json_t *src, guild_channel_t *channel);


+ 2
- 0
include/requests.h Bestand weergeven

@@ -60,6 +60,8 @@ CURLcode requests_put_headers(CURL *curl, req_t *req, char *url, char *data,
char **custom_hdrv, int custom_hdrc);
CURLcode requests_patch(CURL *curl, req_t *req, char *url, char *data,
char **custom_hdrv, int custom_hdrc, int is_json);
CURLcode requests_delete(CURL *curl, req_t *req, char *url, char *data,
char **custom_hdrv, int custom_hdrc);
char *requests_url_encode(CURL *curl, char **data, int data_size);

#endif

+ 53
- 0
lib/requests.c Bestand weergeven

@@ -88,6 +88,7 @@ void requests_close(req_t *req)
for (int i = 0; i < req->resp_hdrc; i++)
free(req->resp_hdrv[i]);


for (int i = 0; i < req->req_hdrc; i++)
free(req->req_hdrv[i]);

@@ -443,6 +444,58 @@ CURLcode requests_patch(CURL *curl, req_t *req, char *url, char *data,
return rc;
}

CURLcode requests_delete(CURL *curl, req_t *req, char *url, char *data,
char **custom_hdrv, int custom_hdrc)
{
CURLcode rc;
struct curl_slist *slist = NULL;
req->url = url;

/* body data */
if (data != NULL) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
} else {
/* content length header defaults to -1, which causes request to fail
sometimes, so we need to manually set it to 0 */
char *cl_header = "Content-Length: 0";
slist = curl_slist_append(slist, cl_header);
if (slist == NULL)
return -1;
if (custom_hdrv == NULL)
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);

hdrv_append(&req->req_hdrv, &req->req_hdrc, cl_header);
}

/* headers */
if (custom_hdrv != NULL) {
rc = process_custom_headers(&slist, req, custom_hdrv, custom_hdrc);
if (rc != CURLE_OK)
return rc;
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
}

common_opt(curl, req);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
char *ua = user_agent();
curl_easy_setopt(curl, CURLOPT_USERAGENT, ua);
rc = curl_easy_perform(curl);
if (rc != CURLE_OK)
return rc;

long code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
req->code = code;
req->ok = check_ok(code);

if (slist != NULL)
curl_slist_free_all(slist);
free(ua);
curl_easy_cleanup(curl);

return rc;
}

/*
* process_custom_headers - Adds custom headers to request and populates the
* req_headerv and req_hdrc fields of the request struct using the supplied

+ 65
- 0
lib/rest.c Bestand weergeven

@@ -172,6 +172,51 @@ json_t *ld_patch_request(struct ld_context *ctx, char *url, json_t *contents)
return res;
}

json_t *ld_delete_request(struct ld_context *ctx, char *url, json_t *contents)
{
req_t req;
ctx->curl_handle = requests_init(&req);

char *target = malloc(strlen(LD_API_ENDPOINT) + strlen(url) + 1);
char *auth = malloc(strlen("Authorization: Bot ") + strlen(ctx->bot_token) + 1);

sprintf(target, "%s%s", LD_API_ENDPOINT, url);
sprintf(auth, "%s%s", "Authorization: Bot ", ctx->bot_token);

if (!contents)
{
requests_delete(ctx->curl_handle, &req, target, "", &auth, 1);
}
else
{
requests_delete(ctx->curl_handle, &req, target, json_dumps(contents, 0), &auth, 1);
}

free(target);
free(auth);

if(!req.ok)
{
_ld_set_last_rest_error(ctx, &req);
return NULL;
}

json_t *res;
json_error_t err;

res = json_loads(req.text, 0, &err);

requests_close(&req);

if (!res)
{
log_error("failed to parse json on line %d: %s", err.line, err.text);
return NULL;
}

return res;
}

char *ld_get_gateway(struct ld_context *ctx)
{
json_t *res = ld_get_request(ctx, "/gateway");
@@ -373,4 +418,24 @@ int ld_modify_channel_result(struct ld_context *ctx, char *channel_id, struct ld

return 0;

}

int ld_delete_channel(struct ld_context *ctx, char *channel_id)
{
char *target = malloc(strlen("/channels/") + strlen(channel_id));

sprintf(target, "/channels/%s", channel_id);

json_t *res = ld_delete_request(ctx, target, NULL);

if (!res)
{
log_error("probably failed to delete channel");
return -1;
}

free(target);
json_decref(res);

return 0;
}

Laden…
Annuleren
Opslaan