-
Notifications
You must be signed in to change notification settings - Fork 29
Customizing Request Properties
The BrightcoveApi.Configuration.RequestTimeout
setting allows one to easily adjust the HttpWebRequest.Timeout
property used when making requests to the API. Sometimes, though, you might have a need to customize other request properties. In order to do so, it's possible to swap-in your own custom IRequestBuilder
.
The easiest way to create your own IRequestBuilder is to subclass the existing implementation of BasicRequestBuilder
and override one or more of the methods. For instance, say you need to connect through a proxy. In that case, you could override the BuildRequest method, like so:
public class MyRequestBuilder : BasicRequestBuilder
{
public MyRequestBuilder(BrightcoveApiConfig configuration) : base(configuration)
{
}
public override HttpWebRequest BuildRequest(string url)
{
HttpWebRequest myRequest = base.BuildRequest(url);
// Configure the request to work through a proxy
WebProxy myProxy = new WebProxy
{
Address = new Uri("http://myproxy.example.com"),
Credentials = new NetworkCredential("proxyUsername", "proxyPassword")
};
myRequest.Proxy = myProxy;
return myRequest;
}
}
Once you have a custom IRequestBuilder, instruct the API wrapper to use it by setting the BrightcoveApi.Connector.RequestBuilder
property, like so:
BrightcoveApi api = BrightcoveApiFactory.CreateApi("my read token", "my write token");
api.Connector.RequestBuilder = new MyRequestBuilder(api.Configuration);
If you're feeling more ambitious, you don't necessarily have to subclass BasicRequestBuilder
. Any class that correctly implements the IRequestBuilder
interface will do:
public class MyRequestBuilder : IRequestBuilder
{
public HttpWebRequest BuildRequest(string url)
{
// TODO: implement me
}
public HttpWebRequest BuildPostFormRequest(string postUrl, NameValueCollection postParameters)
{
// TODO: implement me
}
public HttpWebRequest BuildMultipartFormDataPostRequest(string postUrl, NameValueCollection postParameters, FileParameter fileParameter)
{
// TODO: implement me
}
}
If, for some reason, customizing the IRequestBuilder isn't enough to get the job done, it's also possible to swap in a custom IBrightcoveApiConnector
. We don't currently have any documentation on how to do that, but if it's something you find yourself needing to do, we'd be interested to hear why.