-
Notifications
You must be signed in to change notification settings - Fork 0
/
header-based-routing.bal
71 lines (63 loc) · 2.27 KB
/
header-based-routing.bal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import ballerina/config;
import ballerina/http;
import ballerina/log;
http:ClientConfiguration weatherEPConfig = {
followRedirects: {enabled: true, maxCount: 5},
secureSocket: {
trustStore: {
path: config:getAsString("b7a.home") +
"/bre/security/ballerinaTruststore.p12",
password: "ballerina"
}
}
};
@http:ServiceConfig {
basePath: "/hbr"
}
service headerBasedRouting on new http:Listener(9090) {
@http:ResourceConfig {
methods: ["GET"],
path: "/route"
}
resource function hbrResource(http:Caller caller, http:Request req) {
http:Client weatherEP = new ("http://samples.openweathermap.org",
weatherEPConfig);
http:Client locationEP = new ("http://www.mocky.io");
http:Request newRequest = new;
if (!req.hasHeader("x-type")) {
http:Response errorResponse = new;
errorResponse.statusCode = 500;
json errMsg = {"error": "'x-type' header is not found"};
errorResponse.setPayload(errMsg);
var result = caller->respond(errorResponse);
if (result is error) {
log:printError("Error sending response", result);
}
return;
}
string nameString = req.getHeader("x-type");
http:Response|error response;
if (nameString == "location") {
response = locationEP->post("/v2/5adddd66300000bd2a4b2912",
newRequest);
} else {
response =
weatherEP->get("/data/2.5/weather?lat=35&lon=139&appid=b1b1",
newRequest);
}
if (response is http:Response) {
var result = caller->respond(response);
if (result is error) {
log:printError("Error sending response", result);
}
} else {
http:Response errorResponse = new;
errorResponse.statusCode = 500;
errorResponse.setPayload(<string>response.detail()?.message);
var result = caller->respond(errorResponse);
if (result is error) {
log:printError("Error sending response", result);
}
}
}
}