-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.bal
47 lines (39 loc) · 1.15 KB
/
main.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
import ballerina/http;
configurable string geocodingAPIKey = ?;
type Customer record {|
string customerId;
string address;
|};
type GeoTaggedCustomer record {|
*Customer;
decimal latitude;
decimal longitude;
|};
type Location record {
decimal lat;
decimal lng;
};
type ResultsItem record {
record {
Location location;
} geometry;
};
type GeocodeResponse record {
ResultsItem[] results;
string status;
};
final http:Client geocodingClient = check new ("https://maps.googleapis.com");
isolated service /api/v1 on new http:Listener(8080) {
resource function post customerWithGeoCode(Customer customer) returns GeoTaggedCustomer|error {
GeocodeResponse response = check geocodingClient->
/maps/api/geocode/'json(address = customer.address, key = geocodingAPIKey);
if response.status == "OK" {
return {
...customer,
latitude: response.results[0].geometry.location.lat,
longitude: response.results[0].geometry.location.lng
};
}
return error("Error while getting the location information.");
}
}