explicit use of json-c
[src/app-framework-binder.git] / plugins / samples / SamplePost.c
1 /*
2  * Copyright (C) 2015 "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 #include "afb-plugin.h"
24
25
26 // Sample Generic Ping Debug API
27 static void getPingTest(struct afb_req request)
28 {
29     static int pingcount = 0;
30     json_object *query = afb_req_json(request);
31
32     afb_req_success_f(request, query, "Ping Binder Daemon count=%d", ++pingcount);
33 }
34
35 // With content-type=json data are directly avaliable in request->post->data
36 static void GetJsonByPost (struct afb_req request)
37 {
38     struct afb_arg arg;
39     json_object* jresp;
40     json_object *query = afb_req_json(request);
41
42     arg = afb_req_get(request, "");
43     jresp = arg.value ? json_tokener_parse(arg.value) : NULL;
44     afb_req_success_f(request, jresp, "GetJsonByPost query={%s}", json_object_to_json_string(query));
45 }
46
47 // Upload a file and execute a function when upload is done
48 static void Uploads (struct afb_req request, const char *destination)
49 {
50    afb_req_fail_f(request, "unimplemented", "destination: %s", destination);
51 }
52
53 // Upload a file and execute a function when upload is done
54 static void UploadAppli (struct afb_req request)
55 {
56     Uploads(request, "applications");
57 }
58
59 // Simples Upload case just upload a file
60 static void UploadMusic (struct afb_req request)
61 {
62     Uploads(request, "musics");
63 }
64
65 // PostForm callback is called multiple times (one or each key within form, or once per file buffer)
66 // When file has been fully uploaded call is call with item==NULL 
67 static void UploadImage (struct afb_req request)
68 {
69     Uploads(request, "images");
70 }
71
72
73 // NOTE: this sample does not use session to keep test a basic as possible
74 //       in real application upload-xxx should be protected with AFB_SESSION_CHECK
75 static const struct AFB_restapi pluginApis[]= {
76   {"ping"         , AFB_SESSION_NONE  , getPingTest    ,"Ping Rest Test Service"},
77   {"upload-json"  , AFB_SESSION_NONE  , GetJsonByPost  ,"Demo for Json Buffer on Post"},
78   {"upload-image" , AFB_SESSION_NONE  , UploadImage    ,"Demo for file upload"},
79   {"upload-music" , AFB_SESSION_NONE  , UploadMusic    ,"Demo for file upload"},
80   {"upload-appli" , AFB_SESSION_NONE  , UploadAppli    ,"Demo for file upload"},
81   {NULL}
82 };
83
84 static const struct AFB_plugin plugin_desc = {
85         .type = AFB_PLUGIN_JSON,
86         .info = "Sample with Post Upload Files",
87         .prefix = "post",
88         .apis = pluginApis
89 };
90
91 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)
92 {
93     return &plugin_desc;
94 };