2 * Copyright 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.
29 struct json_object *(*json)(void *req_closure);
30 struct afb_arg (*get)(void *req_closure, const char *name);
31 void (*success)(void *req_closure, struct json_object *obj, const char *info);
32 void (*fail)(void *req_closure, const char *status, const char *info);
33 const char *(*raw)(void *req_closure, size_t *size);
34 void (*send)(void *req_closure, char *buffer, size_t size);
35 void *(*context_get)(void *ctx_closure);
36 void (*context_set)(void *ctx_closure, void *context, void (*free_context)(void*));
37 int (*session_create)(void *req_closure);
38 int (*session_check)(void *req_closure, int refresh);
39 void (*session_close)(void *req_closure);
43 const struct afb_req_itf *itf;
47 static inline struct afb_arg afb_req_get(struct afb_req req, const char *name)
49 return req.itf->get(req.req_closure, name);
52 static inline const char *afb_req_value(struct afb_req req, const char *name)
54 return afb_req_get(req, name).value;
57 static inline const char *afb_req_path(struct afb_req req, const char *name)
59 return afb_req_get(req, name).path;
62 static inline struct json_object *afb_req_json(struct afb_req req)
64 return req.itf->json(req.req_closure);
67 static inline void afb_req_success(struct afb_req req, struct json_object *obj, const char *info)
69 req.itf->success(req.req_closure, obj, info);
72 static inline void afb_req_fail(struct afb_req req, const char *status, const char *info)
74 req.itf->fail(req.req_closure, status, info);
77 static inline const char *afb_req_raw(struct afb_req req, size_t *size)
79 return req.itf->raw(req.req_closure, size);
82 static inline void afb_req_send(struct afb_req req, char *buffer, size_t size)
84 req.itf->send(req.req_closure, buffer, size);
87 static inline void *afb_req_context_get(struct afb_req req)
89 return req.itf->context_get(req.ctx_closure);
92 static inline void afb_req_context_set(struct afb_req req, void *context, void (*free_context)(void*))
94 return req.itf->context_set(req.ctx_closure, context, free_context);
97 static inline void afb_req_context_clear(struct afb_req req)
99 afb_req_context_set(req, NULL, NULL);
102 static inline int afb_req_session_create(struct afb_req req)
104 int result = req.itf->session_create(req.req_closure);
106 afb_req_fail(req, "fail", "Can't create the session");
110 static inline int afb_req_session_check(struct afb_req req, int refresh)
112 int result = req.itf->session_check(req.req_closure, refresh);
114 afb_req_fail(req, "fail", "Token chek failed for the session");
118 static inline void afb_req_session_close(struct afb_req req)
120 req.itf->session_close(req.req_closure);
123 #if !defined(_GNU_SOURCE)
124 # error "_GNU_SOURCE must be defined for using vasprintf"
131 static inline void afb_req_fail_v(struct afb_req req, const char *status, const char *info, va_list args)
134 if (info == NULL || vasprintf(&message, info, args) < 0)
136 afb_req_fail(req, status, message);
140 static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...)
143 va_start(args, info);
144 afb_req_fail_v(req, status, info, args);
148 static inline void afb_req_success_v(struct afb_req req, struct json_object *obj, const char *info, va_list args)
151 if (info == NULL || vasprintf(&message, info, args) < 0)
153 afb_req_success(req, obj, message);
157 static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...)
160 va_start(args, info);
161 afb_req_success_v(req, obj, info, args);