Be more verbose on HTTP error messages
[nci.git] / nci-create-assignment.c
blob64a80c04790aa35e53eb5fe3827eee65ed54d0a7
1 /*
2 * Copyright (c) 2016, 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
7 * in all copies.
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.
18 #include <errno.h>
19 #include <locale.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
25 #include <curl/curl.h>
26 #include <yajl_parse.h>
28 #include "macros.h"
29 #include "util.h"
31 int main(int argc, char **argv)
33 int ret = EINVAL;
34 char *url_base = 0;
35 char *auth_token = 0;
36 char *course_id = 0;
37 char *name = 0;
38 char *max_points_arg = 0;
39 double max_points_d = 0;
40 char *max_points = 0;
41 char *due_date = 0;
42 char *group_id = 0;
43 size_t len = 0;
44 char *built_uri = 0;
45 struct curl_httppost *post = 0;
46 char *new_id = 0;
47 int opt = 0;
49 setlocale(LC_ALL, "");
51 while ((opt = getopt(argc, argv, "c:n:m:d:g:")) != -1) {
52 switch (opt) {
53 case 'c':
54 course_id = optarg;
55 break;
56 case 'n':
57 name = optarg;
58 break;
59 case 'm':
60 max_points_arg = optarg;
61 break;
62 case 'd':
63 due_date = optarg;
64 break;
65 case 'g':
66 group_id = optarg;
67 break;
68 default:
69 break;
73 if (!course_id) {
74 ret = EINVAL;
75 fprintf(stderr, "course-id is mandatory\n");
76 goto cleanup;
79 if (!name) {
80 ret = EINVAL;
81 fprintf(stderr, "name is mandatory\n");
82 goto cleanup;
85 if (!max_points_arg) {
86 ret = EINVAL;
87 fprintf(stderr, "max-points is mandatory\n");
88 goto cleanup;
89 } else {
90 max_points_d = strtod(max_points_arg, 0);
91 len = snprintf(0, 0, "%lf", max_points_d);
93 if (!(max_points = malloc(len + 1))) {
94 ret = errno;
95 perror(L("malloc"));
96 goto cleanup;
99 sprintf(max_points, "%lf", max_points_d);
102 curl_global_init(CURL_GLOBAL_DEFAULT);
104 if (!(url_base = get_url_base())) {
105 ret = ENOENT;
107 /* Error should have already been printed */
108 goto cleanup;
111 if (!(auth_token = get_auth_token())) {
112 ret = ENOENT;
114 /* Error should have already been printed */
115 goto cleanup;
118 len = snprintf(0, 0, "%s/api/v1/courses/%s/assignments", url_base,
119 course_id);
121 if (!(built_uri = malloc(len + 1))) {
122 ret = errno;
123 perror(L("malloc"));
124 goto cleanup;
127 sprintf(built_uri, "%s/api/v1/courses/%s/assignments", url_base,
128 course_id);
130 if (!(post = make_assignment_post(name, max_points, due_date,
131 group_id))) {
132 /* XXX: pass back proper error code from make_assignment_post */
133 goto cleanup;
136 if ((ret = send_and_id_scan(built_uri, auth_token, post, "POST",
137 &new_id))) {
138 goto cleanup;
141 if (new_id) {
142 printf("%s\n", new_id);
145 ret = 0;
146 cleanup:
147 curl_formfree(post);
148 free(new_id);
149 free(built_uri);
150 free(max_points);
151 free(url_base);
152 free(auth_token);
153 curl_global_cleanup();
155 return ret;