Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
70 changes: 70 additions & 0 deletions src/main/java/com/merge/legacy/api/MergeApiClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.merge.legacy.api;

import com.merge.legacy.api.MergeApiClientBuilder;
import com.merge.legacy.api.core.ClientOptions;
import com.merge.legacy.api.core.Suppliers;
import com.merge.legacy.api.resources.accounting.AccountingClient;
import com.merge.legacy.api.resources.ats.AtsClient;
import com.merge.legacy.api.resources.crm.CrmClient;
import com.merge.legacy.api.resources.filestorage.FilestorageClient;
import com.merge.legacy.api.resources.hris.HrisClient;
import com.merge.legacy.api.resources.ticketing.TicketingClient;

import java.util.function.Supplier;

public class MergeApiClient {
protected final ClientOptions clientOptions;

protected final Supplier<AtsClient> atsClient;

protected final Supplier<CrmClient> crmClient;

protected final Supplier<FilestorageClient> filestorageClient;

protected final Supplier<TicketingClient> ticketingClient;

protected final Supplier<HrisClient> hrisClient;

protected final Supplier<AccountingClient> accountingClient;

public MergeApiClient(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
this.atsClient = Suppliers.memoize(() -> new AtsClient(clientOptions));
this.crmClient = Suppliers.memoize(() -> new CrmClient(clientOptions));
this.filestorageClient = Suppliers.memoize(() -> new FilestorageClient(clientOptions));
this.ticketingClient = Suppliers.memoize(() -> new TicketingClient(clientOptions));
this.hrisClient = Suppliers.memoize(() -> new HrisClient(clientOptions));
this.accountingClient = Suppliers.memoize(() -> new AccountingClient(clientOptions));
}

public AtsClient ats() {
return this.atsClient.get();
}

public CrmClient crm() {
return this.crmClient.get();
}

public FilestorageClient filestorage() {
return this.filestorageClient.get();
}

public TicketingClient ticketing() {
return this.ticketingClient.get();
}

public HrisClient hris() {
return this.hrisClient.get();
}

public AccountingClient accounting() {
return this.accountingClient.get();
}

public static MergeApiClientBuilder builder() {
return new MergeApiClientBuilder();
}
}
64 changes: 64 additions & 0 deletions src/main/java/com/merge/legacy/api/MergeApiClientBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.merge.legacy.api;

import com.merge.legacy.api.MergeApiClient;
import com.merge.legacy.api.core.ClientOptions;
import com.merge.legacy.api.core.Environment;

public final class MergeApiClientBuilder {
private ClientOptions.Builder clientOptionsBuilder = ClientOptions.builder();

private String apiKey = null;

private String accountToken = null;

private Environment environment = Environment.PRODUCTION;

/**
* Sets apiKey
*/
public MergeApiClientBuilder apiKey(String apiKey) {
this.apiKey = apiKey;
return this;
}

/**
* Sets accountToken
*/
public MergeApiClientBuilder accountToken(String accountToken) {
this.accountToken = accountToken;
return this;
}

public MergeApiClientBuilder environment(Environment environment) {
this.environment = environment;
return this;
}

public MergeApiClientBuilder url(String url) {
this.environment = Environment.custom(url);
return this;
}

/**
* Sets the timeout (in seconds) for the client
*/
public MergeApiClientBuilder timeout(int timeout) {
this.clientOptionsBuilder.timeout(timeout);
return this;
}

public com.merge.legacy.api.MergeApiClient build() {
if (apiKey == null) {
throw new RuntimeException("Please provide apiKey");
}
this.clientOptionsBuilder.addHeader("Authorization", "Bearer " + this.apiKey);
if (accountToken != null) {
this.clientOptionsBuilder.addHeader("X-Account-Token", this.accountToken);
}
clientOptionsBuilder.environment(this.environment);
return new MergeApiClient(clientOptionsBuilder.build());
}
}
46 changes: 46 additions & 0 deletions src/main/java/com/merge/legacy/api/core/ApiError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.merge.legacy.api.core;

import com.merge.legacy.api.core.MergeException;

/**
* This exception type will be thrown for any non-2XX API responses.
*/
public class ApiError extends MergeException {
/**
* The error code of the response that triggered the exception.
*/
private final int statusCode;

/**
* The body of the response that triggered the exception.
*/
private final Object body;

public ApiError(String message, int statusCode, Object body) {
super(message);
this.statusCode = statusCode;
this.body = body;
}

/**
* @return the statusCode
*/
public int statusCode() {
return this.statusCode;
}

/**
* @return the body
*/
public Object body() {
return this.body;
}

@Override
public String toString() {
return "ApiError{" + "message: " + getMessage() + ", statusCode: " + statusCode + ", body: " + body + "}";
}
}
124 changes: 124 additions & 0 deletions src/main/java/com/merge/legacy/api/core/ClientOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.merge.legacy.api.core;

