2 * Copyright (C) 2016 "IoT.bzh"
3 * Author: José Bollo <jose.bollo@iot.bzh>
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.
20 #include <json-c/json.h>
22 #include <afb/afb-req-itf.h>
24 #include "afb-msg-json.h"
25 #include "afb-context.h"
28 struct json_object *afb_msg_json_reply(const char *status, const char *info, struct json_object *resp, struct afb_context *context, const char *reqid)
30 json_object *msg, *request;
31 const char *token, *uuid;
32 static json_object *type_reply = NULL;
34 msg = json_object_new_object();
36 json_object_object_add(msg, "response", resp);
38 if (type_reply == NULL)
39 type_reply = json_object_new_string("afb-reply");
40 json_object_object_add(msg, "jtype", json_object_get(type_reply));
42 request = json_object_new_object();
43 json_object_object_add(msg, "request", request);
44 json_object_object_add(request, "status", json_object_new_string(status));
47 json_object_object_add(request, "info", json_object_new_string(info));
50 json_object_object_add(request, "reqid", json_object_new_string(reqid));
52 if (context != NULL) {
53 token = afb_context_sent_token(context);
55 json_object_object_add(request, "token", json_object_new_string(token));
57 uuid = afb_context_sent_uuid(context);
59 json_object_object_add(request, "uuid", json_object_new_string(uuid));
65 struct json_object *afb_msg_json_reply_ok(const char *info, struct json_object *resp, struct afb_context *context, const char *reqid)
67 return afb_msg_json_reply("success", info, resp, context, reqid);
70 struct json_object *afb_msg_json_reply_error(const char *status, const char *info, struct afb_context *context, const char *reqid)
72 return afb_msg_json_reply(status, info, NULL, context, reqid);
75 struct json_object *afb_msg_json_event(const char *event, struct json_object *object)
78 static json_object *type_event = NULL;
80 msg = json_object_new_object();
82 json_object_object_add(msg, "event", json_object_new_string(event));
85 json_object_object_add(msg, "data", object);
87 if (type_event == NULL)
88 type_event = json_object_new_string("afb-event");
89 json_object_object_add(msg, "jtype", json_object_get(type_event));
94 struct afb_arg afb_msg_json_get_arg(struct json_object *object, const char *name)
97 struct json_object *value;
99 if (json_object_object_get_ex(object, name, &value)) {
101 arg.value = json_object_get_string(value);