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

com.vital.api.resources.body.BodyClient Maven / Gradle / Ivy

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

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.body.requests.BodyGetRawRequest;
import com.vital.api.resources.body.requests.BodyGetRequest;
import com.vital.api.types.ClientBodyResponse;
import com.vital.api.types.RawBody;
import java.io.IOException;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;

public class BodyClient {
    protected final ClientOptions clientOptions;

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

    /**
     * Get Daily Body data for user_id
     */
    public ClientBodyResponse get(String userId, BodyGetRequest request, RequestOptions requestOptions) {
        HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/summary/body")
                .addPathSegment(userId);
        if (request.getProvider().isPresent()) {
            httpUrl.addQueryParameter("provider", request.getProvider().get());
        }
        httpUrl.addQueryParameter("start_date", request.getStartDate());
        if (request.getEndDate().isPresent()) {
            httpUrl.addQueryParameter("end_date", request.getEndDate().get());
        }
        Request.Builder _requestBuilder = new Request.Builder()
                .url(httpUrl.build())
                .method("GET", 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(), ClientBodyResponse.class);
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Get Daily Body data for user_id
     */
    public ClientBodyResponse get(String userId, BodyGetRequest request) {
        return get(userId, request, null);
    }

    /**
     * Get Daily Body data for user_id
     */
    public RawBody getRaw(String userId, BodyGetRawRequest request, RequestOptions requestOptions) {
        HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
                .newBuilder()
                .addPathSegments("v2/summary/body")
                .addPathSegment(userId)
                .addPathSegments("raw");
        if (request.getProvider().isPresent()) {
            httpUrl.addQueryParameter("provider", request.getProvider().get());
        }
        httpUrl.addQueryParameter("start_date", request.getStartDate());
        if (request.getEndDate().isPresent()) {
            httpUrl.addQueryParameter("end_date", request.getEndDate().get());
        }
        Request.Builder _requestBuilder = new Request.Builder()
                .url(httpUrl.build())
                .method("GET", 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(), RawBody.class);
            }
            throw new ApiError(
                    response.code(),
                    ObjectMappers.JSON_MAPPER.readValue(response.body().string(), Object.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Get Daily Body data for user_id
     */
    public RawBody getRaw(String userId, BodyGetRawRequest request) {
        return getRaw(userId, request, null);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy