comparison scid/page-api-todo.c @ 27:dae2de19ca5d

misc: switch to JSON everywhere
author David Demelier <markand@malikania.fr>
date Wed, 03 Aug 2022 15:18:09 +0200
parents 7e10cace67a3
children 695637f1d8a7
comparison
equal deleted inserted replaced
26:7e10cace67a3 27:dae2de19ca5d
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 17 */
18 18
19 #include <sys/types.h>
20 #include <assert.h> 19 #include <assert.h>
21 #include <stdarg.h> 20 #include <stdio.h>
22 #include <stdint.h>
23 #include <string.h>
24 21
25 #include <kcgi.h> 22 #include "crud.h"
26 #include <jansson.h>
27
28 #include "config.h"
29 #include "db.h" 23 #include "db.h"
30 #include "log.h"
31 #include "page-api-todo.h"
32 #include "page.h" 24 #include "page.h"
33 #include "types.h"
34 #include "util.h" 25 #include "util.h"
35
36 static void
37 list(struct kreq *r, const struct job *jobs, size_t jobsz)
38 {
39 json_t *doc;
40 char *dump;
41
42 doc = job_to(jobs, jobsz);
43 dump = json_dumps(doc, JSON_COMPACT);
44
45 khttp_puts(r, dump);
46 free(dump);
47 json_decref(doc);
48 }
49
50 #if 0
51
52 static int
53 save(const char *json)
54 {
55 struct jobresult res = {0};
56 int ret = -1;
57
58 json_t *doc;
59 json_error_t err;
60
61 if (!(doc = json_loads(json, 0, &err)))
62 log_warn("api/post: invalid JSON input: %s", err.text);
63 else if (jobresult_from(&res, 1, doc) < 0)
64 log_warn("api/post: failed to decode parameters");
65 else if (db_jobresult_add(&res) < 0)
66 log_warn("api/post: database save error");
67 else
68 ret = 0;
69
70 json_decref(doc);
71
72 return ret;
73 }
74
75 #endif
76 26
77 /* 27 /*
78 * GET /api/v1/todo/<worker-name> 28 * GET /api/v1/todo/<worker-name>
79 * ------------------------------ 29 * ------------------------------
80 * 30 *
81 * Retrieve a list of jobs to perform for this worker name. 31 * Retrieve a list of jobs to perform for this worker name.
82 */ 32 */
83 static void 33 static void
84 get(struct kreq *r) 34 get(struct kreq *r)
85 { 35 {
86 struct job jobs[SCI_JOB_LIST_MAX]; 36 crud_list(r, db_job_todo(util_basename(r->path)));
87 ssize_t jobsz;
88
89 if ((jobsz = db_job_todo(jobs, UTIL_SIZE(jobs), util_basename(r->path))) < 0)
90 page(r, KHTTP_500, KMIME_APP_JSON, NULL, NULL);
91 else {
92 khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_JSON]);
93 khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
94 khttp_body(r);
95 list(r, jobs, jobsz);
96 khttp_free(r);
97 }
98
99 for (ssize_t i = 0; i < jobsz; ++i)
100 job_finish(&jobs[i]);
101 } 37 }
102 38
103 void 39 void
104 page_api_v1_todo(struct kreq *r) 40 page_api_v1_todo(struct kreq *r)
105 { 41 {