forked from ballerina-attic/ballerina-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6_demo_external_endpoint.bal
53 lines (44 loc) · 1.35 KB
/
6_demo_external_endpoint.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
// Add another external web service endpoint
// to compensate for slowness use circuit breaker
// To run it:
// ballerina run --config twitter.toml demo.bal
// To invoke:
// curl -X POST localhost:9090
// Invoke a few times to show that it is often slow
import ballerina/config;
import ballerina/http;
import wso2/twitter;
http:Client homer = new("http://www.simpsonquotes.xyz");
twitter:Client tw = new({
clientId: config:getAsString("clientId"),
clientSecret: config:getAsString("clientSecret"),
accessToken: config:getAsString("accessToken"),
accessTokenSecret: config:getAsString("accessTokenSecret"),
clientConfig: {}
});
@http:ServiceConfig {
basePath: "/"
}
service hello on new http:Listener(9090) {
@http:ResourceConfig {
path: "/",
methods: ["POST"]
}
resource function hi (http:Caller caller, http:Request request) returns error? {
var hResp = check homer->get("/quote");
var status = check hResp.getTextPayload();
if (!status.contains("#ballerina")) {
status = status + " #ballerina";
}
var st = check tw->tweet(status);
json myJson = {
text: status,
id: st.id,
agent: "ballerina"
};
http:Response res = new;
res.setPayload(untaint myJson);
_ = check caller->respond(res);
return;
}
}