STd_Out - simple API Json return

Started by spl, March 02, 2026, 11:28:13 AM

Previous topic - Next topic

spl

No need to understand, just test how well the Json data is returned from API call, letting PS do all the backend work
;Simple API json return for crypto market exchange
;Displays Json return as text file
;Stan Littlefield 3/2/2026
;========================================================================================================================
Gosub udfs
file = Dirscript():"okx.json"
If fileexist(file) Then filedelete(file)
args = $"$Url = 'https:://www.okx.com/api/v5/market/tickers?instType=SPOT'
$json = Invoke-RestMethod -UseBasicParsing -Uri $url | ConvertTo-Json 
$json
$"

cmd="Powershell"
msg='Global Assemblies'
BoxOpen("Running...",cmd:" ":args:@LF:"PLEASE WAIT...MAY TAKE SOME TIME")
TimeDelay(2)
vals = Get_stdout()
FilePut(file,vals) 
If FileExist(file) Then Run("notepad.exe",file)
Exit
;======================================================================
:udfs
#DefineSubroutine Get_stdout()
ObjectClrOption("useany","System")
objInfo = ObjectClrNew("System.Diagnostics.ProcessStartInfo")
Output=""
timeOut = ObjectType("I2",5000)
objInfo.FileName = cmd
objInfo.RedirectStandardError = ObjectType("BOOL",@TRUE)
objInfo.RedirectStandardOutput = ObjectType("BOOL",@TRUE)
objInfo.UseShellExecute = ObjectType("BOOL",@FALSE)
objInfo.CreateNoWindow = ObjectType("BOOL",@TRUE)
objInfo.Arguments = args
oProcess = ObjectClrNew("System.Diagnostics.Process")
oProcess.StartInfo = objInfo
BoxShut()
oProcess.Start()
oProcess.WaitForExit(timeout)
STDOUT = oProcess.StandardOutput.ReadToEnd()
STDERR = oProcess.StandardError.ReadToEnd()
Output = Output:STDOUT:@CRLF
If STDERR<>""
  Output = Output:"STDERR:":STDERR:@CRLF
Endif
oProcess = 0
objInfo = 0

Return (Output)
#EndSubroutine
Return
;==========================================================
Stan - formerly stanl [ex-Pundit]

spl

This one is more fun....
;Simple API json return to predict age based on name
;Displays Json return as text file
;Stan Littlefield 3/3/2026
;========================================================================================================================
Gosub udfs
file = Dirscript():"nameage.json"
If fileexist(file) Then filedelete(file)
name = "Tony"
args = $"$url = 'https:://api.agify.io?name=[name]&country_id=US'
$json = Invoke-RestMethod -UseBasicParsing -Uri $url | ConvertTo-Json 
$json
$"
args = StrReplace(args,"[name]",name)
cmd="Powershell"
msg='Guess your Age'
BoxOpen("Running...",cmd:" ":args:@LF:"PLEASE WAIT...MAY TAKE SOME TIME")
TimeDelay(2)
vals = Get_stdout()
FilePut(file,vals) 
If FileExist(file) Then Run("notepad.exe",file)
Exit
;======================================================================
:udfs
#DefineSubroutine Get_stdout()
ObjectClrOption("useany","System")
objInfo = ObjectClrNew("System.Diagnostics.ProcessStartInfo")
Output=""
timeOut = ObjectType("I2",5000)
objInfo.FileName = cmd
objInfo.RedirectStandardError = ObjectType("BOOL",@TRUE)
objInfo.RedirectStandardOutput = ObjectType("BOOL",@TRUE)
objInfo.UseShellExecute = ObjectType("BOOL",@FALSE)
objInfo.CreateNoWindow = ObjectType("BOOL",@TRUE)
objInfo.Arguments = args
oProcess = ObjectClrNew("System.Diagnostics.Process")
oProcess.StartInfo = objInfo
BoxShut()
oProcess.Start()
oProcess.WaitForExit(timeout)
STDOUT = oProcess.StandardOutput.ReadToEnd()
STDERR = oProcess.StandardError.ReadToEnd()
Output = Output:STDOUT:@CRLF
If STDERR<>""
  Output = Output:"STDERR:":STDERR:@CRLF
Endif
oProcess = 0
objInfo = 0

Return (Output)
#EndSubroutine
Return
;==========================================================
Stan - formerly stanl [ex-Pundit]

td

A wee bit off, but the site does have some interesting APIs.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

SMF spam blocked by CleanTalk