added log line
[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         printf("Connected to OpenSRF network...\n");
49
50         if( username && password &&
51                         ( authtoken = oilsUtilsLogin(username, password, "staff", -1 )) ) {
52                 printf("Login Session: %s\n", authtoken);
53         }
54
55         while((request=readline("oils# "))) 
56                 if(do_request(request)) break;
57
58         free(authtoken);
59         return 1;
60 }
61
62
63 int do_request( char* request ) {
64
65         if(!strcasecmp(request, "exit") || !strcasecmp(request,"quit"))
66                 return 1;
67
68         if(!strcmp(request,"")) return 0;
69
70         char* service;
71         char* method;
72         char* tmp;
73         char* item;
74         growing_buffer* buffer = buffer_init(256);
75         
76         service = strtok_r(request, " ", &tmp);
77         method = strtok_r(NULL, " ", &tmp);
78         while( (item = strtok_r(NULL, " ", &tmp)) ) 
79                 buffer_fadd(buffer, "%s", item);
80
81         if( service && method ) {
82
83                 jsonObject* params = NULL;
84                 if(buffer->n_used > 0) 
85                         params = jsonParseString("[%s]", buffer->buf);
86
87                 osrfAppSession* session = osrf_app_client_session_init(service);
88                 int req_id = osrf_app_session_make_req( session, params, method, 1, NULL );
89                 osrfMessage* omsg;
90
91                 while( (omsg = osrfAppSessionRequestRecv( session, req_id, 120 )) ) {
92                         jsonObject* res = osrfMessageGetResult(omsg);
93                         char* data = format_response(res);
94                         printf("\n%s\n", data);
95                         free(data);
96                         osrfMessageFree(omsg);
97                 }
98
99                 osrfAppSessionFree(session);
100                 jsonObjectFree(params);
101
102         } else {
103                 fprintf(stderr, "STATMENT DOES NOT PARSE: %s\n", request);
104         }
105
106         buffer_free(buffer);
107         return 0;
108 }
109
110
111 char* format_response( jsonObject* o ) {
112         if(!o) return NULL;
113
114         int width = 20;
115
116         if( o->classname && isFieldmapper(o->classname) ) {
117
118                 int i = 0;
119                 char* key;
120                 growing_buffer* buffer = buffer_init(256);
121
122                 buffer_fadd(buffer, " FM Class: %s\n", o->classname);
123
124                 while( (key = fm_pton(o->classname, i++)) ) {
125                         char* val = oilsFMGetString(o, key);
126                         jsonObject* item;
127
128                         int l = strlen(key + 2);
129                         buffer_fadd(buffer, " %s: ", key);
130
131                         if(val) {
132
133                                 while( l++ < width ) buffer_add(buffer, "-");
134                                 buffer_fadd(buffer, " %s\n", val);
135                                 free(val);
136
137                         } else if( (item = oilsFMGetObject(o, key))) {
138
139                                 if(item->type != JSON_NULL ) {
140                                         char* d = format_response(item);
141                                         buffer_add(buffer, "\n====================================\n");
142                                         buffer_fadd(buffer, "%s\n", d);
143                                         buffer_add(buffer, "====================================\n");
144                                         free(d);
145                                 } else {
146                                         while( l++ < width ) buffer_add(buffer, "-");
147                                         buffer_add(buffer," NULL \n");
148                                 }
149
150                         } else {
151
152                                 while( l++ < width ) buffer_add(buffer, "-");
153                                 buffer_add(buffer," NULL \n");
154                         }
155
156                         free(key);
157                 }
158
159                 char* data = buffer_data(buffer);
160                 buffer_free(buffer);
161                 return data;
162         }
163
164         char* jjson;
165         if( o->type == JSON_ARRAY ) {
166                 int i = 0;
167                 growing_buffer* arrb = buffer_init(256);
168                 for( i = 0; i != o->size; i++ ) {
169                         char* d = format_response(jsonObjectGetIndex(o, i));
170                         buffer_fadd(arrb, "%s\n", d);
171                 }
172
173                 jjson = buffer_data(arrb);
174                 buffer_free(arrb);
175
176         } else {
177                 char* json = jsonObjectToJSON(o);
178                 jjson = jsonFormatString(json);
179                 free(json);
180         }
181
182         return jjson;
183 }