Don't print an error message for empty courses
[nci.git] / nci-delete-assignment.c
blobdfa5c61309e804bda8ae2dcebd7c361e4df9ed5a
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 <string.h>
24 #include <unistd.h>
26 #include <curl/curl.h>
27 #include <yajl_parse.h>
29 #include "macros.h"
30 #include "util.h"
32 int
33 main(int argc, char **argv)
35 int ret = EINVAL;
36 char *url_base = 0;
37 char *auth_token = 0;
38 char *course_id = 0;
39 char *assignment_id = 0;
40 size_t len = 0;
41 char *built_uri = 0;
42 char *deleted_id = 0;
43 int opt = 0;
45 setlocale(LC_ALL, "");
47 while ((opt = getopt(argc, argv, "c:a:")) != -1) {
48 switch (opt) {
49 case 'c':
50 course_id = optarg;
51 break;
52 case 'a':
53 assignment_id = optarg;
54 break;
55 default:
56 break;
60 if (!course_id) {
61 ret = EINVAL;
62 fprintf(stderr, "course-id is mandatory\n");
63 goto cleanup;
66 if (!assignment_id) {
67 ret = EINVAL;
68 fprintf(stderr, "assignment-id is mandatory\n");
69 goto cleanup;
72 curl_global_init(CURL_GLOBAL_DEFAULT);
74 if (!(url_base = get_url_base())) {
75 ret = ENOENT;
77 /* Error should have already been printed */
78 goto cleanup;
81 if (!(auth_token = get_auth_token())) {
82 ret = ENOENT;
84 /* Error should have already been printed */
85 goto cleanup;
88 len = snprintf(0, 0, "%s/api/v1/courses/%s/assignments/%s", url_base,
89 course_id, assignment_id);
91 if (len + 1 < len) {
92 ret = errno = EOVERFLOW;
93 perror(L(""));
94 goto cleanup;
97 if (!(built_uri = malloc(len + 1))) {
98 ret = errno;
99 perror(L("malloc"));
100 goto cleanup;
103 sprintf(built_uri, "%s/api/v1/courses/%s/assignments/%s", url_base,
104 course_id, assignment_id);
106 if ((ret = send_and_id_scan(built_uri, auth_token, 0, "DELETE",
107 &deleted_id))) {
108 goto cleanup;
111 if (deleted_id) {
112 printf("%s\n", deleted_id);
115 ret = 0;
116 cleanup:
117 free(deleted_id);
118 free(built_uri);
119 free(url_base);
120 free(auth_token);
121 curl_global_cleanup();
123 return ret;