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