test: Move pong response to service.c
[libisds.git] / client / addbox.c
blobc7325fae0b76468cea1a2d327656d02465b9e2c0
1 #define _XOPEN_SOURCE 500
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <locale.h>
5 #include <time.h>
6 #include <string.h>
7 #include <isds.h>
8 #include "common.h"
10 /* Pass e-mail address where to deliver credentials as first argument. */
11 int main(int argc, char **argv) {
12 struct isds_ctx *ctx = NULL;
13 isds_error err;
14 struct isds_DbOwnerInfo *db_owner_info = NULL;
15 struct isds_list *users = NULL;
17 setlocale(LC_ALL, "");
19 err = isds_init();
20 if (err) {
21 printf("isds_init() failed: %s\n", isds_strerror(err));
22 exit(EXIT_FAILURE);
25 isds_set_logging(ILF_ALL & ~ILF_HTTP, ILL_ALL);
27 ctx = isds_ctx_create();
28 if (!ctx) {
29 printf("isds_ctx_create() failed");
32 err = isds_set_timeout(ctx, 10000);
33 if (err) {
34 printf("isds_set_timeout() failed: %s\n", isds_strerror(err));
37 err = isds_login(ctx, url, username(), password(), NULL, NULL);
38 if (err) {
39 printf("isds_login() failed: %s: %s\n", isds_strerror(err),
40 isds_long_message(ctx));
41 } else {
42 printf("Logged in :)\n");
47 printf("Getting info about my box:\n");
48 err = isds_GetOwnerInfoFromLogin(ctx, &db_owner_info);
49 if (err) {
50 printf("isds_GetOwnerInfoFromLogin() failed: %s: %s\n",
51 isds_strerror(err), isds_long_message(ctx));
52 } else {
53 printf("isds_GetOwnerInfoFromLogin() succeeded\n");
55 print_DbOwnerInfo(db_owner_info);
59 /* Get info all users of this box */
60 if (db_owner_info) {
61 struct isds_list *item;
62 printf("Getting users of my box with ID `%s':\n", db_owner_info->dbID);
63 err = isds_GetDataBoxUsers(ctx, db_owner_info->dbID, &users);
64 if (err) {
65 printf("isds_GetDataBoxUsers() failed: %s: %s\n",
66 isds_strerror(err), isds_long_message(ctx));
67 } else {
68 printf("isds_GetDataBoxUsers() succeeded\n");
69 for(item = users; item; item = item->next) {
70 printf("List item:\n");
71 print_DbUserInfo(item->data);
77 /* Create the same box */
78 if (db_owner_info) {
79 char *refnumber = NULL;
80 struct isds_credentials_delivery *credentials_delivery = NULL;
81 const struct isds_approval approval = {
82 .approved = 1, .refference = "Me"
85 credentials_delivery = calloc(1, sizeof(*credentials_delivery));
86 if (credentials_delivery && argv[1])
87 credentials_delivery->email = strdup(argv[1]);
89 if (!credentials_delivery || (argv[1] && !credentials_delivery->email))
91 printf("Not enough memory\n");
92 } else {
93 printf("Creating already existing box\n");
94 err = isds_add_box(ctx, db_owner_info, users, "Former Names",
95 NULL, "CEO", credentials_delivery, &approval, &refnumber);
96 if (err) {
97 printf("isds_add_box() failed: %s: %s\n",
98 isds_strerror(err), isds_long_message(ctx));
99 } else {
100 printf("isds_add_box() succeeded:\n"
101 "\trequest #: %s\n"
102 "\tnew box ID: %s\n"
103 "\tcredentials e-mail: %s\n"
104 "\ttoken value: %s\n",
105 refnumber, db_owner_info->dbID,
106 credentials_delivery->email, credentials_delivery->token);
109 isds_credentials_delivery_free(&credentials_delivery);
112 isds_DbOwnerInfo_free(&db_owner_info);
113 isds_list_free(&users);
116 err = isds_logout(ctx);
117 if (err) {
118 printf("isds_logout() failed: %s\n", isds_strerror(err));
122 err = isds_ctx_free(&ctx);
123 if (err) {
124 printf("isds_ctx_free() failed: %s\n", isds_strerror(err));
128 err = isds_cleanup();
129 if (err) {
130 printf("isds_cleanup() failed: %s\n", isds_strerror(err));
133 exit (EXIT_SUCCESS);