UserGuide

Eddie's Electronics Sample Web Service

From Xojo Documentation

In order to help you with testing a web service, the Eddie's Electronics web service is available for use with your apps. This web service is hosted on Xojo Cloud as a Xojo web app and uses a SQLite database for its data.

Usage

Base URL

All web services calls are done through the base URL:

   https://demos.xojo.com/EEWS/index.cgi/api/


fa-info-circle-32.png
You cannot call the base URL directly. You must append one of the following API calls to the URL.

GetAllCustomers

HTTP Method: GET or POST

This API call returns a JSON document containing the object "GetAllCustomers" which is a key-value store of a customer ID and its data, including FirstName, LastName, City, State and Zip.

Sample Xojo Code

Add a Generic Object to a Window layout. Change its name to EEConnector and its Super to URLConnection.

This code sends the web service request using the URLConnection:

// Send request to web service
EEConnector.Send("GET", "https://demos.xojo.com/EEWS/index.cgi/api/GetAllCustomers")

Add a TextArea to the layout (name it ResultsArea) and the ContentReceived event handler to EEConnector. This code displays the names from the JSON that is received from the web service in ResultsArea:

content = content.DefineEncoding(Encodings.UTF8)

Dim json As New JSONItem(content)
Dim customers As JSONItem = json.Value("GetAllCustomers")

Dim cnt As Integer = customers.Count
Dim names() As String = customers.Names

For Each id As String In customers.Names
Dim customer As JSONItem = customers.Value(id)

ResultsArea.AppendText(customer.Value("FirstName") + " " + customer.Value("LastName") + EndOfLine)
Next

Sample JSON Result

{
"GetAllCustomers":
    {
        "10174":
        {
            "FirstName":"Abdul",
            "LastName":"Mcconnell",
            "City":"Colorado Springs",
            "State":"CO",
            "Zip":"80935"
        },
        "10179":
        {
            "FirstName":"Abel",
            "LastName":"Alexander",
            "City":"Arcadia",
            "State":"IA",
            "Zip":"51430"
        }
    }
}

GetCustomer

HTTP Method: POST

This API call returns all the details for a single customer. You specify the customer by providing a simple JSON document (as request content) containing the ID of the customer you want.

Sample Xojo Code

This code (using the same socket used in the example above) sends a request for a specific customer's data:

Dim cust As New JSONItem
cust.Value("ID") = 10179

EEConnector.SetRequestContent(cust.ToString, "application/x-www-form-urlencoded")
EEConnector.Send("POST", "https://demos.xojo.com/EEWS/index.cgi/api/GetCustomer")

This code in the ContentReceived event handler for the URLConnection combines with the above code to parse the resulting JSON and display the name of the customer:

content = content.DefineEncoding(Encodings.UTF8)

Dim json As New JSONItem(content)

If json.HasName("GetAllCustomers") Then
Dim customers As JSONItem = json.Value("GetAllCustomers")

Dim cnt As Integer = customers.Count
Dim names() As String = customers.Names

For Each id As String In customers.Names
Dim customer As JSONItem = customers.Value(id)

ResultsArea.AppendText(customer.Value("FirstName") + " " + customer.Value("LastName") + EndOfLine)
Next
ElseIf json.HasName("GetCustomer") Then
Dim customer As JSONItem = json.Value("GetCustomer")

ResultsArea.AppendText(customer.Value("FirstName") + " " + customer.Value("LastName") + EndOfLine)
End If

Sample JSON Request

This JSON asks for information for customer ID 10179:

{"ID": 10179}

Sample JSON Result

This is the resulting JSON containing information for customer 10179:

{
    "GetCustomer":
    {
        "ID":"10179",
        "FirstName":"Abel",
        "LastName":"Alexander",
        "City":"Arcadia",
        "State":"IA",
        "Zip":"51430",
        "Phone":"1-261-529-7025",
        "Email":"elit.sed@aliquamarcu.edu",
        "Photo":"Base64EncodedData",
        "Taxable":"0"
    }
}

Implementation

Like all web services made with Xojo, the Eddie's Web Services uses on of the "Handle" events (HandleURL or HandleSpecialURL) on WebApplication. The Eddie's WS uses HandleSpecialURL to determine which of the above methods was called in the URL to the web service. It then calls out to other methods that get the data from the SQLite database and convert it to JSON.

Finally, HandleSpecialURL uses the Print method on the WebRequest to send the JSON back to the caller.

See Also

UserGuide:Web Services Overview topic; Aloe open-source project and library