2 * Copyright (c) 2016-2018 S. Gilles <sgilles@math.umd.edu>
4 * Permission to use, copy, modify, and/or distribute this software
5 * for any purpose with or without fee is hereby granted, provided
6 * that the above copyright notice and this permission notice appear
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
13 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
14 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include <curl/curl.h>
25 #include <yajl_parse.h>
30 static int list_courses(const char *url_base
, const char *auth_token
)
36 const char *fn_courses
[] = { "id", "name", 0 };
38 char **courses_ids
= 0;
40 size_t courses_num
= 0;
42 len
= snprintf(0, 0, "%s/api/v1/courses?per_page=9999", url_base
);
45 ret
= errno
= EOVERFLOW
;
50 if (!(built_uri
= malloc(len
+ 1))) {
56 sprintf(built_uri
, "%s/api/v1/courses?per_page=9999", url_base
);
58 if ((ret
= key_value_extract(built_uri
, auth_token
, fn_courses
, 0,
63 map_get_keys(&courses
, &courses_ids
, &courses_num
);
65 for (j
= 0; j
< courses_num
; ++j
) {
66 c
= map_get(&courses
, courses_ids
[j
]);
69 printf("%s %s\n", courses_ids
[j
], c
[0]);
87 setlocale(LC_ALL
, "");
88 curl_global_init(CURL_GLOBAL_DEFAULT
);
90 if (!(url_base
= get_url_base())) {
93 /* Error should have already been printed */
97 if (!(auth_token
= get_auth_token())) {
100 /* Error should have already been printed */
104 if ((ret
= list_courses(url_base
, auth_token
))) {
112 curl_global_cleanup();