Last active
November 28, 2022 12:37
-
-
Save NilukaSripalim/1387bcbe1f22d79898d971166fe7e88f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ballerina/http; | |
import ballerina/io; | |
import ballerina/lang.runtime; | |
# A service representing a network-accessible API | |
# bound to port `9090`. | |
# | |
# + hasRisk - Field Description | |
type RiskResponse record { | |
boolean hasRisk; | |
}; | |
type RiskRequest record { | |
string ip; | |
}; | |
type ipGeolocationResp record { | |
string ip; | |
string country_code2; | |
}; | |
configurable string geoApiKey = ?; | |
service / on new http:Listener(9090) { | |
resource function post risk(@http:Payload RiskRequest req) returns RiskResponse|error? { | |
runtime:sleep(60); | |
string ip = req.ip; | |
http:Client ipGeolocation = check new ("https://api.ipgeolocation.io"); | |
ipGeolocationResp geoResponse = check ipGeolocation->get(string `/ipgeo?apiKey=${geoApiKey}&ip=${ip}&fields=country_code2`); | |
io:print(geoResponse); | |
RiskResponse resp = { | |
hasRisk: geoResponse.country_code2 != "LK" | |
}; | |
return resp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment