-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathgithub-gist.c
97 lines (82 loc) · 2.57 KB
/
github-gist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#define _GNU_SOURCE /* asprintf() */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "cee-utils.h"
#include "cee-utils/ntl.h"
#include "json-actor.h"
#include "github.h"
#include "github-internal.h"
ORCAcode
github_create_gist(struct github *client, struct github_gist_create_params *params, struct github_gist *gist)
{
log_info("===create-gist===");
if (!params->description) {
log_error("Missing 'description'");
return ORCA_MISSING_PARAMETER;
}
if (!params->title) {
log_error("Missing 'title'");
return ORCA_MISSING_PARAMETER;
}
if (!params->contents) {
log_error("Missing 'contents'");
return ORCA_MISSING_PARAMETER;
}
char payload[4096];
char fmt[2048];
/* Create the format string for the payload
* TODO:
* Allocate buffer big enough, then free it after the request is made
* */
snprintf(fmt, sizeof(fmt), "(public): \"%s\", (description): \"%s\", (files): { (%s): { (content): \"%s\" }}", params->public,
params->description,
params->title,
params->contents);
size_t ret = json_inject(payload, sizeof(payload), fmt);
return github_adapter_run(
&client->adapter,
&(struct ua_resp_handle){
.ok_cb = &github_gist_from_json_v,
.ok_obj = &gist
},
&(struct sized_buffer){ payload, ret },
HTTP_POST,
"/gists");
}
ORCAcode
github_get_gist(struct github *client, char *id, struct github_gist *gist) {
log_info("===get-a-gist===");
if (!id) {
log_error("Missing 'id'");
return ORCA_MISSING_PARAMETER;
}
if (!gist) {
log_error("Missing 'gist'");
return ORCA_MISSING_PARAMETER;
}
return github_adapter_run(
&client->adapter,
&(struct ua_resp_handle){
.ok_cb = &github_gist_from_json_v,
.ok_obj = &gist
},
NULL,
HTTP_GET,
"/gists/%s", id);
}
ORCAcode
github_gist_is_starred(struct github *client, char *id) {
log_info("===gist-is-starred===");
if (!id) {
log_error("Missing 'id'");
return ORCA_MISSING_PARAMETER;
}
return github_adapter_run(
&client->adapter,
NULL,
NULL,
HTTP_GET,
"/gists/%s/star", id);
}