Configurable Http Clients
To make an API call, each SDK uses an HttpClient particular to that language or framework. Developers can provide their own configured instance of the HTTP client during client initialization. This gives them the ability to override default configurations used by our SDKs.
Inject A Custom Http Client
To configure the HTTP client, initialize the client instance with a custom HttpClient. This enables you to set up logging, retries, timeouts, or other advanced configurations tailored to your application's specific needs.
SDK Usage Example
Default headers can be useful for testing, custom authentication schemes, compliance, etc. For example, let's say we want to add an Authorization header for authorization and a Custom-Header for compliance or monitoring. Below are some examples that use a custom HTTP client to configure this before passing it into the SDK client.
- Java
- Python
- .NET
- Go
- Ruby
The Java SDK allows you to provide your own okhttp3.OkHttpClient instances.
import com.mycompany.myapi.MyApiClient;
import com.mycompany.myapi.Environment;
import okhttp3.OkHttpClient;
import okhttp3.Request;
OkHttpClient httpClient = new OkHttpClient.Builder()
.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", "Bearer your-access-token")
.header("Custom-Header", "CustomValue");
Request request = requestBuilder.build();
return chain.proceed(request);
})
.build();
MyApiClient client = new MyApiClient.Builder()
.httpClientConfig(configBuilder -> configBuilder
.httpClientInstance(httpClient))
.environment(Environment.PRODUCTION)
.build();
The Python SDK requires that your HTTP Client class must have a session and a timeout attribute. session refers to the Session Object from the requests library.
class CustomHttpClient:
def __init__(self, timeout=10):
self.session = requests.Session()
self.session.headers.update({
"Authorization": "Bearer your-access-token",
"Custom-Header": "CustomValue",
})
self.timeout = timeout
client = Myapi(
http_client_instance=CustomHttpClient()
)
The .NET SDK accepts an instance of HTTPClient from System.Net.Http.
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer your-access-token");
httpClient.DefaultRequestHeaders.Add("Custom-Header", "CustomValue");
var client = new MyApiClient.Builder()
.HttpClientConfig(builder =>
{
builder.HttpClientInstance(httpClient);
})
.Build();
The Go SDK accepts any implementation of the http.RoundTripper interface.
import (
"myapi"
"net/http"
)
// CustomTransport wraps another RoundTripper to add custom headers.
type CustomTransport struct {
BaseTransport http.RoundTripper
}
// RoundTrip adds headers to the outgoing request.
func (t *CustomTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// Make a copy of the request to avoid mutating the original
reqClone := req.Clone(req.Context())
// Add custom headers
reqClone.Header.Set("Authorization", "Bearer your-access-token")
reqClone.Header.Set("Custom-Header", "CustomValue")
// Use the underlying transport to make the request
return t.BaseTransport.RoundTrip(reqClone)
}
func main() {
customTransport := &CustomTransport{
BaseTransport: http.DefaultTransport,
}
client := myapi.NewClient(
myapi.CreateConfiguration(
myapi.WithHttpConfiguration(
myapi.CreateHttpConfiguration(
myapi.WithTimeout(0),
myapi.WithTransport(customTransport),
),
),
myapi.WithEnvironment(myapi.PRODUCTION),
),
)
}
The Ruby SDK allows you to use an existing faraday adapter of your choice or create your own custom one.
require 'faraday'
require 'myapi'
include MyApi
class CustomHttpClientAdapter < Faraday::Adapter::NetHttp
def call(env)
env[:request_headers]['Authorization'] = 'Bearer your-access-token'
env[:request_headers]['Custom-Header'] = 'CustomValue'
super
end
end
Faraday::Adapter.register_middleware(custom_net_http: CustomHttpClientAdapter)
client = MyApi::Client.new(
environment: Environment::PRODUCTION,
adapter: :custom_net_http
)