Skip to content

Instantly share code, notes, and snippets.

@zaydek-old
Created July 11, 2017 08:35
Show Gist options
  • Save zaydek-old/b1b749f2b4a7bbd78acc2cc614f23d84 to your computer and use it in GitHub Desktop.
Save zaydek-old/b1b749f2b4a7bbd78acc2cc614f23d84 to your computer and use it in GitHub Desktop.
module owl_delivery.GET;
import owl_delivery;
auto GET(string url, Duration duration = seconds(1))
{
string result;
auto
http = HTTP(url);
http.operationTimeout(duration);
http.onReceive((ubyte[] data) { result = strip(cast(string) data); return data.length; });
http.perform();
return Response(
Response.Status(http.statusLine.code, http.statusLine.reason), result);
}
struct Response
{
Status status;
string result; alias result this;
this(Status status, string result)
{
this.status = status;
this.result = result;
if (status.code / 100 != 2)
throw new Exception(toStatusString());
}
auto toStatusString()
{
return format("%-(%s %s%)%s",
status, !result.length ? "" : format(" %s", toSampleString()));
}
auto toSampleString(size_t len = 80)
{
return result.length <= len ?
result : format("%s...", result[0 .. len - 3]);
}
alias Tuple!(ushort, "code", string, "info") Status;
}
import core.time, std.net.curl, std.string, std.typecons, std.format;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment