Update copyright dates
[src/app-framework-binder.git] / bindings / samples / DemoPost.c
1 /*
2  * Copyright (C) 2015-2020 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <string.h>
21 #include <json-c/json.h>
22
23 #define AFB_BINDING_VERSION 3
24 #include <afb/afb-binding.h>
25
26
27 // Sample Generic Ping Debug API
28 static void getPingTest(afb_req_t request)
29 {
30     static int pingcount = 0;
31     json_object *query = afb_req_json(request);
32
33     afb_req_success_f(request, query, "Ping Binder Daemon count=%d", ++pingcount);
34 }
35
36 // With content-type=json data are directly avaliable in request->post->data
37 static void GetJsonByPost (afb_req_t request)
38 {
39     struct afb_arg arg;
40     json_object* jresp;
41     json_object *query = afb_req_json(request);
42
43     arg = afb_req_get(request, "");
44     jresp = arg.value ? json_tokener_parse(arg.value) : NULL;
45     afb_req_success_f(request, jresp, "GetJsonByPost query={%s}", json_object_to_json_string(query));
46 }
47
48 // Upload a file and execute a function when upload is done
49 static void Uploads (afb_req_t request, const char *destination)
50 {
51    struct afb_arg a = afb_req_get(request, "file");
52    if (a.value == NULL || *a.value == 0)
53      afb_req_fail(request, "failed", "no file selected");
54    else
55      afb_req_success_f(request, NULL, "uploaded file %s of path %s for destination %s", a.value, a.path, destination);
56 }
57
58 // Upload a file and execute a function when upload is done
59 static void UploadAppli (afb_req_t request)
60 {
61     Uploads(request, "applications");
62 }
63
64 // Simples Upload case just upload a file
65 static void UploadMusic (afb_req_t request)
66 {
67     Uploads(request, "musics");
68 }
69
70 // PostForm callback is called multiple times (one or each key within form, or once per file buffer)
71 // When file has been fully uploaded call is call with item==NULL
72 static void UploadImage (afb_req_t request)
73 {
74     Uploads(request, "images");
75 }
76
77
78 // NOTE: this sample does not use session to keep test a basic as possible
79 //       in real application upload-xxx should be protected with AFB_SESSION_CHECK
80 static const struct afb_verb_v3 verbs[]= {
81   {.verb="ping"         , .session=AFB_SESSION_NONE  , .callback=getPingTest    ,.info="Ping Rest Test Service"},
82   {.verb="upload-json"  , .session=AFB_SESSION_NONE  , .callback=GetJsonByPost  ,.info="Demo for Json Buffer on Post"},
83   {.verb="upload-image" , .session=AFB_SESSION_NONE  , .callback=UploadImage    ,.info="Demo for file upload"},
84   {.verb="upload-music" , .session=AFB_SESSION_NONE  , .callback=UploadMusic    ,.info="Demo for file upload"},
85   {.verb="upload-appli" , .session=AFB_SESSION_NONE  , .callback=UploadAppli    ,.info="Demo for file upload"},
86   {.verb=NULL}
87 };
88
89 const struct afb_binding_v3 afbBindingV3 = {
90         .info = "Sample with Post Upload Files",
91         .api = "post",
92         .verbs = verbs
93 };
94