import com.merge.legacy.api.core.Environment;
import com.merge.legacy.api.core.RequestOptions;
import com.merge.legacy.api.core.RetryInterceptor;
import okhttp3.OkHttpClient;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

public final class ClientOptions {
private final Environment environment;

private final Map<String, String> headers;

private final Map<String, Supplier<String>> headerSuppliers;

private final OkHttpClient httpClient;

private final int timeout;

private ClientOptions(
Environment environment,
Map<String, String> headers,
Map<String, Supplier<String>> headerSuppliers,
OkHttpClient httpClient,
int timeout) {
this.environment = environment;
this.headers = new HashMap<>();
this.headers.putAll(headers);
this.headers.putAll(new HashMap<String, String>() {
{
put("X-Fern-Language", "JAVA");
put("X-Fern-SDK-Name", "com.merge.fern:api-sdk");
put("X-Fern-SDK-Version", "1.1.1");
}
});
this.headerSuppliers = headerSuppliers;
this.httpClient = httpClient;
this.timeout = timeout;
}

public Environment environment() {
return this.environment;
}

public Map<String, String> headers(RequestOptions requestOptions) {
Map<String, String> values = new HashMap<>(this.headers);
headerSuppliers.forEach((key, supplier) -> {
values.put(key, supplier.get());
});
if (requestOptions != null) {
values.putAll(requestOptions.getHeaders());
}
return values;
}

public OkHttpClient httpClient() {
return this.httpClient;
}

public OkHttpClient httpClientWithTimeout(RequestOptions requestOptions) {
if (requestOptions == null) {
return this.httpClient;
}
return this.httpClient
.newBuilder()
.callTimeout(requestOptions.getTimeout().get(), requestOptions.getTimeoutTimeUnit())
.connectTimeout(0, TimeUnit.SECONDS)
.writeTimeout(0, TimeUnit.SECONDS)
.readTimeout(0, TimeUnit.SECONDS)
.build();
}

public static Builder builder() {
return new Builder();
}

public static final class Builder {
private Environment environment;

private final Map<String, String> headers = new HashMap<>();

private final Map<String, Supplier<String>> headerSuppliers = new HashMap<>();

private int timeout = 60;

public Builder environment(Environment environment) {
this.environment = environment;
return this;
}

public Builder addHeader(String key, String value) {
this.headers.put(key, value);
return this;
}

public Builder addHeader(String key, Supplier<String> value) {
this.headerSuppliers.put(key, value);
return this;
}

/**
* Override the timeout in seconds. Defaults to 60 seconds.
*/
public Builder timeout(int timeout) {
this.timeout = timeout;
return this;
}

public ClientOptions build() {
OkHttpClient okhttpClient = new OkHttpClient.Builder()
.addInterceptor(new RetryInterceptor(3))
.callTimeout(this.timeout, TimeUnit.SECONDS)
.build();
return new ClientOptions(environment, headers, headerSuppliers, okhttpClient, this.timeout);
}
}
}
56 changes: 56 additions & 0 deletions src/main/java/com/merge/legacy/api/core/DateTimeDeserializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.merge.legacy.api.core;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalQueries;

/**
* Custom deserializer that handles converting ISO8601 dates into {@link OffsetDateTime} objects.
*/
class DateTimeDeserializer extends JsonDeserializer<OffsetDateTime> {
private static final SimpleModule MODULE;

static {
MODULE = new SimpleModule().addDeserializer(OffsetDateTime.class, new DateTimeDeserializer());
}

/**
* Gets a module wrapping this deserializer as an adapter for the Jackson ObjectMapper.
*
* @return A {@link SimpleModule} to be plugged onto Jackson ObjectMapper.
*/
public static SimpleModule getModule() {
return MODULE;
}

@Override
public OffsetDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException {
JsonToken token = parser.currentToken();
if (token == JsonToken.VALUE_NUMBER_INT) {
return OffsetDateTime.ofInstant(Instant.ofEpochSecond(parser.getValueAsLong()), ZoneOffset.UTC);
} else {
TemporalAccessor temporal = DateTimeFormatter.ISO_DATE_TIME.parseBest(
parser.getValueAsString(), OffsetDateTime::from, LocalDateTime::from);

if (temporal.query(TemporalQueries.offset()) == null) {
return LocalDateTime.from(temporal).atOffset(ZoneOffset.UTC);
} else {
return OffsetDateTime.from(temporal);
}
}
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/merge/legacy/api/core/Environment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.merge.legacy.api.core;

public final class Environment {
public static final Environment PRODUCTION = new Environment("https://api.merge.dev/api");

public static final Environment SANDBOX = new Environment("https://api-sandbox.merge.dev/api");

public static final Environment PRODUCTION_EU = new Environment("https://api-eu.merge.dev/api");

private final String url;

private Environment(String url) {
this.url = url;
}

public String getUrl() {
return this.url;
}

public static Environment custom(String url) {
return new Environment(url);
}
}
Loading