refactoring context handling
[src/app-framework-binder.git] / include / afb-req-itf.h
1 /*
2  * Copyright 2016 IoT.bzh
3  * Author: José Bollo <jose.bollo@iot.bzh>
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 #pragma once
19
20 struct json_object;
21
22 struct afb_arg {
23         const char *name;
24         const char *value;
25         const char *path;
26 };
27
28 struct afb_req_itf {
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);
40 };
41
42 struct afb_req {
43         const struct afb_req_itf *itf;
44         void *req_closure;
45         void *ctx_closure;
46 };
47 static inline struct afb_arg afb_req_get(struct afb_req req, const char *name)
48 {
49         return req.itf->get(req.req_closure, name);
50 }
51
52 static inline const char *afb_req_value(struct afb_req req, const char *name)
53 {
54         return afb_req_get(req, name).value;
55 }
56
57 static inline const char *afb_req_path(struct afb_req req, const char *name)
58 {
59         return afb_req_get(req, name).path;
60 }
61
62 static inline struct json_object *afb_req_json(struct afb_req req)
63 {
64         return req.itf->json(req.req_closure);
65 }
66
67 static inline void afb_req_success(struct afb_req req, struct json_object *obj, const char *info)
68 {
69         req.itf->success(req.req_closure, obj, info);
70 }
71
72 static inline void afb_req_fail(struct afb_req req, const char *status, const char *info)
73 {
74         req.itf->fail(req.req_closure, status, info);
75 }
76
77 static inline const char *afb_req_raw(struct afb_req req, size_t *size)
78 {
79         return req.itf->raw(req.req_closure, size);
80 }
81
82 static inline void afb_req_send(struct afb_req req, char *buffer, size_t size)
83 {
84         req.itf->send(req.req_closure, buffer, size);
85 }
86
87 static inline void *afb_req_context_get(struct afb_req req)
88 {
89         return req.itf->context_get(req.ctx_closure);
90 }
91
92 static inline void afb_req_context_set(struct afb_req req, void *context, void (*free_context)(void*))
93 {
94         return req.itf->context_set(req.ctx_closure, context, free_context);
95 }
96
97 static inline void afb_req_context_clear(struct afb_req req)
98 {
99         afb_req_context_set(req, NULL, NULL);
100 }
101
102 static inline int afb_req_session_create(struct afb_req req)
103 {
104         int result = req.itf->session_create(req.req_closure);
105         if (!result)
106                 afb_req_fail(req, "fail", "Can't create the session");
107         return result;
108 }
109
110 static inline int afb_req_session_check(struct afb_req req, int refresh)
111 {
112         int result = req.itf->session_check(req.req_closure, refresh);
113         if (!result)
114                 afb_req_fail(req, "fail", "Token chek failed for the session");
115         return result;
116 }
117
118 static inline void afb_req_session_close(struct afb_req req)
119 {
120         req.itf->session_close(req.req_closure);
121 }
122
123 #if !defined(_GNU_SOURCE)
124 # error "_GNU_SOURCE must be defined for using vasprintf"
125 #endif
126
127 #include <stdarg.h>
128 #include <stdlib.h>
129 #include <stdio.h>
130
131 static inline void afb_req_fail_v(struct afb_req req, const char *status, const char *info, va_list args)
132 {
133         char *message;
134         if (info == NULL || vasprintf(&message, info, args) < 0)
135                 message = NULL;
136         afb_req_fail(req, status, message);
137         free(message);
138 }
139
140 static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...)
141 {
142         va_list args;
143         va_start(args, info);
144         afb_req_fail_v(req, status, info, args);
145         va_end(args);
146 }
147
148 static inline void afb_req_success_v(struct afb_req req, struct json_object *obj, const char *info, va_list args)
149 {
150         char *message;
151         if (info == NULL || vasprintf(&message, info, args) < 0)
152                 message = NULL;
153         afb_req_success(req, obj, message);
154         free(message);
155 }
156
157 static inline void afb_req_success_f(struct afb_req req, struct json_object *obj, const char *info, ...)
158 {
159         va_list args;
160         va_start(args, info);
161         afb_req_success_v(req, obj, info, args);
162         va_end(args);
163 }
164