-
Notifications
You must be signed in to change notification settings - Fork 0
/
punkapi.c
212 lines (179 loc) · 4.83 KB
/
punkapi.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <curl/curl.h>
#include <jzon.h>
#ifdef VERSION
static const char *version = VERSION;
#else
static const char *version = "UNKNOWN";
#endif
/* operational flags */
struct flags {
unsigned int random;
unsigned int page;
unsigned int items;
unsigned int insecure;
};
struct flags flags = {
.random = 0,
.page = 1,
.items = 25,
.insecure = 0
};
/* dynamic buffer */
struct buffer {
size_t size;
char *data;
};
void help(void)
{
const char *usage =
"Usage: punkapi [options]\n\n"
"OPTIONS:\n"
" -r \tGet a random beer\n"
" -p <page> \tGet all beers at the given page\n"
" -i <items>\tSet the number of beers per page\n"
" -k \tUse insecure connection\n";
fprintf(stdout, "PunkAPI %s\n\n%s", version, usage);
}
size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
size_t realsize = size * nmemb;
struct buffer *buffer = userdata;
buffer->data = realloc(buffer->data, buffer->size + realsize);
if (!buffer->data) {
return 0;
}
memcpy(buffer->data + buffer->size, ptr, realsize);
buffer->size += realsize;
return realsize;
}
int perform_api_request(const char *endpoint, struct buffer *buffer,
char *errbuf)
{
/* initialize cURL */
CURL *curl = curl_easy_init();
if (!curl) {
return -1;
}
/* set cURL options */
curl_easy_setopt(curl, CURLOPT_URL, endpoint);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, buffer);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
if (flags.insecure) {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
}
/* perform cURL request */
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
curl_easy_cleanup(curl);
return -1;
}
/* cleanup cURL */
curl_easy_cleanup(curl);
return buffer->size;
}
void print_beers(struct jzon *jzon)
{
if (jzon_is_array(jzon, NULL)) {
for (int i = 0; i < jzon_array_size(jzon, NULL); i++) {
struct jzon *beer = jzon_array_get(jzon, i, NULL);
fprintf(stdout, "%s\n", jzon_object_get(beer, "name", NULL)->string);
}
}
}
int main(int argc, char **argv)
{
/* PunkAPI JSON API HTTP endpoint */
const char *punkapi_endpoint = "api.punkapi.com/v2/beers";
/* parse command line */
int ch;
while ((ch = getopt(argc, argv, "rp:i:hk")) != -1) {
switch (ch) {
case 'r':
flags.random = 1;
break;
case 'p':
flags.page = atoi(optarg);
break;
case 'i':
flags.items = atoi(optarg);
break;
case 'h':
help();
return EXIT_SUCCESS;
case 'k':
flags.insecure = 1;
break;
default:
help();
return EXIT_FAILURE;
}
}
/* receive buffer */
struct buffer buffer;
buffer.size = 0;
buffer.data = NULL;
/* prepare request URL */
char *request_url = NULL;
if (flags.random) {
request_url =
calloc(
8 + strlen(punkapi_endpoint) + 7 + 1,
sizeof(char)
);
sprintf(
request_url,
"https://%s/random",
punkapi_endpoint
);
} else {
request_url =
calloc(
8 + strlen(punkapi_endpoint) + 22 + 1,
sizeof(char)
);
sprintf(
request_url,
"https://%s?page=%d&per_page=%d",
punkapi_endpoint,
flags.page,
flags.items
);
}
/* perform API request */
char errbuf[CURL_ERROR_SIZE];
errbuf[0] = '\0';
if (perform_api_request(request_url, &buffer, errbuf) == -1) {
fprintf(stderr, "ERROR: %s\n", errbuf);
if (buffer.data) {
free(buffer.data);
}
free(request_url);
return EXIT_FAILURE;
}
/* cleanup request URL */
free(request_url);
/* make buffer data a string */
buffer.data = realloc(buffer.data, buffer.size + 1);
buffer.data[buffer.size] = '\0';
buffer.size += 1;
/* JZON parse API response */
struct jzon *jzon = jzon_parse(buffer.data, NULL);
if (!jzon) {
fprintf(stderr, "ERROR: %s\n", "JSON data could not get parsed");
free(buffer.data);
return EXIT_FAILURE;
}
/* cleanup buffer */
free(buffer.data);
/* print beers */
print_beers(jzon);
/* cleanup JZON */
jzon_free(jzon);
return EXIT_SUCCESS;
}