Page 1 of 1

Using HTTP calls in Gambas 3.15

Posted: Tuesday 23rd March 2021 9:42pm
by AndyGable
Hi All,

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

The following are loaded at start up of the module

as these are unique to each client

Code: Select all

   Private Sub GetTerminalStatus()
            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")
                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
                    Select Case item.Name
                        Case "status"
                            Select Case item.Value
                                Case "AVAILABLE"
                                    GetStatus = 1
                                    addtoStatusList("Terminal Ready")

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

                                Case "Offline", "OFFLINE", "offline"
                                    addtoStatusList("Terminal OFFLINE NO Card processing Possible")
                            End Select
                    End Select
            End If
        Catch ex As Exception
            If DebugActive = "Yes" Then AddToDebugList(ex.ToString)
        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

Re: Using HTTP calls in Gambas 3.15

Posted: Thursday 25th March 2021 4:16pm
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 = ""                   '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