Using HTTP calls in Gambas 3.15

Post

Posted
Rating:
#1 (In Topic #624)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
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

   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
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
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: -

Code (gambas)

  1. Public Sub GetData() As Variant                                               'Get the data
  2. Dim sResult As String                                                         'To store the data details
  3. Dim hClient As HttpClient                                                     'To create a HTTP Client
  4.  
  5. hClient = New HttpClient As "hClient"                                         'Create a HTTP Client
  6. With hClient                                                                  'With the Client..
  7.   .URL = "https://poloniex.com/public?command=returnTicker"                   'Set up the URL
  8.   .Async = False                                                              'No asynchronous transmittion
  9.   .TimeOut = 60                                                               'Don't hang around waiting for more than 60 seconds
  10.   .get                                                                        'Get the data
  11.  
  12. If Lof(hClient) Then sResult = Read #hClient, Lof(hClient)                    'When all the data is downloaded store it in sResult
  13.  
  14. Return JSON.Decode(sResult)                                                   'Return the decoded data
  15.  
Online now: No Back to the top
1 guest and 0 members have just viewed this.