2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Fulup Ar Foll"
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "afb-plugin.h"
25 #include "afb-req-itf.h"
28 // Sample Generic Ping Debug API
29 static void getPingTest(struct afb_req request)
31 static int pingcount = 0;
32 json_object *query = afb_req_json(request);
34 afb_req_success_f(request, query, "Ping Binder Daemon count=%d", ++pingcount);
37 // With content-type=json data are directly avaliable in request->post->data
38 static void GetJsonByPost (struct afb_req request)
42 json_object *query = afb_req_json(request);
44 arg = afb_req_get(request, "");
45 jresp = arg.value ? json_tokener_parse(arg.value) : NULL;
46 afb_req_success_f(request, jresp, "GetJsonByPost query={%s}", json_object_to_json_string(query));
49 // Upload a file and execute a function when upload is done
50 static void Uploads (struct afb_req request, const char *destination)
52 afb_req_fail_f(request, "unimplemented", "destination: %s", destination);
55 // Upload a file and execute a function when upload is done
56 static void UploadAppli (struct afb_req request)
58 Uploads(request, "applications");
61 // Simples Upload case just upload a file
62 static void UploadMusic (struct afb_req request)
64 Uploads(request, "musics");
67 // PostForm callback is called multiple times (one or each key within form, or once per file buffer)
68 // When file has been fully uploaded call is call with item==NULL
69 static void UploadImage (struct afb_req request)
71 Uploads(request, "images");
75 // NOTE: this sample does not use session to keep test a basic as possible
76 // in real application upload-xxx should be protected with AFB_SESSION_CHECK
77 static const struct AFB_restapi pluginApis[]= {
78 {"ping" , AFB_SESSION_NONE , getPingTest ,"Ping Rest Test Service"},
79 {"upload-json" , AFB_SESSION_NONE , GetJsonByPost ,"Demo for Json Buffer on Post"},
80 {"upload-image" , AFB_SESSION_NONE , UploadImage ,"Demo for file upload"},
81 {"upload-music" , AFB_SESSION_NONE , UploadMusic ,"Demo for file upload"},
82 {"upload-appli" , AFB_SESSION_NONE , UploadAppli ,"Demo for file upload"},
86 static const struct AFB_plugin plugin_desc = {
87 .type = AFB_PLUGIN_JSON,
88 .info = "Sample with Post Upload Files",
93 const struct AFB_plugin *pluginRegister (const struct AFB_interface *itf)