All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vital.api.resources.link.LinkClient Maven / Gradle / Ivy

/**
 * This file was auto-generated by Fern from our API Definition.
 */
package com.vital.api.resources.link;

import com.fasterxml.jackson.core.type.TypeReference;
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.link.requests.BeginLinkTokenRequest;
import com.vital.api.resources.link.requests.DemoConnectionCreationPayload;
import com.vital.api.resources.link.requests.EmailAuthLink;
import com.vital.api.resources.link.requests.EmailProviderAuthLink;
import com.vital.api.resources.link.requests.IndividualProviderData;
import com.vital.api.resources.link.requests.LinkCodeCreateRequest;
import com.vital.api.resources.link.requests.LinkConnectOauthProviderRequest;
import com.vital.api.resources.link.requests.LinkTokenBase;
import com.vital.api.resources.link.requests.LinkTokenExchange;
import com.vital.api.resources.link.requests.ManualConnectionData;
import com.vital.api.resources.link.requests.PasswordAuthLink;
import com.vital.api.types.ConnectionStatus;
import com.vital.api.types.DemoConnectionStatus;
import com.vital.api.types.LinkTokenExchangeResponse;
import com.vital.api.types.ManualProviders;
import com.vital.api.types.OAuthProviders;
import com.vital.api.types.PasswordProviders;
import com.vital.api.types.ProviderLinkResponse;
import com.vital.api.types.Source;
import com.vital.api.types.SourceLink;
import com.vital.api.types.VitalTokenCreatedResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LinkClient {
    protected final ClientOptions clientOptions;

    public LinkClient(ClientOptions clientOptions) {
        this.clientOptions = clientOptions;
    }

    /**
     * Endpoint to generate a user link token, to be used throughout the vital
     * link process. The vital link token is a one time use token, that
     * expires after 10 minutes. If you would like vital-link widget to launch
     * with a specific provider, pass in a provider in the body. If you would
     * like to redirect to a custom url after successful or error connection,
     * pass in your own custom redirect_url parameter.
     */
    public LinkTokenExchangeResponse token(LinkTokenExchange request, RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/link/token")
                .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(), LinkTokenExchangeResponse.class);
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Endpoint to generate a user link token, to be used throughout the vital
     * link process. The vital link token is a one time use token, that
     * expires after 10 minutes. If you would like vital-link widget to launch
     * with a specific provider, pass in a provider in the body. If you would
     * like to redirect to a custom url after successful or error connection,
     * pass in your own custom redirect_url parameter.
     */
    public LinkTokenExchangeResponse token(LinkTokenExchange request) {
        return token(request, null);
    }

    public Map isTokenValid(LinkTokenBase request, RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/link/token/isValid")
                .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(), new TypeReference>() {});
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public Map isTokenValid(LinkTokenBase request) {
        return isTokenValid(request, null);
    }

    /**
     * Generate a token to invite a user of Vital mobile app to your team
     */
    public VitalTokenCreatedResponse codeCreate(LinkCodeCreateRequest request, RequestOptions requestOptions) {
        HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/link/code/create");
        httpUrl.addQueryParameter("user_id", request.getUserId());
        if (request.getExpiresAt().isPresent()) {
            httpUrl.addQueryParameter("expires_at", request.getExpiresAt().get().toString());
        }
        Request.Builder _requestBuilder = new Request.Builder()
                .url(httpUrl.build())
                .method("POST", RequestBody.create("", null))
                .headers(Headers.of(clientOptions.headers(requestOptions)))
                .addHeader("Content-Type", "application/json");
        Request okhttpRequest = _requestBuilder.build();
        try {
            Response response =
                    clientOptions.httpClient().newCall(okhttpRequest).execute();
            if (response.isSuccessful()) {
                return ObjectMappers.JSON_MAPPER.readValue(response.body().string(), VitalTokenCreatedResponse.class);
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Generate a token to invite a user of Vital mobile app to your team
     */
    public VitalTokenCreatedResponse codeCreate(LinkCodeCreateRequest request) {
        return codeCreate(request, null);
    }

    /**
     * REQUEST_SOURCE: VITAL-LINK
     * PROVIDER_TYPE: OAUTH
     * Connect oauth providers
     */
    public Map connectOauthProvider(String provider) {
        return connectOauthProvider(
                provider, LinkConnectOauthProviderRequest.builder().build());
    }

    /**
     * REQUEST_SOURCE: VITAL-LINK
     * PROVIDER_TYPE: OAUTH
     * Connect oauth providers
     */
    public Map connectOauthProvider(
            String provider, LinkConnectOauthProviderRequest request, RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/link/connect")
                .addPathSegment(provider)
                .build();
        Request.Builder _requestBuilder = new Request.Builder()
                .url(httpUrl)
                .method("GET", null)
                .headers(Headers.of(clientOptions.headers(requestOptions)))
                .addHeader("Content-Type", "application/json");
        if (request.getVitalSdkNoRedirect().isPresent()) {
            _requestBuilder.addHeader(
                    "x-vital-sdk-no-redirect", request.getVitalSdkNoRedirect().get());
        }
        Request okhttpRequest = _requestBuilder.build();
        try {
            Response response =
                    clientOptions.httpClient().newCall(okhttpRequest).execute();
            if (response.isSuccessful()) {
                return ObjectMappers.JSON_MAPPER.readValue(
                        response.body().string(), new TypeReference>() {});
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * REQUEST_SOURCE: VITAL-LINK
     * PROVIDER_TYPE: OAUTH
     * Connect oauth providers
     */
    public Map connectOauthProvider(String provider, LinkConnectOauthProviderRequest request) {
        return connectOauthProvider(provider, request, null);
    }

    /**
     * REQUEST_SOURCE: VITAL-LINK
     * Start link token process
     */
    public Map startConnect(BeginLinkTokenRequest request, RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/link/start")
                .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(), new TypeReference>() {});
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * REQUEST_SOURCE: VITAL-LINK
     * Start link token process
     */
    public Map startConnect(BeginLinkTokenRequest request) {
        return startConnect(request, null);
    }

    /**
     * REQUEST_SOURCE: VITAL-LINK
     * Check link token state - can be hit continuously used as heartbeat
     */
    public Map tokenState(RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/link/state")
                .build();
        Request okhttpRequest = new Request.Builder()
                .url(httpUrl)
                .method("GET", null)
                .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(), new TypeReference>() {});
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * REQUEST_SOURCE: VITAL-LINK
     * Check link token state - can be hit continuously used as heartbeat
     */
    public Map tokenState() {
        return tokenState(null);
    }

    /**
     * REQUEST_SOURCE: VITAL-LINK
     * PROVIDER_TYPE: EMAIL-AUTH
     * This function is hit by vital-link to authenticate a email provider.
     */
    public ConnectionStatus emailAuth(EmailAuthLink request, RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/link/auth/email")
                .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(), ConnectionStatus.class);
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * REQUEST_SOURCE: VITAL-LINK
     * PROVIDER_TYPE: EMAIL-AUTH
     * This function is hit by vital-link to authenticate a email provider.
     */
    public ConnectionStatus emailAuth(EmailAuthLink request) {
        return emailAuth(request, null);
    }

    /**
     * REQUEST_SOURCE: VITAL-LINK
     * PROVIDER_TYPE: PASSWORD-AUTH
     * This function is hit by vital-link to authenticate a password provider.
     */
    public ConnectionStatus passwordAuth(PasswordAuthLink request, RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/link/auth")
                .build();
        Map properties = new HashMap<>();
        properties.put("username", request.getUsername());
        properties.put("password", request.getPassword());
        properties.put("provider", request.getProvider());
        properties.put("auth_type", request.getAuthType());
        RequestBody body;
        try {
            body = RequestBody.create(
                    ObjectMappers.JSON_MAPPER.writeValueAsBytes(properties), MediaType.parse("application/json"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Request.Builder _requestBuilder = new Request.Builder()
                .url(httpUrl)
                .method("POST", body)
                .headers(Headers.of(clientOptions.headers(requestOptions)))
                .addHeader("Content-Type", "application/json");
        if (request.getVitalLinkClientRegion().isPresent()) {
            _requestBuilder.addHeader(
                    "x-vital-link-client-region",
                    request.getVitalLinkClientRegion().get());
        }
        Request okhttpRequest = _requestBuilder.build();
        try {
            Response response =
                    clientOptions.httpClient().newCall(okhttpRequest).execute();
            if (response.isSuccessful()) {
                return ObjectMappers.JSON_MAPPER.readValue(response.body().string(), ConnectionStatus.class);
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * REQUEST_SOURCE: VITAL-LINK
     * PROVIDER_TYPE: PASSWORD-AUTH
     * This function is hit by vital-link to authenticate a password provider.
     */
    public ConnectionStatus passwordAuth(PasswordAuthLink request) {
        return passwordAuth(request, null);
    }

    /**
     * This endpoint generates an OAuth link for oauth provider
     */
    public Source generateOauthLink(OAuthProviders oauthProvider, RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/link/provider/oauth")
                .addPathSegment(oauthProvider.toString())
                .build();
        Request okhttpRequest = new Request.Builder()
                .url(httpUrl)
                .method("GET", null)
                .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(), Source.class);
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * This endpoint generates an OAuth link for oauth provider
     */
    public Source generateOauthLink(OAuthProviders oauthProvider) {
        return generateOauthLink(oauthProvider, null);
    }

    /**
     * This connects auth providers that are password based.
     */
    public ProviderLinkResponse connectPasswordProvider(
            PasswordProviders provider, IndividualProviderData request, RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/link/provider/password")
                .addPathSegment(provider.toString())
                .build();
        Map properties = new HashMap<>();
        properties.put("username", request.getUsername());
        properties.put("password", request.getPassword());
        RequestBody body;
        try {
            body = RequestBody.create(
                    ObjectMappers.JSON_MAPPER.writeValueAsBytes(properties), MediaType.parse("application/json"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Request.Builder _requestBuilder = new Request.Builder()
                .url(httpUrl)
                .method("POST", body)
                .headers(Headers.of(clientOptions.headers(requestOptions)))
                .addHeader("Content-Type", "application/json");
        if (request.getVitalLinkClientRegion().isPresent()) {
            _requestBuilder.addHeader(
                    "x-vital-link-client-region",
                    request.getVitalLinkClientRegion().get());
        }
        Request okhttpRequest = _requestBuilder.build();
        try {
            Response response =
                    clientOptions.httpClient().newCall(okhttpRequest).execute();
            if (response.isSuccessful()) {
                return ObjectMappers.JSON_MAPPER.readValue(response.body().string(), ProviderLinkResponse.class);
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * This connects auth providers that are password based.
     */
    public ProviderLinkResponse connectPasswordProvider(PasswordProviders provider, IndividualProviderData request) {
        return connectPasswordProvider(provider, request, null);
    }

    /**
     * This connects auth providers that are email based.
     */
    public ConnectionStatus connectEmailAuthProvider(
            String provider, EmailProviderAuthLink request, RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/link/provider/email")
                .addPathSegment(provider)
                .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(), ConnectionStatus.class);
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * This connects auth providers that are email based.
     */
    public ConnectionStatus connectEmailAuthProvider(String provider, EmailProviderAuthLink request) {
        return connectEmailAuthProvider(provider, request, null);
    }

    /**
     * GET List of all available providers given the generated link token.
     */
    public List getProviders(RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/link/providers")
                .build();
        Request okhttpRequest = new Request.Builder()
                .url(httpUrl)
                .method("GET", null)
                .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(), new TypeReference>() {});
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * GET List of all available providers given the generated link token.
     */
    public List getProviders() {
        return getProviders(null);
    }

    /**
     * REQUEST_SOURCE: CUSTOMER
     * PROVIDER_TYPE: MANUAL-PROVIDER
     * This connects auth providers that are password based.
     */
    public Map connectManualProvider(
            ManualProviders provider, ManualConnectionData request, RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/link/provider/manual")
                .addPathSegment(provider.toString())
                .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(), new TypeReference>() {});
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * REQUEST_SOURCE: CUSTOMER
     * PROVIDER_TYPE: MANUAL-PROVIDER
     * This connects auth providers that are password based.
     */
    public Map connectManualProvider(ManualProviders provider, ManualConnectionData request) {
        return connectManualProvider(provider, request, null);
    }

    /**
     * POST Connect the given Vital user to a demo provider.
     */
    public DemoConnectionStatus connectDemoProvider(
            DemoConnectionCreationPayload request, RequestOptions requestOptions) {
        HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/link/connect/demo")
                .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(), DemoConnectionStatus.class);
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * POST Connect the given Vital user to a demo provider.
     */
    public DemoConnectionStatus connectDemoProvider(DemoConnectionCreationPayload request) {
        return connectDemoProvider(request, null);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy