Using HTTP calls in Gambas 3.15

Post your Gambas programming questions here.
Post Reply
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Using HTTP calls in Gambas 3.15

Post by AndyGable »

Hi All,

I was wondering if someone could talk me though converting this bit of VB.net code so it can function with Gambas

The following are loaded at start up of the module
PS_URL
TerminalIDNumber
PS_USER
PS_PASS

as these are unique to each client

Code: Select all

   Private Sub GetTerminalStatus()
        Try
            Dim Request As HttpWebRequest = HttpWebRequest.Create(PS_URL & "/terminals/" & TerminalIDNumber)
            Dim credentials As String = Convert.ToBase64String(Encoding.ASCII.GetBytes(PS_USER & ":" & PS_PASS))
            txtResults.Text = vbNullString

            With Request
                .Proxy = Nothing
                .Headers(HttpRequestHeader.Authorization) = String.Format("Basic {0}", credentials)
                .UserAgent = PS_USER
            End With

            Dim response As HttpWebResponse = Request.GetResponse()
            Dim dataStream As Stream = response.GetResponseStream
            Dim reader As New StreamReader(dataStream)
            Dim responseFromServer As String = reader.ReadToEnd()

            txtResults.Text = responseFromServer


            If responseFromServer = "0" Then
                MsgBox("Retreal of Status Failed")
                addtoStatusList("Retreal of Status Failed")
                If DebugActive = "Yes" Then AddToDebugList("Retreal of Status Failed Please try again")
                SendToPoSterminal("RetrealFailed")                
            Else
                Dim json As String = responseFromServer
                Dim ser As JObject = JObject.Parse(json)
                Dim data As List(Of JToken) = ser.Children().ToList
                For Each item As JProperty In data
                    item.CreateReader()
                    Select Case item.Name
                        Case "status"
                            Select Case item.Value
                                Case "AVAILABLE"
                                    GetStatus = 1
                                    SendToPoSterminal("TerminalOnLine|")
                                    addtoStatusList("Terminal Ready")

                                Case "BUSY"
                                    SendToPoSterminal("TerminalBusy|")
                                    addtoStatusList("Terminal busy please wait 10 seconds and try again")

                                Case "Offline", "OFFLINE", "offline"
                                    SendToPoSterminal("offline|")
                                    addtoStatusList("Terminal OFFLINE NO Card processing Possible")
                            End Select
                    End Select
                Next
            End If
        Catch ex As Exception
            addtoStatusList(ex.ToString)
            If DebugActive = "Yes" Then AddToDebugList(ex.ToString)
            SendToPoSterminal("ProcessingError")
            FromPoSTCP.Stop()
            FromPoSTCP.Start()
        End Try
    End Sub
I ask as I am considering to moving some commercial applications from Windows into Linux and this is the one that if I can not be converted would be the cancellation of the full migration.

If someone could guide me on this one (as it is one of the simple functions I can work out the rest of the other functions myself

The PoS Talks to the Module via TCP Connection at the moment but if someone knows a more quicker and stable way (that does not use files) then I am open to the idea of updating the communication method
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Using HTTP calls in Gambas 3.15

Post by cogier »

I'm not sure if this will help but if you have a look on the Gambas Farm at the programs NASA_APOD and Your Location. These programs get information from the net using APIs. Looking at your code I think you need to build the URL to include the PS_USER and PS_PASS and catch the data returned.

You will need to use gb.curl for the web part and gb.util.web to decode the JSON data. Something along the lines of: -
Public Sub GetData() As Variant                                               'Get the data
Dim sResult As String                                                         'To store the data details
Dim hClient As HttpClient                                                     'To create a HTTP Client

hClient = New HttpClient As "hClient"                                         'Create a HTTP Client
With hClient                                                                  'With the Client..
  .URL = "https://poloniex.com/public?command=returnTicker"                   'Set up the URL
  .Async = False                                                              'No asynchronous transmittion
  .TimeOut = 60                                                               'Don't hang around waiting for more than 60 seconds
  .get                                                                        'Get the data
End With

If Lof(hClient) Then sResult = Read #hClient, Lof(hClient)                    'When all the data is downloaded store it in sResult

Return JSON.Decode(sResult)                                                   'Return the decoded data

End
Post Reply