Browse Source

Delete channel request

Alexander Memer 10 months ago
parent
commit
403ece68b4
5 changed files with 123 additions and 0 deletions
  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 View File

@@ -114,5 +114,7 @@ int main(int argc, char **argv)
114 114
 
115 115
     free(m);
116 116
 
117
+    ld_delete_channel(ctx, "482874514124832779");
118
+
117 119
     return 0;
118 120
 }

+ 1
- 0
include/discord.h View File

@@ -266,6 +266,7 @@ int ld_create_message(struct ld_context *ctx, char *channel_id, char *content);
266 266
 int ld_get_channel(struct ld_context *ctx, char *channel_id, guild_channel_t *target);
267 267
 int ld_modify_channel_result(struct ld_context *ctx, char *channel_id, struct ld_modify_channel_obj *modify, guild_channel_t *result);
268 268
 int ld_modify_channel(struct ld_context *ctx, char *channel_id, struct ld_modify_channel_obj *modify);
269
+int ld_delete_channel(struct ld_context *ctx, char *channel_id);
269 270
 
270 271
 void ld_channel_obj_to_type(json_t *src, guild_channel_t *channel);
271 272
 

+ 2
- 0
include/requests.h View File

@@ -60,6 +60,8 @@ CURLcode requests_put_headers(CURL *curl, req_t *req, char *url, char *data,
60 60
                               char **custom_hdrv, int custom_hdrc);
61 61
 CURLcode requests_patch(CURL *curl, req_t *req, char *url, char *data,
62 62
                                char **custom_hdrv, int custom_hdrc, int is_json);
63
+CURLcode requests_delete(CURL *curl, req_t *req, char *url, char *data,
64
+                         char **custom_hdrv, int custom_hdrc);
63 65
 char *requests_url_encode(CURL *curl, char **data, int data_size);
64 66
 
65 67
 #endif

+ 53
- 0
lib/requests.c View File

@@ -88,6 +88,7 @@ void requests_close(req_t *req)
88 88
     for (int i = 0; i < req->resp_hdrc; i++)
89 89
         free(req->resp_hdrv[i]);
90 90
 
91
+
91 92
     for (int i = 0; i < req->req_hdrc; i++)
92 93
         free(req->req_hdrv[i]);
93 94
 
@@ -443,6 +444,58 @@ CURLcode requests_patch(CURL *curl, req_t *req, char *url, char *data,
443 444
     return rc;
444 445
 }
445 446
 
447
+CURLcode requests_delete(CURL *curl, req_t *req, char *url, char *data,
448
+                            char **custom_hdrv, int custom_hdrc)
449
+{
450
+    CURLcode rc;
451
+    struct curl_slist *slist = NULL;
452
+    req->url = url;
453
+
454
+    /* body data */
455
+    if (data != NULL) {
456
+        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
457
+    } else {
458
+        /* content length header defaults to -1, which causes request to fail
459
+           sometimes, so we need to manually set it to 0 */
460
+        char *cl_header = "Content-Length: 0";
461
+        slist = curl_slist_append(slist, cl_header);
462
+        if (slist == NULL)
463
+            return -1;
464
+        if (custom_hdrv == NULL)
465
+            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
466
+
467
+        hdrv_append(&req->req_hdrv, &req->req_hdrc, cl_header);
468
+    }
469
+
470
+    /* headers */
471
+    if (custom_hdrv != NULL) {
472
+        rc = process_custom_headers(&slist, req, custom_hdrv, custom_hdrc);
473
+        if (rc != CURLE_OK)
474
+            return rc;
475
+        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
476
+    }
477
+
478
+    common_opt(curl, req);
479
+    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
480
+    char *ua = user_agent();
481
+    curl_easy_setopt(curl, CURLOPT_USERAGENT, ua);
482
+    rc = curl_easy_perform(curl);
483
+    if (rc != CURLE_OK)
484
+        return rc;
485
+
486
+    long code;
487
+    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
488
+    req->code = code;
489
+    req->ok = check_ok(code);
490
+
491
+    if (slist != NULL)
492
+        curl_slist_free_all(slist);
493
+    free(ua);
494
+    curl_easy_cleanup(curl);
495
+
496
+    return rc;
497
+}
498
+
446 499
 /*
447 500
  * process_custom_headers - Adds custom headers to request and populates the
448 501
  * req_headerv and req_hdrc fields of the request struct using the supplied

+ 65
- 0
lib/rest.c View File

@@ -172,6 +172,51 @@ json_t *ld_patch_request(struct ld_context *ctx, char *url, json_t *contents)
172 172
     return res;
173 173
 }
174 174
 
175
+json_t *ld_delete_request(struct ld_context *ctx, char *url, json_t *contents)
176
+{
177
+    req_t req;
178
+    ctx->curl_handle = requests_init(&req);
179
+
180
+    char *target = malloc(strlen(LD_API_ENDPOINT) + strlen(url) + 1);
181
+    char *auth = malloc(strlen("Authorization: Bot ") + strlen(ctx->bot_token) + 1);
182
+
183
+    sprintf(target, "%s%s", LD_API_ENDPOINT, url);
184
+    sprintf(auth, "%s%s", "Authorization: Bot ", ctx->bot_token);
185
+
186
+    if (!contents)
187
+    {
188
+        requests_delete(ctx->curl_handle, &req, target, "", &auth, 1);
189
+    }
190
+    else
191
+    {
192
+        requests_delete(ctx->curl_handle, &req, target, json_dumps(contents, 0), &auth, 1);
193
+    }
194
+
195
+    free(target);
196
+    free(auth);
197
+
198
+    if(!req.ok)
199
+    {
200
+        _ld_set_last_rest_error(ctx, &req);
201
+        return NULL;
202
+    }
203
+
204
+    json_t *res;
205
+    json_error_t err;
206
+
207
+    res = json_loads(req.text, 0, &err);
208
+
209
+    requests_close(&req);
210
+
211
+    if (!res)
212
+    {
213
+        log_error("failed to parse json on line %d: %s", err.line, err.text);
214
+        return NULL;
215
+    }
216
+
217
+    return res;
218
+}
219
+
175 220
 char *ld_get_gateway(struct ld_context *ctx)
176 221
 {
177 222
     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
373 418
 
374 419
     return 0;
375 420
 
421
+}
422
+
423
+int ld_delete_channel(struct ld_context *ctx, char *channel_id)
424
+{
425
+    char *target = malloc(strlen("/channels/") + strlen(channel_id));
426
+
427
+    sprintf(target, "/channels/%s", channel_id);
428
+
429
+    json_t *res = ld_delete_request(ctx, target, NULL);
430
+
431
+    if (!res)
432
+    {
433
+        log_error("probably failed to delete channel");
434
+        return -1;
435
+    }
436
+
437
+    free(target);
438
+    json_decref(res);
439
+
440
+    return 0;
376 441
 }

Loading…
Cancel
Save