HTTP Client
Your GreenLightning app can also function as a HTTP client for sending REST requests, etc... to other servers and parsing the response.
Please refer to the GreenLightning-API repo for more examples like this one.
Use the code below to get started with creating a HTTP client.

Example

This is an example GreenLightning App in which we want to hit up a server somewhere containing JSON weather data (or see Simple REST Server on how to build your own REST service).
In this example, we build a simple app that performs a GET request to a local server which servers a static JSON file containing "wather data":
First, we create our client. We specify that we are also acting as a HTTP client on line 8, and create the client instance on line 11. We also declare the JSON fields we care about.
Client.java
1
public class Client implements GreenApp
2
{
3
private ClientHostPortInstance weatherSession;
4
5
@Override
6
public void declareConfiguration(Builder config) {
7
// In this example, we have TLS enabled.
8
HTTPClientConfig netClientConfig = config.useNetClient();
9
10
// We directly parse JSON of the current session.
11
weatherSession = netClientConfig.createHTTP1xClient("127.0.0.1", 443)
12
.parseJSON()
13
.stringField("condition", WeatherFields.WEATHER_CONDITIONS)
14
.decimalField("temperature", WeatherFields.TEMP_KELVIN)
15
.finish();
16
}
17
18
@Override
19
public void declareBehavior(GreenRuntime runtime) {
20
// Add the weather behavior here.
21
runtime.addResponseListener(new WeatherBehavior(runtime, weatherSession))
22
.acceptHostResponses(weatherSession);
23
}
24
25
}
Copied!
Create the field identifiers:
WeatherFields.java
1
package com.ociweb;
2
3
public enum WeatherFields {
4
WEATHER_CONDITIONS, TEMP_KELVIN
5
}
Copied!
WeatherBehavior will be responsible for sending out the request and receiving the response. We use a HTTPRequestService to do this.
WeatherBehavior.java
1
public class WeatherBehavior implements HTTPResponseListener, StartupListener, ShutdownListener {
2
// Keep track of active session for get request.
3
private ClientHostPortInstance session;
4
5
// This will actually perform the request.
6
private final HTTPRequestService clientService;
7
8
public WeatherBehavior(GreenRuntime runtime, ClientHostPortInstance session) {
9
// Create the request service.
10
clientService = runtime.newCommandChannel().newHTTPClientService();
11
this.session = session;
12
}
13
14
@Override
15
public void startup() {
16
// Send out the request on startup.
17
clientService.httpGet(session, "/api/weather");
18
}
19
20
@Override
21
public boolean acceptShutdown() {
22
// Close the session when shutting down.
23
return clientService.httpClose(session);
24
}
25
26
@Override
27
public boolean responseHTTP(HTTPResponseReader reader) {
28
// We need to open the payload data.
29
reader.openPayloadData( (r)-> {
30
// Since we want to directly parse the fields, it needs to be structured.
31
StructuredReader s = r.structured();
32
33
// Read the fields from JSON.
34
double tempKelvin = s.readDecimalAsDouble(WeatherFields.TEMP_KELVIN);
35
String conditions = s.readText(WeatherFields.WEATHER_CONDITIONS);
36
37
// Do whatever you'd like with the data here, this is just a short example.
38
// Usually, you would use a PubSub service to send this information back to
39
// another behavior.
40
System.out.println(conditions + ", " + tempKelvin);
41
});
42
43
return true;
44
}
45
}
Copied!

Methods

HTTPRequestService.httpGet(ClientHostPortInstance session, String route, optional: HeaderWritable headers)

Sends a GET request to the route (host is defined in session). Optional headers can be written.
1
clientService.httpGet(session, "/api/weather");
Copied!
1
clientService.httpGet(session, "/shopping",
2
header -> header.write(HTTPHeaderDefaults.SET_COOKIE, "__Secure-ID=123; Secure; Domain=example.com"));
Copied!
You can also write multiple headers:
1
clientService.httpGet(session, "/shopping",
2
header -> {
3
header.write(HTTPHeaderDefaults.SET_COOKIE, "__Secure-ID=123; Secure; Domain=example.com");
4
header.write(HTTPHeaderDefaults.DATE, "Wed, 21 Oct 2018 07:28:00 GMT");
5
});
Copied!

HTTPRequestService.httpPost(ClientHostPortInstance session, String route, Writable payload)

Sends a POST request to the route (host is defined in session). Optional headers can be written.
1
clientService.httpPost(session, "/post_comment", payload -> {
2
payload.writeUTF("This is a comment.");
3
});
Copied!

HTTPRequestService.httpPost(ClientHostPortInstance session, String route, HeaderWritable headers, Writable payload)

Sends a POST request to the route (host is defined in session) with custom headers.
1
clientService.httpPost(session, "/post_comment",
2
header-> header.write(HTTPHeaderDefaults.DATE, "Wed, 21 Oct 2018 07:28:00 GMT"),
3
payload -> {
4
payload.writeUTF("This is a comment.");
5
});
Copied!

HTTPRequestService.httpClose(ClientHostPortInstance session)

Closes the current session. Usually, you want to call this in the acceptShutdown() method using a ShutdownListener in your behavior.
1
@Override
2
public boolean acceptShutdown() {
3
return clientService.httpClose(session);
4
}
Copied!
Last modified 3yr ago