Don't print an error message for empty courses
[nci.git] / nci-create-assignment.c
blobdd049237b7192d1b8cbf3131927de12bfb2a91dd
1 /*
2 * Copyright (c) 2016-2019 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
32 main(int argc, char **argv)
34 int ret = EINVAL;
35 char *url_base = 0;
36 char *auth_token = 0;
37 char *course_id = 0;
38 char *name = 0;
39 char *max_points_arg = 0;
40 double max_points_d = 0;
41 char *max_points = 0;
42 char *due_date = 0;
43 char *group_id = 0;
44 size_t len = 0;
45 char *built_uri = 0;
46 struct curl_httppost *post = 0;
47 char *new_id = 0;
48 int opt = 0;
50 setlocale(LC_ALL, "");
52 while ((opt = getopt(argc, argv, "c:n:m:d:g:")) != -1) {
53 switch (opt) {
54 case 'c':
55 course_id = optarg;
56 break;
57 case 'n':
58 name = optarg;
59 break;
60 case 'm':
61 max_points_arg = optarg;
62 break;
63 case 'd':
64 due_date = optarg;
65 break;
66 case 'g':
67 group_id = optarg;
68 break;
69 default:
70 break;
74 if (!course_id) {
75 ret = EINVAL;
76 fprintf(stderr, "course-id is mandatory\n");
77 goto cleanup;
80 if (!name) {
81 ret = EINVAL;
82 fprintf(stderr, "name is mandatory\n");
83 goto cleanup;
86 if (!max_points_arg) {
87 ret = EINVAL;
88 fprintf(stderr, "max-points is mandatory\n");
89 goto cleanup;
90 } else {
91 max_points_d = strtod(max_points_arg, 0);
92 len = snprintf(0, 0, "%lf", max_points_d);
94 if (len + 1 < len) {
95 ret = errno = EOVERFLOW;
96 perror(L(""));
97 goto cleanup;
100 if (!(max_points = malloc(len + 1))) {
101 ret = errno;
102 perror(L("malloc"));
103 goto cleanup;
106 sprintf(max_points, "%lf", max_points_d);
109 curl_global_init(CURL_GLOBAL_DEFAULT);
111 if (!(url_base = get_url_base())) {
112 ret = ENOENT;
114 /* Error should have already been printed */
115 goto cleanup;
118 if (!(auth_token = get_auth_token())) {
119 ret = ENOENT;
121 /* Error should have already been printed */
122 goto cleanup;
125 len = snprintf(0, 0, "%s/api/v1/courses/%s/assignments", url_base,
126 course_id);
128 if (len + 1 < len) {
129 ret = errno = EOVERFLOW;
130 perror(L(""));
131 goto cleanup;
134 if (!(built_uri = malloc(len + 1))) {
135 ret = errno;
136 perror(L("malloc"));
137 goto cleanup;
140 sprintf(built_uri, "%s/api/v1/courses/%s/assignments", url_base,
141 course_id);
143 if (!(post = make_assignment_post(name, max_points, due_date,
144 group_id))) {
145 /* XXX: pass back proper error code from make_assignment_post */
146 goto cleanup;
149 if ((ret = send_and_id_scan(built_uri, auth_token, post, "POST",
150 &new_id))) {
151 goto cleanup;
154 if (new_id) {
155 printf("%s\n", new_id);
158 ret = 0;
159 cleanup:
160 curl_formfree(post);
161 free(new_id);
162 free(built_uri);
163 free(max_points);
164 free(url_base);
165 free(auth_token);
166 curl_global_cleanup();
168 return ret;