264 lines
6.8 KiB
Plaintext
264 lines
6.8 KiB
Plaintext
*-- utils.prg - Utilitare pentru GoMag API
|
|
*-- Functii pentru citirea/scrierea fisierelor INI si alte utilitare
|
|
*-- Autor: Claude AI
|
|
*-- Data: 27.08.2025
|
|
|
|
*-- Functie pentru citirea fisierelor INI private
|
|
*-- Returneaza valoarea din sectiunea si intrarea specificata sau blank daca nu e gasita
|
|
FUNCTION ReadPini
|
|
PARAMETERS cSection, cEntry, cINIFile
|
|
LOCAL cDefault, cRetVal, nRetLen
|
|
|
|
cDefault = ""
|
|
cRetVal = SPACE(255)
|
|
nRetLen = LEN(cRetVal)
|
|
|
|
DECLARE INTEGER GetPrivateProfileString IN WIN32API ;
|
|
STRING cSection, ;
|
|
STRING cEntry, ;
|
|
STRING cDefault, ;
|
|
STRING @cRetVal, ;
|
|
INTEGER nRetLen, ;
|
|
STRING cINIFile
|
|
|
|
nRetLen = GetPrivateProfileString(cSection, ;
|
|
cEntry, ;
|
|
cDefault, ;
|
|
@cRetVal, ;
|
|
nRetLen, ;
|
|
cINIFile)
|
|
|
|
RETURN LEFT(cRetVal, nRetLen)
|
|
ENDFUNC
|
|
|
|
*-- Functie pentru scrierea in fisierele INI private
|
|
*-- Returneaza .T. daca e successful, .F. daca nu
|
|
FUNCTION WritePini
|
|
PARAMETERS cSection, cEntry, cValue, cINIFile
|
|
LOCAL nRetVal
|
|
|
|
DECLARE INTEGER WritePrivateProfileString IN WIN32API ;
|
|
STRING cSection, ;
|
|
STRING cEntry, ;
|
|
STRING cValue, ;
|
|
STRING cINIFile
|
|
|
|
nRetVal = WritePrivateProfileString(cSection, ;
|
|
cEntry, ;
|
|
cValue, ;
|
|
cINIFile)
|
|
|
|
RETURN nRetVal = 1
|
|
ENDFUNC
|
|
|
|
*-- Functie pentru incarcarea tuturor setarilor din fisierul INI
|
|
FUNCTION LoadSettings
|
|
PARAMETERS cINIFile
|
|
LOCAL loSettings
|
|
|
|
*-- Cream un obiect pentru toate setarile
|
|
loSettings = CREATEOBJECT("Empty")
|
|
|
|
*-- Sectiunea API
|
|
ADDPROPERTY(loSettings, "ApiBaseUrl", ReadPini("API", "ApiBaseUrl", cINIFile))
|
|
ADDPROPERTY(loSettings, "OrderApiUrl", ReadPini("API", "OrderApiUrl", cINIFile))
|
|
ADDPROPERTY(loSettings, "ApiKey", ReadPini("API", "ApiKey", cINIFile))
|
|
ADDPROPERTY(loSettings, "ApiShop", ReadPini("API", "ApiShop", cINIFile))
|
|
ADDPROPERTY(loSettings, "UserAgent", ReadPini("API", "UserAgent", cINIFile))
|
|
ADDPROPERTY(loSettings, "ContentType", ReadPini("API", "ContentType", cINIFile))
|
|
|
|
*-- Sectiunea PAGINATION
|
|
ADDPROPERTY(loSettings, "Limit", VAL(ReadPini("PAGINATION", "Limit", cINIFile)))
|
|
|
|
*-- Sectiunea OPTIONS
|
|
ADDPROPERTY(loSettings, "GetProducts", ReadPini("OPTIONS", "GetProducts", cINIFile) = "1")
|
|
ADDPROPERTY(loSettings, "GetOrders", ReadPini("OPTIONS", "GetOrders", cINIFile) = "1")
|
|
|
|
*-- Sectiunea FILTERS
|
|
ADDPROPERTY(loSettings, "OrderDaysBack", VAL(ReadPini("FILTERS", "OrderDaysBack", cINIFile)))
|
|
|
|
RETURN loSettings
|
|
ENDFUNC
|
|
|
|
*-- Test conectivitate internet
|
|
FUNCTION TestConnectivity
|
|
LOCAL loHttp, llResult
|
|
|
|
llResult = .T.
|
|
|
|
TRY
|
|
loHttp = CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
|
|
loHttp.Open("GET", "https://www.google.com", .F.)
|
|
loHttp.SetTimeouts(5000, 5000, 5000, 5000)
|
|
loHttp.Send()
|
|
|
|
IF loHttp.Status != 200
|
|
llResult = .F.
|
|
ENDIF
|
|
|
|
CATCH
|
|
llResult = .F.
|
|
ENDTRY
|
|
|
|
loHttp = NULL
|
|
RETURN llResult
|
|
ENDFUNC
|
|
|
|
*-- Functie pentru codificare URL
|
|
FUNCTION UrlEncode
|
|
PARAMETERS tcString
|
|
|
|
LOCAL lcResult, lcChar, lnI
|
|
|
|
lcResult = ""
|
|
|
|
FOR lnI = 1 TO LEN(tcString)
|
|
lcChar = SUBSTR(tcString, lnI, 1)
|
|
|
|
DO CASE
|
|
CASE ISALPHA(lcChar) OR ISDIGIT(lcChar) OR INLIST(lcChar, "-", "_", ".", "~")
|
|
lcResult = lcResult + lcChar
|
|
OTHERWISE
|
|
lcResult = lcResult + "%" + RIGHT("0" + TRANSFORM(ASC(lcChar), "@0"), 2)
|
|
ENDCASE
|
|
ENDFOR
|
|
|
|
RETURN lcResult
|
|
ENDFUNC
|
|
|
|
*-- Functie pentru verificarea existentei fisierului INI
|
|
FUNCTION CheckIniFile
|
|
PARAMETERS cINIFile
|
|
LOCAL llExists
|
|
|
|
TRY
|
|
llExists = FILE(cINIFile)
|
|
CATCH
|
|
llExists = .F.
|
|
ENDTRY
|
|
|
|
RETURN llExists
|
|
ENDFUNC
|
|
|
|
*-- Functie pentru crearea unui fisier INI implicit cu setari de baza
|
|
FUNCTION CreateDefaultIni
|
|
PARAMETERS cINIFile
|
|
LOCAL llSuccess
|
|
|
|
llSuccess = .T.
|
|
|
|
TRY
|
|
*-- Sectiunea API
|
|
WritePini("API", "ApiBaseUrl", "https://api.gomag.ro/api/v1/product/read/json?enabled=1", cINIFile)
|
|
WritePini("API", "OrderApiUrl", "https://api.gomag.ro/api/v1/order/read/json", cINIFile)
|
|
WritePini("API", "ApiKey", "YOUR_API_KEY_HERE", cINIFile)
|
|
WritePini("API", "ApiShop", "https://yourstore.gomag.ro", cINIFile)
|
|
WritePini("API", "UserAgent", "Mozilla/5.0", cINIFile)
|
|
WritePini("API", "ContentType", "application/json", cINIFile)
|
|
|
|
*-- Sectiunea PAGINATION
|
|
WritePini("PAGINATION", "Limit", "100", cINIFile)
|
|
|
|
*-- Sectiunea OPTIONS
|
|
WritePini("OPTIONS", "GetProducts", "1", cINIFile)
|
|
WritePini("OPTIONS", "GetOrders", "1", cINIFile)
|
|
|
|
*-- Sectiunea FILTERS
|
|
WritePini("FILTERS", "OrderDaysBack", "7", cINIFile)
|
|
|
|
CATCH
|
|
llSuccess = .F.
|
|
ENDTRY
|
|
|
|
RETURN llSuccess
|
|
ENDFUNC
|
|
|
|
*-- Functie pentru initializarea logging-ului
|
|
FUNCTION InitLog
|
|
PARAMETERS cBaseName
|
|
LOCAL lcLogFile, lcStartTime, lcLogHeader, lcLogDir
|
|
|
|
*-- Cream directorul log daca nu existe
|
|
lcLogDir = gcAppPath + "log"
|
|
IF !DIRECTORY(lcLogDir)
|
|
MKDIR (lcLogDir)
|
|
ENDIF
|
|
|
|
*-- Generam numele fisierului log cu timestamp in directorul log
|
|
lcStartTime = DTOS(DATE()) + "_" + STRTRAN(TIME(), ":", "")
|
|
lcLogFile = lcLogDir + "\" + cBaseName + "_" + lcStartTime + ".log"
|
|
|
|
*-- Header pentru log
|
|
lcLogHeader = "[" + TIME() + "] [START] === GoMag Sync Started ===" + CHR(13) + CHR(10)
|
|
lcLogHeader = lcLogHeader + "[" + TIME() + "] [INFO ] Date: " + DTOC(DATE()) + " | VFP: " + VERSION() + CHR(13) + CHR(10)
|
|
|
|
*-- Cream fisierul log
|
|
STRTOFILE(lcLogHeader, lcLogFile)
|
|
|
|
RETURN lcLogFile
|
|
ENDFUNC
|
|
|
|
*-- Functie pentru logging cu nivel si timestamp
|
|
FUNCTION LogMessage
|
|
PARAMETERS cMessage, cLevel, cLogFile
|
|
LOCAL lcTimeStamp, lcLogEntry, lcExistingContent
|
|
|
|
*-- Setam nivel implicit daca nu e specificat
|
|
IF EMPTY(cLevel)
|
|
cLevel = "INFO "
|
|
ELSE
|
|
*-- Formatam nivelul pentru a avea 5 caractere
|
|
cLevel = LEFT(cLevel + " ", 5)
|
|
ENDIF
|
|
|
|
*-- Cream timestamp-ul
|
|
lcTimeStamp = TIME()
|
|
|
|
*-- Formatam mesajul pentru log
|
|
lcLogEntry = "[" + lcTimeStamp + "] [" + cLevel + "] " + cMessage + CHR(13) + CHR(10)
|
|
|
|
*-- Adaugam la fisierul existent
|
|
lcExistingContent = ""
|
|
IF FILE(cLogFile)
|
|
lcExistingContent = FILETOSTR(cLogFile)
|
|
ENDIF
|
|
|
|
STRTOFILE(lcExistingContent + lcLogEntry, cLogFile)
|
|
|
|
RETURN .T.
|
|
ENDFUNC
|
|
|
|
*-- Functie pentru inchiderea logging-ului cu statistici
|
|
FUNCTION CloseLog
|
|
PARAMETERS nStartTime, nProductsCount, nOrdersCount, cLogFile
|
|
LOCAL lcEndTime, lcDuration, lcStatsEntry
|
|
|
|
lcEndTime = TIME()
|
|
*-- Calculam durata in secunde
|
|
nDuration = SECONDS() - nStartTime
|
|
|
|
*-- Formatam statisticile finale
|
|
lcStatsEntry = "[" + lcEndTime + "] [END ] === GoMag Sync Completed ===" + CHR(13) + CHR(10)
|
|
lcStatsEntry = lcStatsEntry + "[" + lcEndTime + "] [STATS] Duration: " + TRANSFORM(INT(nDuration)) + "s"
|
|
|
|
IF nProductsCount > 0
|
|
lcStatsEntry = lcStatsEntry + " | Products: " + TRANSFORM(nProductsCount)
|
|
ENDIF
|
|
|
|
IF nOrdersCount > 0
|
|
lcStatsEntry = lcStatsEntry + " | Orders: " + TRANSFORM(nOrdersCount)
|
|
ENDIF
|
|
|
|
lcStatsEntry = lcStatsEntry + CHR(13) + CHR(10)
|
|
|
|
*-- Adaugam la log
|
|
LOCAL lcExistingContent
|
|
lcExistingContent = ""
|
|
IF FILE(cLogFile)
|
|
lcExistingContent = FILETOSTR(cLogFile)
|
|
ENDIF
|
|
|
|
STRTOFILE(lcExistingContent + lcStatsEntry, cLogFile)
|
|
|
|
RETURN .T.
|
|
ENDFUNC |