Files
MBS/WEBSERVICES/test/akeneo/conector.4gl
T

184 lines
5.2 KiB
Plaintext

IMPORT com
IMPORT util
IMPORT os
IMPORT security
TYPE t_sync_cmd RECORD
oper STRING,
data STRING
END RECORD
DEFINE url STRING
MAIN
CALL conector()
END MAIN
FUNCTION conector()
DEFINE ret INTEGER,
res,dataresponse,respuesta STRING,
data_response RECORD
access_token
,expires_in
,token_type
,scope
,refresh_token
STRING
END RECORD
DEFINE data_c RECORD
username
,password
,grant_type
STRING
END RECORD,
metodo,cmd STRING
LET url = "https://pim.marmotech.com.do/api/oauth/v1/token"
LET data_c.grant_type = 'password'
LET data_c.username = "jsoto"
LET data_c.password = "Marmotech1234"
LET metodo = "POST"
LET cmd = util.JSON.stringify(data_c)
DISPLAY " CMD ",cmd
CALL sync_send_command(url,cmd, metodo) RETURNING ret,res,dataresponse
CALL util.JSON.parse(dataresponse,data_response)
DISPLAY "json: ",data_response.access_token," - ",data_response.expires_in," - ",data_response.refresh_token," - ",
data_response.scope," - ",data_response.token_type
DISPLAY "RESPONSE ",ret,"--- ",res,"---",dataresponse
LET metodo = 'GET'
LET url = "https://pim.marmotech.com.do"
#CALL send_data(url,data_response.access_token,metodo) RETURNING ret,res,respuesta
DISPLAY "categorias ",ret ,"-",res ,"-",respuesta
END FUNCTION
FUNCTION send_data(xurl,xcmd,xmetodo1)
DEFINE xret,xres,xrespuesta,xurl,xcmd,xmetodo1 STRING,
http_re com.HTTPRequest,
http_response com.HttpResponse
DEFINE datahost RECORD
host STRING
,authentication RECORD
fos_oauth_server_token
,route
STRING
END RECORD
END RECORD,
s string
DEFINE categorias RECORD
ref RECORD
links
,self
,href STRING
END RECORD,
ref1 RECORD
primero
,href STRING
END RECORD,
ref2 RECORD
anterior
,href STRING
END RECORD ,
ref3 RECORD
siguiente
,href STRING
END RECORD,
ref4 RECORD
pagina
,almacenado STRING
END RECORD,
items DYNAMIC ARRAY OF RECORD
link RECORD
self
,href STRING
END RECORD,
code
,parent STRING
END RECORD,
labels DYNAMIC ARRAY OF RECORD
en_US
,fr_fr
,de_DE STRING
END RECORD
END RECORD
LET xcmd = 'Bearer ',xcmd
DISPLAY "URL ",xurl
LET datahost.host = xurl
LET datahost.authentication.fos_oauth_server_token =xcmd
LET datahost.authentication.route = '/api/oauth/v1/token'
LET s = util.JSON.stringify(datahost)
LET xurl = xurl CLIPPED,'/api/rest/v1/categories'
TRY
LET http_re = com.HttpRequest.Create(xurl)
CALL http_re.setMethod(xmetodo1)
CALL http_re.setHeader("Authorization",xcmd)
CALL http_re.setHeader("Accept",'*/*')
CALL http_re.doRequest()
DISPLAY "ESTADO ", http_response.getStatusCode()
IF http_response.getStatusCode() != 200 THEN
RETURN http_response.getStatusCode(), http_response.getStatusDescription(),http_response.getTextResponse()
END IF
LET xrespuesta = http_response.getTextResponse()
CATCH
RETURN -2, SFMT("HTTP request error: STATUS=%1 (%2)",STATUS,SQLCA.SQLERRM),'ERROR'
END TRY
RETURN http_response.getStatusCode(), http_response.getStatusDescription(),http_response.getTextResponse()
END FUNCTION
FUNCTION sync_send_command(url, cmd, xmetodo)
DEFINE xmetodo,url, cmd,client_id,secret,b64 STRING
DEFINE result, tmp STRING
DEFINE http_req com.HTTPRequest
DEFINE http_resp com.HTTPResponse
LET client_id = "6_5yt79vgp374s0gsgsggg848kwooscowwkc8ok0kog8okg8c0oc",":","yljqzqi1r808o8wog8kksok80ogwocg88w8sksk0wokgswc0c"
LET b64 = security.Base64.FromString(client_id)
LET b64 = 'Basic ',b64
TRY
LET http_req = com.HTTPRequest.Create(url)
#CALL http_req.setConnectionTimeout(40)
#CALL http_req.setTimeout(40)
CALL http_req.setMethod(xmetodo)
CALL http_req.setCharset("UTF-8")
CALL http_req.setHeader("clientId","6_5yt79vgp374s0gsgsggg848kwooscowwkc8ok0kog8okg8c0oc")
CALL http_req.setHeader("secret","yljqzqi1r808o8wog8kksok80ogwocg88w8sksk0wokgswc0c")
CALL http_req.setHeader("Content-Type","application/json")
CALL http_req.setHeader("Authorization",b64)
CALL http_req.doTextRequest(cmd)
LET http_resp=http_req.getResponse()
IF http_resp.getStatusCode() != 200 THEN
RETURN http_resp.getStatusCode(), http_resp.getStatusDescription(),http_resp.getTextResponse()
END IF
LET tmp = http_resp.getStatusDescription()
IF (tmp == "OK" || tmp == "no error" ) THEN
RETURN -1, SFMT("HTTP request status description: %1 ",tmp),http_resp.getTextResponse()
END IF
LET result = http_resp.getTextResponse()
CATCH
RETURN -2, SFMT("HTTP request error: STATUS=%1 (%2)",STATUS,SQLCA.SQLERRM)
END TRY
RETURN http_resp.getStatusCode(), http_resp.getHeaderName(7), result
END FUNCTION
FUNCTION show_sync_err(msg)
DEFINE msg STRING
CALL fgl_winmessage(%"ERROR", msg, "exclamation")
END FUNCTION