
com.merge.api.resources.accounting.generatekey.GenerateKeyClient Maven / Gradle / Ivy
package com.merge.api.resources.accounting.generatekey;
import com.merge.api.core.ClientOptions;
import com.merge.api.core.ObjectMappers;
import com.merge.api.resources.accounting.generatekey.requests.GenerateRemoteKeyRequest;
import com.merge.api.resources.accounting.types.RemoteKey;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class GenerateKeyClient {
protected final ClientOptions clientOptions;
public GenerateKeyClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
}
public RemoteKey create(GenerateRemoteKeyRequest request) {
HttpUrl _httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("generate-key")
.build();
Map _requestBodyProperties = new HashMap<>();
_requestBodyProperties.put("name", request.getName());
RequestBody _requestBody;
try {
_requestBody = RequestBody.create(
ObjectMappers.JSON_MAPPER.writeValueAsBytes(_requestBodyProperties),
MediaType.parse("application/json"));
} catch (Exception e) {
throw new RuntimeException(e);
}
Request.Builder _requestBuilder = new Request.Builder()
.url(_httpUrl)
.method("POST", _requestBody)
.headers(Headers.of(clientOptions.headers()))
.addHeader("Content-Type", "application/json");
Request _request = _requestBuilder.build();
try {
Response _response = clientOptions.httpClient().newCall(_request).execute();
if (_response.isSuccessful()) {
return ObjectMappers.JSON_MAPPER.readValue(_response.body().string(), RemoteKey.class);
}
throw new RuntimeException();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy