2 * Copyright (C) 2015, 2016, 2017 "IoT.bzh"
3 * Author "Fulup Ar Foll"
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <json-c/json.h>
23 #include <afb/afb-binding.h>
26 // Sample Generic Ping Debug API
27 static void getPingTest(struct afb_req request)
29 static int pingcount = 0;
30 json_object *query = afb_req_json(request);
32 afb_req_success_f(request, query, "Ping Binder Daemon count=%d", ++pingcount);
35 // With content-type=json data are directly avaliable in request->post->data
36 static void GetJsonByPost (struct afb_req request)
40 json_object *query = afb_req_json(request);
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));
47 // Upload a file and execute a function when upload is done
48 static void Uploads (struct afb_req request, const char *destination)
50 struct afb_arg a = afb_req_get(request, "file");
51 if (a.value == NULL || *a.value == 0)
52 afb_req_fail(request, "failed", "no file selected");
54 afb_req_success_f(request, NULL, "uploaded file %s of path %s for destination %s", a.value, a.path, destination);
57 // Upload a file and execute a function when upload is done
58 static void UploadAppli (struct afb_req request)
60 Uploads(request, "applications");
63 // Simples Upload case just upload a file
64 static void UploadMusic (struct afb_req request)
66 Uploads(request, "musics");
69 // PostForm callback is called multiple times (one or each key within form, or once per file buffer)
70 // When file has been fully uploaded call is call with item==NULL
71 static void UploadImage (struct afb_req request)
73 Uploads(request, "images");
77 // NOTE: this sample does not use session to keep test a basic as possible
78 // in real application upload-xxx should be protected with AFB_SESSION_CHECK
79 static const struct afb_verb_desc_v1 verbs[]= {
80 {"ping" , AFB_SESSION_NONE , getPingTest ,"Ping Rest Test Service"},
81 {"upload-json" , AFB_SESSION_NONE , GetJsonByPost ,"Demo for Json Buffer on Post"},
82 {"upload-image" , AFB_SESSION_NONE , UploadImage ,"Demo for file upload"},
83 {"upload-music" , AFB_SESSION_NONE , UploadMusic ,"Demo for file upload"},
84 {"upload-appli" , AFB_SESSION_NONE , UploadAppli ,"Demo for file upload"},
88 static const struct afb_binding plugin_desc = {
89 .type = AFB_BINDING_VERSION_1,
91 .info = "Sample with Post Upload Files",
97 const struct afb_binding *afbBindingV1Register (const struct afb_binding_interface *itf)