forked from chamodshehanka/BackContact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contact_service.bal
69 lines (61 loc) · 1.95 KB
/
Contact_service.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
// A system package containing protocol access constructs
// Package objects referenced with 'http:' in code
import ballerina/http;
import marcus/gcontacts3;
import ballerina/config;
import ballerina/io;
documentation {
A service endpoint represents a listener.
}
endpoint http:Listener listener {
port:9090
};
endpoint gcontacts3:Client gContactsEP {
clientConfig:{
auth:{
accessToken:config:getAsString("ACCESS_TOKEN"),
clientId:config:getAsString("CLIENT_ID"),
clientSecret:config:getAsString("CLIENT_SECRET"),
refreshToken:config:getAsString("REFRESH_TOKEN")
}
}
};
documentation {
A service is a network-accessible API
Advertised on '/hello', port comes from listener endpoint
}
function validate(string input) returns @untainted string {
string regEx = "[^a-zA-Z]";
return input.replace(regEx, "");
}
service<http:Service> serviceMain bind listener {
documentation {
A resource is an invokable API method
Accessible at '/hello/sayHello
'caller' is the client invoking this resource
P{{caller}} Server Connector
P{{request}} Request
}
getContent (endpoint caller, http:Request request) {
string userEmail = "default";
var response = gContactsEP -> getAllContacts(userEmail);
string name;
match response {
xml xmlRes => {
name = xmlRes.elements().getTextValue();
}
error err => {
io:println(err);
}
}
string res = validate(name);
//Create object to carry data back to caller
http:Response httpresponse = new;
// Objects and structs can have function calls
httpresponse.setTextPayload(res);
// Send a response back to caller
// Errors are ignored with '_'
// -> indicates a synchronous network-bound call
_ = caller -> respond(httpresponse);
}
}