aboutsummaryrefslogtreecommitdiffstats
path: root/bindings/tutorial
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2018-04-09 18:16:07 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2018-06-15 17:57:36 +0200
commit4521c1e7ae5371ab9d639adc617d17fb4e8ded0c (patch)
treea8a1416a2d58c16ab3993c7e4dc405fc71daab6a /bindings/tutorial
parent63682b4da9d3e892d1d0a671de860adc43068142 (diff)
api-v3: First draft
This commit introduces the bindings v3 API for bindings. The documentation has still to be improved and will come very soon. Change-Id: I8f9007370e29f671fdfd1da87fff7372a17db7af Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'bindings/tutorial')
-rw-r--r--bindings/tutorial/tuto-1.c10
-rw-r--r--bindings/tutorial/tuto-2.c40
-rw-r--r--bindings/tutorial/tuto-3.cpp28
3 files changed, 43 insertions, 35 deletions
diff --git a/bindings/tutorial/tuto-1.c b/bindings/tutorial/tuto-1.c
index 433a4ebb..1f58be6c 100644
--- a/bindings/tutorial/tuto-1.c
+++ b/bindings/tutorial/tuto-1.c
@@ -1,18 +1,18 @@
-#define AFB_BINDING_VERSION 2
+#define AFB_BINDING_VERSION 3
#include <afb/afb-binding.h>
-void hello(afb_req req)
+void hello(afb_req_t req)
{
AFB_REQ_DEBUG(req, "hello world");
- afb_req_success(req, NULL, "hello world");
+ afb_req_reply(req, NULL, NULL, "hello world");
}
-const afb_verb_v2 verbs[] = {
+const afb_verb_t verbs[] = {
{ .verb="hello", .callback=hello },
{ .verb=NULL }
};
-const afb_binding_v2 afbBindingV2 = {
+const afb_binding_t afbBindingExport = {
.api = "tuto-1",
.verbs = verbs
};
diff --git a/bindings/tutorial/tuto-2.c b/bindings/tutorial/tuto-2.c
index dc2d55ab..10797081 100644
--- a/bindings/tutorial/tuto-2.c
+++ b/bindings/tutorial/tuto-2.c
@@ -1,12 +1,12 @@
#include <string.h>
#include <json-c/json.h>
-#define AFB_BINDING_VERSION 2
+#define AFB_BINDING_VERSION 3
#include <afb/afb-binding.h>
-afb_event event_login, event_logout;
+afb_event_t event_login, event_logout;
-void login(afb_req req)
+void login(afb_req_t req)
{
json_object *args, *user, *passwd;
char *usr;
@@ -15,24 +15,24 @@ void login(afb_req req)
if (!json_object_object_get_ex(args, "user", &user)
|| !json_object_object_get_ex(args, "password", &passwd)) {
AFB_REQ_ERROR(req, "login, bad request: %s", json_object_get_string(args));
- afb_req_fail(req, "bad-request", NULL);
+ afb_req_reply(req, NULL, "bad-request", NULL);
} else if (afb_req_context_get(req)) {
AFB_REQ_ERROR(req, "login, bad state, logout first");
- afb_req_fail(req, "bad-state", NULL);
+ afb_req_reply(req, NULL, "bad-state", NULL);
} else if (strcmp(json_object_get_string(passwd), "please")) {
AFB_REQ_ERROR(req, "login, unauthorized: %s", json_object_get_string(args));
- afb_req_fail(req, "unauthorized", NULL);
+ afb_req_reply(req, NULL, "unauthorized", NULL);
} else {
usr = strdup(json_object_get_string(user));
AFB_REQ_NOTICE(req, "login user: %s", usr);
afb_req_session_set_LOA(req, 1);
afb_req_context_set(req, usr, free);
- afb_req_success(req, NULL, NULL);
+ afb_req_reply(req, NULL, NULL, NULL);
afb_event_push(event_login, json_object_new_string(usr));
}
}
-void action(afb_req req)
+void action(afb_req_t req)
{
json_object *args, *val;
char *usr;
@@ -51,10 +51,10 @@ void action(afb_req req)
afb_req_unsubscribe(req, event_logout);
}
}
- afb_req_success(req, json_object_get(args), NULL);
+ afb_req_reply(req, json_object_get(args), NULL, NULL);
}
-void logout(afb_req req)
+void logout(afb_req_t req)
{
char *usr;
@@ -63,34 +63,34 @@ void logout(afb_req req)
afb_event_push(event_logout, json_object_new_string(usr));
afb_req_session_set_LOA(req, 0);
afb_req_context_clear(req);
- afb_req_success(req, NULL, NULL);
+ afb_req_reply(req, NULL, NULL, NULL);
}
-int preinit()
+int preinit(afb_api_t api)
{
- AFB_NOTICE("preinit");
+ AFB_API_NOTICE(api, "preinit");
return 0;
}
-int init()
+int init(afb_api_t api)
{
- AFB_NOTICE("init");
- event_login = afb_daemon_make_event("login");
- event_logout = afb_daemon_make_event("logout");
+ AFB_API_NOTICE(api, "init");
+ event_login = afb_api_make_event(api, "login");
+ event_logout = afb_api_make_event(api, "logout");
if (afb_event_is_valid(event_login) && afb_event_is_valid(event_logout))
return 0;
- AFB_ERROR("Can't create events");
+ AFB_API_ERROR(api, "Can't create events");
return -1;
}
-const afb_verb_v2 verbs[] = {
+const afb_verb_t verbs[] = {
{ .verb="login", .callback=login },
{ .verb="action", .callback=action, .session=AFB_SESSION_LOA_1 },
{ .verb="logout", .callback=logout, .session=AFB_SESSION_LOA_1 },
{ .verb=NULL }
};
-const afb_binding_v2 afbBindingV2 = {
+const afb_binding_t afbBindingExport = {
.api = "tuto-2",
.specification = NULL,
.verbs = verbs,
diff --git a/bindings/tutorial/tuto-3.cpp b/bindings/tutorial/tuto-3.cpp
index 7400b986..66f8e83a 100644
--- a/bindings/tutorial/tuto-3.cpp
+++ b/bindings/tutorial/tuto-3.cpp
@@ -1,11 +1,12 @@
#include <string.h>
#include <json-c/json.h>
+#define AFB_BINDING_VERSION 3
#include <afb/afb-binding>
afb::event event_login, event_logout;
-void login(afb_req r)
+void login(afb_req_t r)
{
json_object *args, *user, *passwd;
char *usr;
@@ -26,20 +27,21 @@ void login(afb_req r)
usr = strdup(json_object_get_string(user));
AFB_REQ_NOTICE(req, "login user: %s", usr);
req.session_set_LOA(1);
- req.context_set(usr, free);
+// req.context(1, nullptr, free, usr);
req.success();
event_login.push(json_object_new_string(usr));
}
}
-void action(afb_req r)
+void action(afb_req_t r)
{
json_object *args, *val;
char *usr;
afb::req req(r);
args = req.json();
- usr = (char*)req.context_get();
+// usr = (char*)req.context_get();
+usr = nullptr;
AFB_REQ_NOTICE(req, "action for user %s: %s", usr, json_object_get_string(args));
if (json_object_object_get_ex(args, "subscribe", &val)) {
if (json_object_get_boolean(val)) {
@@ -55,20 +57,25 @@ void action(afb_req r)
req.success(json_object_get(args));
}
-void logout(afb_req r)
+void logout(afb_req_t r)
{
char *usr;
afb::req req(r);
- usr = (char*)req.context_get();
+// usr = (char*)req.context_get();
+usr = nullptr;
AFB_REQ_NOTICE(req, "login user %s out", usr);
event_logout.push(json_object_new_string(usr));
req.session_set_LOA(0);
- req.context_clear();
+// req.context_clear();
req.success();
}
-int init()
+int init(
+#if AFB_BINDING_VERSION >= 3
+ afb_api_t api
+#endif
+)
{
AFB_NOTICE("init");
event_login = afb_daemon_make_event("login");
@@ -79,12 +86,13 @@ int init()
return -1;
}
-const afb_verb_v2 verbs[] = {
+const afb_verb_t verbs[] = {
afb::verb("login", login, "log in the system"),
afb::verb("action", action, "perform an action", AFB_SESSION_LOA_1),
afb::verb("logout", logout, "log out the system", AFB_SESSION_LOA_1),
afb::verbend()
};
-const afb_binding_v2 afbBindingV2 = afb::binding("tuto-3", verbs, "third tutorial: C++", init);
+const afb_binding_t afbBindingExport = afb::binding("tuto-3", verbs, "third tutorial: C++", init);
+