com.vital.api.resources.testkit.TestkitClient Maven / Gradle / Ivy
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.vital.api.resources.testkit;
import com.vital.api.core.ApiError;
import com.vital.api.core.ClientOptions;
import com.vital.api.core.ObjectMappers;
import com.vital.api.core.RequestOptions;
import com.vital.api.resources.testkit.requests.CreateRegistrableTestkitOrderRequest;
import com.vital.api.resources.testkit.requests.RegisterTestkitRequest;
import com.vital.api.types.PostOrderResponse;
import java.io.IOException;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class TestkitClient {
protected final ClientOptions clientOptions;
public TestkitClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
}
public PostOrderResponse register(RegisterTestkitRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v3/order/testkit/register")
.build();
RequestBody body;
try {
body = RequestBody.create(
ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaType.parse("application/json"));
} catch (Exception e) {
throw new RuntimeException(e);
}
Request okhttpRequest = new Request.Builder()
.url(httpUrl)
.method("POST", body)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.addHeader("Content-Type", "application/json")
.build();
try {
Response response =
clientOptions.httpClient().newCall(okhttpRequest).execute();
if (response.isSuccessful()) {
return ObjectMappers.JSON_MAPPER.readValue(response.body().string(), PostOrderResponse.class);
}
throw new ApiError(
response.code(),
ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public PostOrderResponse register(RegisterTestkitRequest request) {
return register(request, null);
}
/**
* Creates an order for an unregistered testkit
*/
public PostOrderResponse createOrder(CreateRegistrableTestkitOrderRequest request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v3/order/testkit")
.build();
RequestBody body;
try {
body = RequestBody.create(
ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaType.parse("application/json"));
} catch (Exception e) {
throw new RuntimeException(e);
}
Request okhttpRequest = new Request.Builder()
.url(httpUrl)
.method("POST", body)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.addHeader("Content-Type", "application/json")
.build();
try {
Response response =
clientOptions.httpClient().newCall(okhttpRequest).execute();
if (response.isSuccessful()) {
return ObjectMappers.JSON_MAPPER.readValue(response.body().string(), PostOrderResponse.class);
}
throw new ApiError(
response.code(),
ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Creates an order for an unregistered testkit
*/
public PostOrderResponse createOrder(CreateRegistrableTestkitOrderRequest request) {
return createOrder(request, null);
}
}