Skip to content

Customizing Request Properties

dmillz edited this page Feb 21, 2011 · 7 revisions

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.

Creating your own 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;
        }
    }

Using your custom IRequestBuilder

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);

For the Ambitious

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.