2024-12-16 22:20:15 +00:00
|
|
|
module data_utils;
|
|
|
|
|
|
|
|
import handy_httpd;
|
|
|
|
import asdf;
|
|
|
|
|
|
|
|
public import handy_httpd.components.optional;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads a JSON payload into a type T. Throws an `HttpStatusException` if
|
|
|
|
* the data cannot be read or converted to the given type, with a 400 BAD
|
|
|
|
* REQUEST status.
|
|
|
|
* Params:
|
|
|
|
* ctx = The request context to read from.
|
|
|
|
* Returns: The data that was read.
|
|
|
|
*/
|
|
|
|
T readJsonPayload(T)(ref HttpRequestContext ctx) {
|
|
|
|
try {
|
|
|
|
string requestBody = ctx.request.readBodyAsString();
|
|
|
|
return deserialize!T(requestBody);
|
|
|
|
} catch (SerdeException e) {
|
2024-12-17 03:22:56 +00:00
|
|
|
import slf4d;
|
|
|
|
warnF!"Failed to read JSON payload: %s"(e.msg);
|
2024-12-16 22:20:15 +00:00
|
|
|
throw new HttpStatusException(HttpStatus.BAD_REQUEST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes data of type T to a JSON response body. Throws an `HttpStatusException`
|
|
|
|
* with status 501 INTERNAL SERVER ERROR if serialization fails.
|
|
|
|
* Params:
|
|
|
|
* ctx = The request context to write to.
|
|
|
|
* data = The data to write.
|
|
|
|
*/
|
|
|
|
void writeJsonBody(T)(ref HttpRequestContext ctx, in T data) {
|
2024-12-17 03:22:56 +00:00
|
|
|
import std.traits : isArray;
|
2024-12-16 22:20:15 +00:00
|
|
|
try {
|
2024-12-17 03:22:56 +00:00
|
|
|
static if (isArray!T) {
|
|
|
|
if (data.length == 0) {
|
|
|
|
ctx.response.writeBodyString("[]", "application/json");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2024-12-16 22:20:15 +00:00
|
|
|
string jsonStr = serializeToJson(data);
|
|
|
|
ctx.response.writeBodyString(jsonStr, "application/json");
|
|
|
|
} catch (SerdeException e) {
|
|
|
|
throw new HttpStatusException(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2024-12-17 03:22:56 +00:00
|
|
|
|
|
|
|
ulong getUnixTimestampMillis() {
|
|
|
|
import std.datetime;
|
|
|
|
SysTime now = Clock.currTime();
|
|
|
|
SysTime unixEpoch = SysTime(DateTime(1970, 1, 1), UTC());
|
|
|
|
Duration diff = now - unixEpoch;
|
|
|
|
return diff.total!"msecs";
|
|
|
|
}
|