Skip to content

Instantly share code, notes, and snippets.

@javierllns
Last active May 23, 2024 20:59
Show Gist options
  • Save javierllns/68a3320f8cc73949c8bd844fa61072e0 to your computer and use it in GitHub Desktop.
Save javierllns/68a3320f8cc73949c8bd844fa61072e0 to your computer and use it in GitHub Desktop.
//THIS CODE DEPENDS ON XAILER's HARBOUR IDE
/*
* Project: xailer-fetch-api
* File: Principal.prg
* Descripción: Xailer (Harbour (xBase) language IDE) sample to fetch URL APIS.
* Autor: javierllns
* Fecha: 17/04/2024
*/
#include "Xailer.ch"
#include "WinINet.api" //Must be included to enable use of Xailer's TInternet class.
CLASS TMainForm FROM TForm
COMPONENT oFetchButton
METHOD CreateForm()
METHOD FetchButtonClick( oSender, nItem )
ENDCLASS
#include "MainForm.xfm"
//------------------------------------------------------------------------------
METHOD FetchButtonClick( oSender, nItem ) CLASS TPrincipal
xData := xFetchUrl('https://www.boredapi.com/api/activity/')
MsgInfo(xData)
RETURN NIL
//------------------------------------------------------------------------------
FUNCTION xFetchUrl(cURL)
//Establish connection with the remote server
oRemoteConnection := TInternet():New()
lConnectionEstablished := oRemoteConnection:Open()
IF !lConnectionEstablished
MsgInfo("Can not connect to remote server")
RETURN NIL
END IF
//Recover the desired data (Json Response, files, etc.).
hResource := oRemoteConnection:OpenURL(cURL) //Manejador del recurso
lResourceExists := !Empty(hResource)
IF !lResourceExists
MsgInfo("Can not open the URL resource")
RETURN NIL
END IF
xData = xGetResorceData(oRemoteConnection, hResource)
oRemoteConnection:CloseURL(hResource)
oRemoteConnection:Close()
RETURN xData
//------------------------------------------------------------------------------
FUNCTION xGetResorceData(oRemoteConnection, hResource)
xDataChunkAcumulator := ''
xDataChunk := ''
WHILE oRemoteConnection:ReadFile( hResource, @xDataChunk, 65536)
xDataChunkAcumulator += xDataChunk
END DO
RETURN xDataChunkAcumulator
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment