Don't print an error message for empty courses
[nci.git] / nci-edit-assignment-group.c
blobfe1856fc79fa59f3d2add62700d792e66a5ad9e7
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 *group_id = 0;
39 char *name = 0;
40 char *weight_arg = 0;
41 double weight_d = 0;
42 char *weight = 0;
43 char *drop_lowest_arg = 0;
44 long long drop_lowest_ll = 0;
45 char *drop_lowest = 0;
46 char *drop_highest_arg = 0;
47 long long drop_highest_ll = 0;
48 char *drop_highest = 0;
49 size_t len = 0;
50 char *built_uri = 0;
51 struct curl_httppost *post = 0;
52 int opt = 0;
54 setlocale(LC_ALL, "");
56 while ((opt = getopt(argc, argv, "g:c:n:w:d:D:")) != -1) {
57 switch (opt) {
58 case 'c':
59 course_id = optarg;
60 break;
61 case 'g':
62 group_id = optarg;
63 break;
64 case 'n':
65 name = optarg;
66 break;
67 case 'w':
68 weight_arg = optarg;
69 break;
70 case 'd':
71 drop_lowest_arg = optarg;
72 break;
73 case 'D':
74 drop_highest_arg = optarg;
75 break;
76 default:
77 break;
81 if (!course_id) {
82 ret = EINVAL;
83 fprintf(stderr, "course-id is mandatory\n");
84 goto cleanup;
87 if (!group_id) {
88 ret = EINVAL;
89 fprintf(stderr, "group-id is mandatory\n");
90 goto cleanup;
93 if (weight_arg) {
94 weight_d = strtod(weight_arg, 0);
95 len = snprintf(0, 0, "%lf", weight_d);
97 if (len + 1 < len) {
98 ret = errno = EOVERFLOW;
99 perror(L(""));
100 goto cleanup;
103 if (!(weight = malloc(len + 1))) {
104 ret = errno;
105 perror(L("malloc"));
106 goto cleanup;
109 sprintf(weight, "%lf", weight_d);
112 if (drop_lowest_arg) {
113 drop_lowest_ll = strtoll(drop_lowest_arg, 0, 0);
114 len = snprintf(0, 0, "%lld", drop_lowest_ll);
116 if (len + 1 < len) {
117 ret = errno = EOVERFLOW;
118 perror(L(""));
119 goto cleanup;
122 if (!(drop_lowest = malloc(len + 1))) {
123 ret = errno;
124 perror(L("malloc"));
125 goto cleanup;
128 sprintf(drop_lowest, "%lld", drop_lowest_ll);
131 if (drop_highest_arg) {
132 drop_highest_ll = strtoll(drop_highest_arg, 0, 0);
133 len = snprintf(0, 0, "%lld", drop_highest_ll);
135 if (len + 1 < len) {
136 ret = errno = EOVERFLOW;
137 perror(L(""));
138 goto cleanup;
141 if (!(drop_highest = malloc(len + 1))) {
142 ret = errno;
143 perror(L("malloc"));
144 goto cleanup;
147 sprintf(drop_highest, "%lld", drop_highest_ll);
150 curl_global_init(CURL_GLOBAL_DEFAULT);
152 if (!(url_base = get_url_base())) {
153 ret = ENOENT;
155 /* Error should have already been printed */
156 goto cleanup;
159 if (!(auth_token = get_auth_token())) {
160 ret = ENOENT;
162 /* Error should have already been printed */
163 goto cleanup;
166 len = snprintf(0, 0, "%s/api/v1/courses/%s/assignment_groups/%s",
167 url_base, course_id, group_id);
169 if (len + 1 < len) {
170 ret = errno = EOVERFLOW;
171 perror(L(""));
172 goto cleanup;
175 if (!(built_uri = malloc(len + 1))) {
176 ret = errno;
177 perror(L("malloc"));
178 goto cleanup;
181 sprintf(built_uri, "%s/api/v1/courses/%s/assignment_groups/%s",
182 url_base, course_id, group_id);
184 if (!(post = make_agroup_post(name, weight, drop_lowest,
185 drop_highest))) {
186 /* XXX: pass back proper error code from make_agroup_post() */
187 goto cleanup;
190 if ((ret = send_and_id_scan(built_uri, auth_token, post, "PUT", 0))) {
191 goto cleanup;
194 ret = 0;
195 cleanup:
196 curl_formfree(post);
197 free(built_uri);
198 free(drop_highest);
199 free(drop_lowest);
200 free(weight);
201 free(url_base);
202 free(auth_token);
203 curl_global_cleanup();
205 return ret;