Implement basic Belfius mobile site API client

This commit is contained in:
Les De Ridder 2017-02-14 00:52:27 +01:00
commit 1812a076a7
No known key found for this signature in database
GPG Key ID: 5EC132DFA85DB372
4 changed files with 138 additions and 0 deletions

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
.dub
docs.json
__dummy.html
*.o
*.obj
__test__*__
belfius-balance

14
dub.json Normal file
View File

@ -0,0 +1,14 @@
{
"authors": [
"Les De Ridder"
],
"name": "belfius-balance",
"license": "NCSA",
"copyright": "Copyright © 2017, Les De Ridder",
"dependencies": {
"vibe-d": "~>0.7.28"
},
"versions": [
"VibeDefaultMain"
]
}

11
dub.selections.json Normal file
View File

@ -0,0 +1,11 @@
{
"fileVersion": 1,
"versions": {
"diet-ng": "1.1.3",
"libasync": "0.7.9",
"libevent": "2.0.2+2.0.16",
"memutils": "0.4.9",
"openssl": "1.1.5+1.0.1g",
"vibe-d": "0.7.30"
}
}

106
source/app.d Normal file
View File

@ -0,0 +1,106 @@
import std.stdio;
import vibe.d;
import vibe.data.json;
import vibe.textfilter.urlencode;
void helloWorld(HTTPServerRequest req,
HTTPServerResponse res)
{
res.writeBody("Hello");
}
class ApiRequest
{
}
class OpenSession : ApiRequest
{
string applicationType;
string locale;
string[string] attributes;
this(string applicationType, string locale, string[string] attributes)
{
this.applicationType = applicationType;
this.locale = locale;
this.attributes = attributes;
}
}
class ApiMessage
{
string executionMode;
string protocolVersion;
Json[string][] requests;
this(string executionMode)
{
this.executionMode = executionMode;
this.protocolVersion = "2.1";
Json[string] requestArray;
this.requests = [requestArray];
}
void addRequest(T : ApiRequest)(string name, T request)
{
requests[0][name] = request.serializeToJson;
}
}
class ApiClient
{
enum string Endpoint = "https://m.belfius.be/F2CRenderingDexiaToClient/GEPARendering/machineIdentifier=s0/";
private string _locale;
this(string locale)
{
_locale = locale;
}
void connect()
{
auto openSessionRequest = new OpenSession("yui3a", _locale, ["application": "Container", "P": "P055"]);
auto response = sendRequest("sequential", openSessionRequest);
writeln(response);
}
Json sendRequest(T : ApiRequest)(string executionMode, T request)
{
auto message = new ApiMessage(executionMode);
message.addRequest(T.stringof, request);
auto response = requestHTTP(Endpoint,
(scope request) {
request.method = HTTPMethod.POST;
request.headers["Accept"] = "*/*";
request.headers["Content-Type"] = "application/x-www-form-urlencoded";
request.headers["User-Agent"] = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36";
request.writeBody(cast(ubyte[])("request=" ~ message.serializeToJsonString.urlEncode));
}
).bodyReader.readAllUTF8;
return response.parseJsonString;
}
}
shared static this()
{
auto router = new URLRouter;
router.get("/hello", &helloWorld);
auto settings = new HTTPServerSettings;
settings.sessionStore = new MemorySessionStore;
settings.port = 8080;
settings.bindAddresses = ["::1", "127.0.0.1"];
listenHTTP(settings, router);
auto apiClient = new ApiClient("nl_BE");
apiClient.connect();
}