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