LP1825851 Add Perl HTML::Defang dependency
[working/Evergreen.git] / Open-ILS / src / extras / oils_requestor.c
1 #include <getopt.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <readline/readline.h>
5 #include "opensrf/utils.h"
6 #include "opensrf/osrf_system.h"
7 #include "opensrf/osrf_app_session.h"
8 #include "openils/oils_event.h"
9 #include "openils/oils_utils.h"
10
11 char* script    = NULL;
12 char* authtoken = NULL;
13 static char* tz = NULL;
14
15 static int do_request( char* request );
16 static char* format_response( const jsonObject* o );
17
18 int main( int argc, char* argv[] ) {
19
20         int c;
21         char* username      = NULL;
22         char* password      = NULL;
23         char* config        = NULL;
24         char* context       = NULL;
25         char* idl_filename  = NULL;
26         char* hostname      = NULL;
27         char* request;
28
29         while( (c = getopt( argc, argv, "f:u:p:s:c:i:h:" )) != -1 ) {
30                 switch(c) {
31                         case '?': return -1;
32                         case 'f': config        = strdup(optarg); break;
33                         case 'c': context       = strdup(optarg); break;
34                         case 'u': username      = strdup(optarg); break;
35                         case 'p': password      = strdup(optarg); break;
36                         case 's': script        = strdup(optarg); break;
37                         case 'i': idl_filename  = strdup(optarg); break;
38                         case 'h': hostname      = strdup(optarg); break;
39                 }
40         }
41
42         if(!(config && context)) {
43                 fprintf(stderr, "Config or config context not provided. Exiting...\n");
44                 return -1;
45         }
46
47         if( ! osrf_system_bootstrap_client(config, context) ) {
48                 fprintf(stderr, "Unable to connect to OpenSRF network... [config:%s : context:%s]\n",
49                         config, context);
50                 return 1;
51         }
52
53         if(!idl_filename) {
54                 if(!hostname) {
55                         fprintf( stderr, "We need an IDL file name or a settings server hostname...\n");
56                     return 1;
57                 }
58                 osrf_settings_retrieve(hostname);
59         }
60
61         if (!oilsInitIDL( idl_filename )) {
62                 fprintf(stderr, "IDL file could not be loaded. Exiting...\n");
63                 return -1;
64         }
65
66         printf("Connected to OpenSRF network...\n");
67
68     tz = getenv("TZ");
69
70         if( username && password &&
71                         ( authtoken = oilsUtilsLogin(username, password, "staff", -1 )) ) {
72                 printf("Login Session: %s\n", authtoken);
73         }
74
75         while( (request=readline("oils# ")) ) {
76                 int retcode = do_request(request);
77                 free(request);
78                 if( retcode )
79                         break;
80         }
81
82         free(config);
83         free(context);
84         free(username);
85         free(password);
86         free(script);
87         free(authtoken);
88         free(idl_filename);
89         osrf_settings_free_host_config(NULL);
90         return 1;
91 }
92
93
94 static int do_request( char* request ) {
95
96         if(!strcasecmp(request, "exit") || !strcasecmp(request,"quit"))
97                 return 1;
98
99         if(!strcmp(request,""))
100                 return 0;
101
102         const char* service;
103         const char* method;
104         char* tmp = NULL;
105
106         service = strtok_r(request, " ", &tmp);
107         method = strtok_r(NULL, " ", &tmp);
108
109         if( service && method ) {
110
111                 jsonObject* params = NULL;
112
113                 if( *tmp ) {
114                         growing_buffer* buffer = buffer_init(256);
115                         buffer_fadd( buffer, "[%s]", tmp );
116                         params = jsonParse( buffer->buf );
117                         buffer_free(buffer);
118                 }
119
120                 osrfAppSession* session = osrfAppSessionClientInit(service);
121         if (tz) osrf_app_session_set_tz(session,tz);
122
123                 int req_id = osrfAppSessionSendRequest( session, params, method, 1 );
124                 osrfMessage* omsg;
125
126                 while( (omsg = osrfAppSessionRequestRecv( session, req_id, 120 )) ) {
127                         const jsonObject* res = osrfMessageGetResult(omsg);
128                         char* data = format_response(res);
129                         printf("%s\n", data);
130                         free(data);
131                         osrfMessageFree(omsg);
132                 }
133
134                 osrfAppSessionFree(session);
135                 jsonObjectFree(params);
136
137         } else {
138                 fprintf(stderr, "STATEMENT DOES NOT PARSE: %s\n", request);
139         }
140
141         return 0;
142 }
143
144
145 static char* format_response( const jsonObject* o ) {
146         if(!o) return NULL;
147
148         int width = 20;
149
150         if( o->classname && isFieldmapper(o->classname) ) {
151
152                 int i = 0;
153                 char* key;
154                 growing_buffer* buffer = buffer_init(256);
155
156                 buffer_fadd(buffer, " FM Class: %s\n", o->classname);
157
158                 while( (key = fm_pton(o->classname, i++)) ) {
159                         char* val = oilsFMGetString(o, key);
160                         const jsonObject* item;
161
162                         int l = strlen(key + 2);
163                         buffer_fadd(buffer, " %s: ", key);
164
165                         if(val) {
166
167                                 while( l++ < width ) buffer_add(buffer, "-");
168                                 buffer_fadd(buffer, " %s\n", val);
169                                 free(val);
170
171                         } else if( (item = oilsFMGetObject(o, key))) {
172
173                                 if(item->type != JSON_NULL ) {
174                                         char* d = format_response(item);
175                                         buffer_add(buffer, "\n====================================\n");
176                                         buffer_fadd(buffer, "%s\n", d);
177                                         buffer_add(buffer, "====================================\n");
178                                         free(d);
179                                 } else {
180                                         while( l++ < width ) buffer_add(buffer, "-");
181                                         buffer_add(buffer," NULL \n");
182                                 }
183
184                         } else {
185
186                                 while( l++ < width ) buffer_add(buffer, "-");
187                                 buffer_add(buffer," NULL \n");
188                         }
189
190                         free(key);
191                 }
192
193                 return buffer_release(buffer);
194         }
195
196         char* jjson;
197         if( o->type == JSON_ARRAY ) {
198                 int i = 0;
199                 growing_buffer* arrb = buffer_init(256);
200                 for( i = 0; i != o->size; i++ ) {
201                         char* d = format_response(jsonObjectGetIndex(o, i));
202                         buffer_fadd(arrb, "%s\n", d);
203                         free(d);
204                 }
205
206                 jjson = buffer_release(arrb);
207
208         } else {
209                 char* json = jsonObjectToJSON(o);
210                 jjson = jsonFormatString(json);
211                 free(json);
212         }
213
214         return jjson;
215 }