8d0a54fbba38ee4d954e84b21c4a073a43c70c61
[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/fieldmapper_lookup.h"
9 #include "openils/oils_event.h"
10 #include "openils/oils_utils.h"
11
12 char* script            = NULL;
13 char* authtoken = NULL;
14
15 int do_request( char* request );
16 char* format_response( jsonObject* o );
17
18 int main( int argc, char* argv[] ) {
19         
20         char c;
21         char* username          = NULL;
22         char* password          = NULL;
23         char* config            = NULL;
24         char* context           = NULL;
25         char* request;
26
27         while( (c = getopt( argc, argv, "f:u:p:s:c:" )) != -1 ) {
28                 switch(c) {
29                         case '?': return -1;
30                         case 'f': config                = strdup(optarg);
31                         case 'c': context               = strdup(optarg);
32                         case 'u': username      = strdup(optarg);
33                         case 'p': password      = strdup(optarg);
34                         case 's': script                = strdup(optarg);
35                 }
36         }
37
38         if(!(config && context)) {
39                 fprintf(stderr, "Config or config context not provided. Exiting...\n");
40                 return -1;
41         }
42
43         if( ! osrf_system_bootstrap_client(config, context) ) {
44                 fprintf(stderr, "Unable to connect to OpenSRF network...\n");
45                 return 1;
46         }
47
48         if( username && password &&
49                         ( authtoken = oilsUtilsLogin(username, password, "staff", -1 )) ) {
50                 printf("Login Session: %s\n", authtoken);
51         }
52
53         while((request=readline("oils# "))) 
54                 if(do_request(request)) break;
55
56         free(authtoken);
57         return 1;
58 }
59
60
61 int do_request( char* request ) {
62
63         if(!strcasecmp(request, "exit") || !strcasecmp(request,"quit"))
64                 return 1;
65
66         if(!strcmp(request,"")) return 0;
67
68         char* service;
69         char* method;
70         char* tmp;
71         char* item;
72         growing_buffer* buffer = buffer_init(256);
73         
74         service = strtok_r(request, " ", &tmp);
75         method = strtok_r(NULL, " ", &tmp);
76         while( (item = strtok_r(NULL, " ", &tmp)) ) 
77                 buffer_fadd(buffer, "%s", item);
78
79         if( service && method ) {
80
81                 jsonObject* params = NULL;
82                 if(buffer->n_used > 0) 
83                         params = jsonParseString("[%s]", buffer->buf);
84
85                 osrfAppSession* session = osrf_app_client_session_init(service);
86                 int req_id = osrf_app_session_make_req( session, params, method, 1, NULL );
87                 osrfMessage* omsg;
88
89                 while( (omsg = osrfAppSessionRequestRecv( session, req_id, 120 )) ) {
90                         jsonObject* res = osrfMessageGetResult(omsg);
91                         char* data = format_response(res);
92                         printf("\n%s\n", data);
93                         free(data);
94                         osrfMessageFree(omsg);
95                 }
96
97                 osrfAppSessionFree(session);
98                 jsonObjectFree(params);
99
100         } else {
101                 fprintf(stderr, "STATMENT DOES NOT PARSE: %s\n", request);
102         }
103
104         buffer_free(buffer);
105         return 0;
106 }
107
108
109 char* format_response( jsonObject* o ) {
110         if(!o) return NULL;
111
112         int width = 20;
113
114         if( o->classname && isFieldmapper(o->classname) ) {
115
116                 int i = 0;
117                 char* key;
118                 growing_buffer* buffer = buffer_init(256);
119
120                 buffer_fadd(buffer, " FM Class: %s\n", o->classname);
121
122                 while( (key = fm_pton(o->classname, i++)) ) {
123                         char* val = oilsFMGetString(o, key);
124                         jsonObject* item;
125
126                         int l = strlen(key + 2);
127                         buffer_fadd(buffer, " %s: ", key);
128
129                         if(val) {
130
131                                 while( l++ < width ) buffer_add(buffer, "-");
132                                 buffer_fadd(buffer, " %s\n", val);
133                                 free(val);
134
135                         } else if( (item = oilsFMGetObject(o, key))) {
136
137                                 if(item->type != JSON_NULL ) {
138                                         char* d = format_response(item);
139                                         buffer_add(buffer, "\n====================================\n");
140                                         buffer_fadd(buffer, "%s\n", d);
141                                         buffer_add(buffer, "====================================\n");
142                                         free(d);
143                                 } else {
144                                         while( l++ < width ) buffer_add(buffer, "-");
145                                         buffer_add(buffer," NULL \n");
146                                 }
147
148                         } else {
149
150                                 while( l++ < width ) buffer_add(buffer, "-");
151                                 buffer_add(buffer," NULL \n");
152                         }
153
154                         free(key);
155                 }
156
157                 char* data = buffer_data(buffer);
158                 buffer_free(buffer);
159                 return data;
160         }
161
162         char* jjson;
163         if( o->type == JSON_ARRAY ) {
164                 int i = 0;
165                 growing_buffer* arrb = buffer_init(256);
166                 for( i = 0; i != o->size; i++ ) {
167                         char* d = format_response(jsonObjectGetIndex(o, i));
168                         buffer_fadd(arrb, "%s\n", d);
169                 }
170
171                 jjson = buffer_data(arrb);
172                 buffer_free(arrb);
173
174         } else {
175                 char* json = jsonObjectToJSON(o);
176                 jjson = jsonFormatString(json);
177                 free(json);
178         }
179
180         return jjson;
181 }