XDK Live
In Mita, HTTP is defined as a many resource
. That means, multiple instances of this resource can exist, and each is given a unique name by the developer. All possible configuration items and signals for the HTTP resource are listed below.
connectivity many HttpRestClient {
generator "org.eclipse.mita.platform.xdk110.connectivity.RestClientGenerator"
validator "org.eclipse.mita.platform.xdk110.connectivity.RestClientValidator"
/**
* The underlying transport connectivity we use to send the data out. In the future we might also
* support LoRa here.
*/
required configuration-item transport : WLAN
/**
* The part of the endpoint URL common to all requests, e.g. "http://foobar.com/api/v1"
*/
required configuration-item endpointBase : string
/**
* A custom header which is added to each HTTP request. Example:
* "X-Auth: MySecretToken\nX-Version: 1.0"
*/
configuration-item headerContent : string
signal resource(endpoint : string, contentType : string = "application/json", writeMethod : HttpMethod = HttpMethod.POST, readMethod : HttpMethod = HttpMethod.GET) : string
}
Setup
HTTP requires a network connection to the destination server, to which the HTTP requests are sent. With the XDK, this network is usually established using Wi-Fi. The following code can be used to create a WLAN resource for use with HTTP:
setup wifi : WLAN {
connection = Personal;
ssid = 'mySSID';
psk = 'myPassword';
}
Setting up HTTP resources
All HTTP resources are generally set up with an endpointBase and a transport. The first configuration-item is a full URL, pointing to a desired endpoint base, such as http://www.my-backend.com:80. The configuration-item transport is the Wi-Fi resource, through which the HTTP messages are sent.
The following code can be used to create an HTTP resource:
setup googleRoot : HttpRestClient {
transport = wifi;
endpointBase = 'http://www.google.com:80';
var root = resource('/');
}
Setting up variable resource-signals
given a backend-api located at www.my-server.com:8080/api, and the resources light and temperature, the following code would initialize an HTTP instance with the specified endpoints as resource-signals:
setup myServer : HttpRestClient {
transport = wifi;
endpointBase = 'http://www.my-server/api';
var light = resource('/light');
var temperature = resource('/temperature');
}
Every read/write operation on one of the signals will only send HTTP messages to either www.my-server/api/light or www.my-server/api/temperature.
So far, the resource-signals have been initialized with default settings (apart from the endpoint). They have the following optional parameters (and default values):
contentType
, of typestring
. This defines the content-type header of the HTTP message. Per default, this is"application/json"
writeMethod
, of typeHttpMethod
. This defines which HTTP Method (GET, POST, PUT, DELETE, UPDATE) is used when the signal is accessed using thewrite
method. Per default, this isHttpMethod.POST
.readMethod
, of typeHttpMethod
. This defines which HTTP Method (GET, POST, PUT, DELETE, UPDATE) is used when the signal is accessed using theread
method. Per default, this isHttpMethod.GET
.
Headers
For an HTTP resource, additional headers can be set using the optional configuration-item headerContent
. The code below is an example for setting the headers
X-Auth: MySecretToken
X-Version: 1.0
for messages which are sent using the specifically setup HTTP resource
setup myServer : HttpRestClient {
transport = wifi;
endpointBase = 'http://www.my-server/api';
headerContent = 'X-Auth: MySecretToken\nX-Version: 1.0';
var light = resource('/light');
}
All headers are insert using a single string, where the headers are separated using the character\n.
Performing Requests
Then the following code will send a POST request (the default write method for resource-signals) to www.my-server/api/helloWorld every 5 seconds, with a JSON payload
every 5 seconds {
myServer.light.write('{"hello": "world"}');
}
The input must always be a string.
Last updated
Was this helpful?