HttpApiClient

An experimental component for working with REST apis. Note that it is a zero-argument template, so to create one, use new HttpApiClient!()(args..) or you will get "HttpApiClient is used as a type" compile errors.

This will probably not work for you yet, and I might change it significantly.

Requires arsd.jsvar.

More...

Constructors

this
this(string urlBase, string oauth2Token, string submittedContentType, HttpClient httpClient)

Members

Functions

request
HttpRequestWrapper request(string uri, HttpVerb requestMethod, ubyte[] bodyBytes)
throwOnError
var throwOnError(HttpResponse res)

Properties

rest
RestBuilder rest [@property getter]

Structs

HttpRequestWrapper
struct HttpRequestWrapper
RestBuilder
struct RestBuilder

Detailed Description

Here's a snippet to create a pull request on GitHub to Phobos:

auto github = new HttpApiClient!()("https://api.github.com/", "your personal api token here");

// create the arguments object
// see: https://developer.github.com/v3/pulls/#create-a-pull-request
var args = var.emptyObject;
args.title = "My Pull Request";
args.head = "yourusername:" ~ branchName;
args.base = "master";
// note it is ["body"] instead of .body because `body` is a D keyword
args["body"] = "My cool PR is opened by the API!";
args.maintainer_can_modify = true;

/+
	Fun fact, you can also write that:

	var args = [
		"title": "My Pull Request".var,
		"head": "yourusername:" ~ branchName.var,
		"base" : "master".var,
		"body" : "My cool PR is opened by the API!".var,
		"maintainer_can_modify": true.var
	];

	Note the .var constructor calls in there. If everything is the same type, you actually don't need that, but here since there's strings and bools, D won't allow the literal without explicit constructors to align them all.
+/

// this translates to `repos/dlang/phobos/pulls` and sends a POST request,
// containing `args` as json, then immediately grabs the json result and extracts
// the value `html_url` from it. `prUrl` is typed `var`, from arsd.jsvar.
auto prUrl = github.rest.repos.dlang.phobos.pulls.POST(args).result.html_url;

writeln("Created: ", prUrl);

Why use this instead of just building the URL? Well, of course you can! This just makes it a bit more convenient than string concatenation and manages a few headers for you.

Subtypes could potentially add static type checks too.

Meta