Files
MBS/WEBSERVICES/woocommerce/wproducto.4gl
T

146 lines
4.3 KiB
Plaintext

IMPORT util
IMPORT com
IMPORT os
IMPORT security
TYPE t_sync_cmd RECORD
oper STRING,
data STRING
END RECORD
DEFINE s, cmd,usuario,clave STRING
MAIN
CONNECT TO 'smarmotech' USER 'jsoto' USING 'lmmjvsd2014'
CALL familias()
END MAIN
FUNCTION familias()
DEFINE metodo,url,selec,ret,res STRING,
producto1 RECORD
name,
type STRING,
regular_price STRING,
description ,
short_description STRING
END RECORD,
http_req com.HTTPRequest,
http_resp com.HTTPResponse,
data t_sync_cmd
LET url = 'https://shop.marmotech.com.do/wp-json/wc/v3/products'
LET selec = "SELECT a.descripcion FROM familia a ORDER BY a.id_familia"
LET producto1.name = 'PRUEBA15'
LET PRODUCTO1.type='simple'
LET producto1.regular_price = '21.98'
LET producto1.description='ESTA ES LA DESCRIPCION LARGA DEL PRODUCTO'
LET producto1.short_description = 'Esta es la descripcion corta'
# LET data.oper = 'products'
# LET cmd = util.JSON.stringify(producto1)
LET cmd = '{
"name": "Premium Quality",
"type": "simple",
"regular_price": "21.99",
"description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
"short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
"categories": [
{
"id": 9
},
{
"id": 14
}
],
"images": [
{
"src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg"
},
{
"src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg"
}
]
}'
# LET cmd = util.JSON.stringify(data)
CALL sync_send_command(url,cmd) RETURNING ret,res
DISPLAY "RESPUESTA ",ret," ",res
{ PREPARE comando FROM selec
DECLARE busca_cat CURSOR FOR comando
LET data.oper = 'products/categories'
FOREACH busca_cat INTO categoria.*
LET data.data = util.JSON.stringify(categoria)
LET cmd= util.JSON.stringify(data)
LET cmd = url CLIPPED,"?name=",categoria.NAME CLIPPED
CALL sync_send_command(url,cmd) RETURNING ret,res
DISPLAY "RESPUESTA ",ret," ",res
END FOREACH
}
END FUNCTION
FUNCTION sync_send_command(url, cmd)
DEFINE url, cmd STRING
DEFINE result, tmp STRING
DEFINE http_req com.HTTPRequest
DEFINE http_resp com.HTTPResponse
LET usuario = "ck_f41ca9ba9ec888d82243a7c24717af625c26f834"
LET clave = "cs_dbd762cb45184e3badc5a114b8f4a30c19f114fc"
TRY LET http_req = com.HTTPRequest.Create(url)
CALL http_req.setAuthentication(usuario,clave,"Basic",NULL)
CALL http_req.setKeepConnection(TRUE)
CALL http_req.setConnectionTimeout(40)
CALL http_req.setTimeout(40)
CALL http_req.setMethod("POST")
CALL http_req.setCharset("UTF-8")
DISPLAY "data ",cmd
CALL http_req.doTextRequest(cmd)
#CALL http_req.doRequest()
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
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