207 lines
5.5 KiB
Plaintext
207 lines
5.5 KiB
Plaintext
IMPORT util
|
|
IMPORT com
|
|
IMPORT os
|
|
IMPORT security
|
|
|
|
TYPE t_sync_cmd RECORD
|
|
oper STRING,
|
|
data STRING
|
|
END RECORD
|
|
|
|
MAIN
|
|
DEFINE k,kurl,kbodega STRING,
|
|
ktrue BOOLEAN
|
|
DEFINE ku RECORD
|
|
usuario VARCHAR(50),
|
|
clave VARCHAR(20),
|
|
sesion VARCHAR(10)
|
|
END RECORD
|
|
LET ku.usuario = 'jsoto'
|
|
LET ku.clave = 'lmmjvsd2014'
|
|
LET ku.sesion = 'ABRIR'
|
|
|
|
LET kurl = "https://ws.marmotech.com.do/gas320/ws/r/restserver"
|
|
CALL chequea_req(kurl)
|
|
|
|
END MAIN
|
|
|
|
FUNCTION chequea_req(xurl)
|
|
DEFINE xurl STRING
|
|
|
|
DEFINE p RECORD
|
|
credenciales RECORD
|
|
usuario,clave VARCHAR(50),
|
|
num_req INT
|
|
END RECORD,
|
|
documentos DYNAMIC ARRAY OF RECORD
|
|
contenido STRING,
|
|
nombre_file STRING
|
|
END RECORD,
|
|
mensajes RECORD
|
|
cod_msg INT,
|
|
mensaje_out STRING
|
|
END RECORD
|
|
|
|
END RECORD,
|
|
i INT,
|
|
msg,cmd,ret,res STRING,
|
|
pfile BYTE
|
|
DeFINE r_cmd t_sync_cmd
|
|
|
|
LET p.credenciales.usuario ='JSOTO'
|
|
LET p.credenciales.clave = 'lmmjvsd2014'
|
|
LET p.credenciales.num_req = 9615
|
|
LET r_cmd.oper = "requisicion_doc"
|
|
LET r_cmd.data = util.JSON.stringify(p) -- aca se convierte a string los datos de la operacion
|
|
LET cmd = util.JSON.stringify(r_cmd)
|
|
CALL sync_send_command(xurl, cmd) RETURNING ret, res
|
|
|
|
IF ret == 0 THEN
|
|
CALL util.JSON.parse(res,p)
|
|
|
|
|
|
# OPEN WINDOW w1 WITH FORM "reqcompra"
|
|
# DISPLAY ARRAY data.detalle TO record1.*
|
|
# CLOSE WINDOW w1
|
|
|
|
FOR i=1 TO p.documentos.getLength()
|
|
IF p.documentos[i].contenido IS NOT NULL THEN
|
|
CALL b64_to_file(p.documentos[i].contenido,p.documentos[i].nombre_file CLIPPED)
|
|
END IF
|
|
END FOR
|
|
END IF
|
|
END FUNCTION
|
|
|
|
FUNCTION b64_to_file(str_b64,f_sal)
|
|
|
|
DEFINE f_sal,str_b64,str_aux STRING
|
|
|
|
TRY
|
|
CALL security.Base64.SaveBinary(f_sal,str_b64)
|
|
CATCH
|
|
DISPLAY " data ",str_b64
|
|
LET str_aux="Unable to convert Base64 String to file :",STATUS
|
|
CALL fgl_winmessage("Error",str_aux,"stop")
|
|
END TRY
|
|
|
|
END FUNCTION
|
|
|
|
FUNCTION valida_usuario(u,url)
|
|
DEFINE u RECORD
|
|
usuario VARCHAR(50),
|
|
clave VARCHAR(20),
|
|
sesion VARCHAR(10)
|
|
END RECORD
|
|
DEFINE r_cmd t_sync_cmd
|
|
|
|
DEFINE cmd, res, msg,url STRING,
|
|
ret INT
|
|
DEFINE r RECORD
|
|
perror STRING,
|
|
pbodega SMALLINT
|
|
END RECORD
|
|
LET r_cmd.oper = "v_usuarios"
|
|
LET r_cmd.data = util.JSON.stringify(u) -- aca se convierte a string los datos de la operacion
|
|
|
|
LET cmd = util.JSON.stringify(r_cmd) -- aca se convierte a string el comando/operacion a enviar al server
|
|
|
|
CALL sync_send_command(url, cmd) RETURNING ret, res
|
|
|
|
IF ret == 0 THEN
|
|
CALL util.JSON.parse(res,r)
|
|
|
|
IF r.perror = "VALIDO" THEN
|
|
RETURN TRUE,r.pbodega
|
|
ELSE
|
|
LET msg=SFMT(%"Connection '%1' failed",r_cmd.oper),"\n",url,"\n",res
|
|
CALL show_sync_err(msg)
|
|
RETURN FALSE,r.pbodega
|
|
END IF
|
|
ELSE
|
|
LET msg=SFMT(%"Connection '%1' failed",r_cmd.oper),"\n",url,"\n",res
|
|
CALL show_sync_err(msg)
|
|
RETURN FALSE,r.pbodega
|
|
END IF
|
|
END FUNCTION
|
|
|
|
FUNCTION chequea_rnc(url)
|
|
DEFINE r_cmd t_sync_cmd
|
|
DEFINE u CHAR(11),
|
|
url,cmd,res,msg STRING,
|
|
ret INT
|
|
DEFINE r RECORD
|
|
perror STRING,
|
|
pbodega SMALLINT
|
|
END RECORD
|
|
DEFINE prnc RECORD
|
|
nombre_cliente STRING,
|
|
direccion STRING,
|
|
existe INT
|
|
END RECORD
|
|
LET u ='130337391'
|
|
LET r_cmd.oper = "rnc"
|
|
LET r_cmd.data = util.JSON.stringify(u) -- aca se convierte a string los datos de la operacion
|
|
LET cmd = util.JSON.stringify(r_cmd)
|
|
CALL sync_send_command(url, cmd) RETURNING ret, res
|
|
DISPLAY "ret ",ret
|
|
IF ret == 0 THEN
|
|
CALL util.JSON.parse(res,prnc)
|
|
DISPLAY "datos ",prnc.nombre_cliente
|
|
IF r.perror = "VALIDO" THEN
|
|
RETURN TRUE,r.pbodega
|
|
ELSE
|
|
LET msg=SFMT(%"Connection '%1' failed",r_cmd.oper),"\n",url,"\n",res
|
|
CALL show_sync_err(msg)
|
|
RETURN FALSE,r.pbodega
|
|
END IF
|
|
ELSE
|
|
LET msg=SFMT(%"Connection '%1' failed",r_cmd.oper),"\n",url,"\n",res
|
|
CALL show_sync_err(msg)
|
|
RETURN FALSE,r.pbodega
|
|
END IF
|
|
END FUNCTION
|
|
|
|
|
|
#
|
|
## Communications library using com (http or https)
|
|
#
|
|
|
|
FUNCTION sync_send_command(url, cmd)
|
|
|
|
DEFINE url, cmd STRING
|
|
DEFINE result, tmp STRING
|
|
DEFINE http_req com.HTTPRequest
|
|
DEFINE http_resp com.HTTPResponse
|
|
|
|
|
|
TRY
|
|
LET http_req = com.HTTPRequest.Create(url)
|
|
CALL http_req.setConnectionTimeout(40)
|
|
CALL http_req.setTimeout(40)
|
|
CALL http_req.setMethod("POST")
|
|
CALL http_req.setCharset("UTF-8")
|
|
CALL http_req.setHeader("DBSync-Client","mbs")
|
|
CALL http_req.setHeader("Content-Type","dbsync/json")
|
|
CALL http_req.doTextRequest(cmd)
|
|
LET http_resp=http_req.getResponse()
|
|
IF http_resp.getStatusCode() != 200 THEN
|
|
RETURN http_resp.getStatusCode(), http_resp.getStatusDescription()
|
|
END IF
|
|
LET tmp = http_resp.getStatusDescription()
|
|
IF (tmp == "OK" || tmp == "no error" ) THEN
|
|
RETURN -1, SFMT("HTTP request status description: %1 ",tmp)
|
|
END IF
|
|
LET result = http_resp.getTextResponse()
|
|
CATCH
|
|
RETURN -2, SFMT("HTTP request error: STATUS=%1 (%2)",STATUS,SQLCA.SQLERRM)
|
|
END TRY
|
|
|
|
RETURN 0, result
|
|
|
|
END FUNCTION
|
|
|
|
FUNCTION show_sync_err(msg)
|
|
DEFINE msg STRING
|
|
CALL fgl_winmessage(%"ERROR", msg, "exclamation")
|
|
END FUNCTION
|