UserGuide

Eddie's Electronics Sample Web Service

From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!

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://eews-demos.xojo.com


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:

EEConnector.AllowCertificateValidation = False
// Send request to web service
Var content As String = EEConnector.SendSync("POST", "https://eews-demos.xojo.com/GetAllCustomers")

content = content.DefineEncoding(Encodings.UTF8)

Var json As New JSONItem(content)

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

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

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

ResultsArea.AddText(customer.Value("FirstName") + " " + customer.Value("LastName") + EndOfLine)
Next
Elseif json.HasName("GetCustomer") Then
Var customer As JSONItem = json.Value("GetCustomer")

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

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:

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

EEConnector.SetRequestContent(cust.ToString, "application/json")
EEConnector.AllowCertificateValidation = False
Var content As String = EEConnector.SendSync("POST", "https://eews-demos.xojo.com/GetCustomer")

content = content.DefineEncoding(Encodings.UTF8)

Var json As New JSONItem(content)

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

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

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

ResultsArea.AddText(customer.Value("FirstName") + " " + customer.Value("LastName") + EndOfLine)
Next
Elseif json.HasName("GetCustomer") Then
Var customer As JSONItem = json.Value("GetCustomer")

ResultsArea.AddText(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 the HandleURL event on WebApplication. The Eddie's WS uses HandleURL 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, HandleURL uses the WebResponse.Write method to send the JSON back to the caller.

See Also

UserGuide:Web Services Overview topic