|
| 1 | +/** |
| 2 | + * This file was auto-generated by Fern from our API Definition. |
| 3 | + */ |
| 4 | + |
| 5 | +package com.langfuse.client.resources.metricsv2; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 8 | +import com.langfuse.client.core.ClientOptions; |
| 9 | +import com.langfuse.client.core.LangfuseClientApiException; |
| 10 | +import com.langfuse.client.core.LangfuseClientException; |
| 11 | +import com.langfuse.client.core.ObjectMappers; |
| 12 | +import com.langfuse.client.core.QueryStringMapper; |
| 13 | +import com.langfuse.client.core.RequestOptions; |
| 14 | +import java.io.IOException; |
| 15 | +import java.lang.Object; |
| 16 | +import java.lang.String; |
| 17 | +import okhttp3.Headers; |
| 18 | +import okhttp3.HttpUrl; |
| 19 | +import okhttp3.OkHttpClient; |
| 20 | +import okhttp3.Request; |
| 21 | +import okhttp3.Response; |
| 22 | +import okhttp3.ResponseBody; |
| 23 | +import com.langfuse.client.resources.commons.errors.AccessDeniedError; |
| 24 | +import com.langfuse.client.resources.commons.errors.Error; |
| 25 | +import com.langfuse.client.resources.commons.errors.MethodNotAllowedError; |
| 26 | +import com.langfuse.client.resources.commons.errors.NotFoundError; |
| 27 | +import com.langfuse.client.resources.commons.errors.UnauthorizedError; |
| 28 | +import com.langfuse.client.resources.metricsv2.requests.GetMetricsV2Request; |
| 29 | +import com.langfuse.client.resources.metricsv2.types.MetricsV2Response; |
| 30 | + |
| 31 | +public class MetricsV2Client { |
| 32 | + protected final ClientOptions clientOptions; |
| 33 | + |
| 34 | + public MetricsV2Client(ClientOptions clientOptions) { |
| 35 | + this.clientOptions = clientOptions; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Get metrics from the Langfuse project using a query object. V2 endpoint with optimized performance. |
| 40 | + */ |
| 41 | + public MetricsV2Response metrics(GetMetricsV2Request request) { |
| 42 | + return metrics(request,null); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Get metrics from the Langfuse project using a query object. V2 endpoint with optimized performance. |
| 47 | + */ |
| 48 | + public MetricsV2Response metrics(GetMetricsV2Request request, RequestOptions requestOptions) { |
| 49 | + HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()).newBuilder() |
| 50 | + .addPathSegments("api/public") |
| 51 | + .addPathSegments("v2/metrics");QueryStringMapper.addQueryParameter(httpUrl, "query", request.getQuery(), false); |
| 52 | + Request.Builder _requestBuilder = new Request.Builder() |
| 53 | + .url(httpUrl.build()) |
| 54 | + .method("GET", null) |
| 55 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 56 | + .addHeader("Content-Type", "application/json") |
| 57 | + .addHeader("Accept", "application/json"); |
| 58 | + Request okhttpRequest = _requestBuilder.build(); |
| 59 | + OkHttpClient client = clientOptions.httpClient(); |
| 60 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 61 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 62 | + } |
| 63 | + try (Response response = client.newCall(okhttpRequest).execute()) { |
| 64 | + ResponseBody responseBody = response.body(); |
| 65 | + if (response.isSuccessful()) { |
| 66 | + return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), MetricsV2Response.class); |
| 67 | + } |
| 68 | + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; |
| 69 | + try { |
| 70 | + switch (response.code()) { |
| 71 | + case 400:throw new Error(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); |
| 72 | + case 401:throw new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); |
| 73 | + case 403:throw new AccessDeniedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); |
| 74 | + case 404:throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); |
| 75 | + case 405:throw new MethodNotAllowedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); |
| 76 | + } |
| 77 | + } |
| 78 | + catch (JsonProcessingException ignored) { |
| 79 | + // unable to map error response, throwing generic error |
| 80 | + } |
| 81 | + throw new LangfuseClientApiException("Error with status code " + response.code(), response.code(), ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); |
| 82 | + } |
| 83 | + catch (IOException e) { |
| 84 | + throw new LangfuseClientException("Network error executing HTTP request", e); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
0 commit comments