Commit 8c617e99 authored by Olivier Bertrand's avatar Olivier Bertrand

Add getting REST query answer via curl. modified storage/connect/tabrest.cpp

parent 878065b4
...@@ -42,7 +42,13 @@ ...@@ -42,7 +42,13 @@
#include "tabfmt.h" #include "tabfmt.h"
#include "tabrest.h" #include "tabrest.h"
#if defined(__WIN__) || defined(_WINDOWS)
#define popen _popen
#define pclose _pclose
#endif
static XGETREST getRestFnc = NULL; static XGETREST getRestFnc = NULL;
static int Xcurl(PGLOBAL g, PCSZ Http, PCSZ Uri, PCSZ filename);
#if !defined(MARIADB) #if !defined(MARIADB)
/***********************************************************************/ /***********************************************************************/
...@@ -71,6 +77,40 @@ PTABDEF __stdcall GetREST(PGLOBAL g, void *memp) ...@@ -71,6 +77,40 @@ PTABDEF __stdcall GetREST(PGLOBAL g, void *memp)
} // end of GetREST } // end of GetREST
#endif // !MARIADB #endif // !MARIADB
/***********************************************************************/
/* Xcurl: retrieve the REST answer by executing curl. */
/***********************************************************************/
int Xcurl(PGLOBAL g, PCSZ Http, PCSZ Uri, PCSZ filename)
{
char buf[1024];
int rc;
FILE *pipe;
if (Uri) {
if (*Uri == '/' || Http[strlen(Http) - 1] == '/')
sprintf(buf, "curl %s%s > %s", Http, Uri, filename);
else
sprintf(buf, "curl %s/%s > %s", Http, Uri, filename);
} else
sprintf(buf, "curl %s > %s", Http, filename);
if ((pipe = popen(buf, "rt"))) {
if (trace(515))
while (fgets(buf, sizeof(buf), pipe)) {
htrc("%s", buf);
} // endwhile
pclose(pipe);
rc = 0;
} else {
sprintf(g->Message, "curl failed, errno =%d", errno);
rc = 1;
} // endif pipe
return rc;
} // end od Xcurl
/***********************************************************************/ /***********************************************************************/
/* GetREST: get the external TABDEF from OEM module. */ /* GetREST: get the external TABDEF from OEM module. */
/***********************************************************************/ /***********************************************************************/
...@@ -148,13 +188,15 @@ PQRYRES RESTColumns(PGLOBAL g, PTOS tp, char *tab, char *db, bool info) ...@@ -148,13 +188,15 @@ PQRYRES RESTColumns(PGLOBAL g, PTOS tp, char *tab, char *db, bool info)
PQRYRES __stdcall ColREST(PGLOBAL g, PTOS tp, char *tab, char *db, bool info) PQRYRES __stdcall ColREST(PGLOBAL g, PTOS tp, char *tab, char *db, bool info)
#endif // !MARIADB #endif // !MARIADB
{ {
PQRYRES qrp= NULL; PQRYRES qrp= NULL;
char filename[_MAX_PATH + 1]; // MAX PATH ??? char filename[_MAX_PATH + 1]; // MAX PATH ???
PCSZ http, uri, fn, ftype; int rc;
bool curl = false;
PCSZ http, uri, fn, ftype;
XGETREST grf = GetRestFunction(g); XGETREST grf = GetRestFunction(g);
if (!grf) if (!grf)
return NULL; curl = true;
http = GetStringTableOption(g, tp, "Http", NULL); http = GetStringTableOption(g, tp, "Http", NULL);
uri = GetStringTableOption(g, tp, "Uri", NULL); uri = GetStringTableOption(g, tp, "Uri", NULL);
...@@ -182,13 +224,17 @@ PQRYRES __stdcall ColREST(PGLOBAL g, PTOS tp, char *tab, char *db, bool info) ...@@ -182,13 +224,17 @@ PQRYRES __stdcall ColREST(PGLOBAL g, PTOS tp, char *tab, char *db, bool info)
// We used the file name relative to recorded datapath // We used the file name relative to recorded datapath
PlugSetPath(filename, fn, db); PlugSetPath(filename, fn, db);
//strcat(strcat(strcat(strcpy(filename, "."), slash), db), slash); curl = GetBooleanTableOption(g, tp, "Curl", curl);
//strncat(filename, fn, _MAX_PATH - strlen(filename));
// Retrieve the file from the web and copy it locally // Retrieve the file from the web and copy it locally
if (http && grf(g->Message, trace(515), http, uri, filename)) { if (curl)
// sprintf(g->Message, "Failed to get file at %s", http); rc = Xcurl(g, http, uri, filename);
} else if (!stricmp(ftype, "JSON")) else
rc = grf(g->Message, trace(515), http, uri, filename);
if (rc)
return NULL;
else if (!stricmp(ftype, "JSON"))
qrp = JSONColumns(g, db, NULL, tp, info); qrp = JSONColumns(g, db, NULL, tp, info);
else if (!stricmp(ftype, "CSV")) else if (!stricmp(ftype, "CSV"))
qrp = CSVColumns(g, NULL, tp, info); qrp = CSVColumns(g, NULL, tp, info);
...@@ -209,14 +255,14 @@ PQRYRES __stdcall ColREST(PGLOBAL g, PTOS tp, char *tab, char *db, bool info) ...@@ -209,14 +255,14 @@ PQRYRES __stdcall ColREST(PGLOBAL g, PTOS tp, char *tab, char *db, bool info)
/***********************************************************************/ /***********************************************************************/
bool RESTDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff) bool RESTDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff)
{ {
char filename[_MAX_PATH + 1]; char filename[_MAX_PATH + 1];
int rc = 0, n; int rc = 0, n;
bool xt = trace(515); bool curl = false, xt = trace(515);
LPCSTR ftype; LPCSTR ftype;
XGETREST grf = GetRestFunction(g); XGETREST grf = GetRestFunction(g);
if (!grf) if (!grf)
return true; curl = true;
#if defined(MARIADB) #if defined(MARIADB)
ftype = GetStringCatInfo(g, "Type", "JSON"); ftype = GetStringCatInfo(g, "Type", "JSON");
...@@ -235,8 +281,8 @@ bool RESTDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff) ...@@ -235,8 +281,8 @@ bool RESTDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff)
: (!stricmp(ftype, "CSV")) ? 3 : 0; : (!stricmp(ftype, "CSV")) ? 3 : 0;
if (n == 0) { if (n == 0) {
htrc("DefineAM: Unsupported REST table type %s", am); htrc("DefineAM: Unsupported REST table type %s\n", ftype);
sprintf(g->Message, "Unsupported REST table type %s", am); sprintf(g->Message, "Unsupported REST table type %s", ftype);
return true; return true;
} // endif n } // endif n
...@@ -247,8 +293,13 @@ bool RESTDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff) ...@@ -247,8 +293,13 @@ bool RESTDEF::DefineAM(PGLOBAL g, LPCSTR am, int poff)
// We used the file name relative to recorded datapath // We used the file name relative to recorded datapath
PlugSetPath(filename, Fn, GetPath()); PlugSetPath(filename, Fn, GetPath());
curl = GetBoolCatInfo("Curl", curl);
// Retrieve the file from the web and copy it locally // Retrieve the file from the web and copy it locally
rc = grf(g->Message, xt, Http, Uri, filename); if (curl)
rc = Xcurl(g, Http, Uri, filename);
else
rc = grf(g->Message, xt, Http, Uri, filename);
if (xt) if (xt)
htrc("Return from restGetFile: rc=%d\n", rc); htrc("Return from restGetFile: rc=%d\n", rc);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment