|
| 1 | +package com.comet.opik.api.resources.v1.priv; |
| 2 | + |
| 3 | +import com.codahale.metrics.annotation.Timed; |
| 4 | +import com.comet.opik.api.ProviderApiKey; |
| 5 | +import com.comet.opik.api.ProviderApiKeyUpdate; |
| 6 | +import com.comet.opik.api.error.ErrorMessage; |
| 7 | +import com.comet.opik.domain.LlmProviderApiKeyService; |
| 8 | +import com.comet.opik.infrastructure.auth.RequestContext; |
| 9 | +import com.fasterxml.jackson.annotation.JsonView; |
| 10 | +import io.swagger.v3.oas.annotations.Operation; |
| 11 | +import io.swagger.v3.oas.annotations.headers.Header; |
| 12 | +import io.swagger.v3.oas.annotations.media.Content; |
| 13 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 14 | +import io.swagger.v3.oas.annotations.parameters.RequestBody; |
| 15 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
| 16 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 17 | +import jakarta.inject.Inject; |
| 18 | +import jakarta.inject.Provider; |
| 19 | +import jakarta.validation.Valid; |
| 20 | +import jakarta.ws.rs.Consumes; |
| 21 | +import jakarta.ws.rs.GET; |
| 22 | +import jakarta.ws.rs.PATCH; |
| 23 | +import jakarta.ws.rs.POST; |
| 24 | +import jakarta.ws.rs.Path; |
| 25 | +import jakarta.ws.rs.PathParam; |
| 26 | +import jakarta.ws.rs.Produces; |
| 27 | +import jakarta.ws.rs.core.Context; |
| 28 | +import jakarta.ws.rs.core.MediaType; |
| 29 | +import jakarta.ws.rs.core.Response; |
| 30 | +import jakarta.ws.rs.core.UriInfo; |
| 31 | +import lombok.NonNull; |
| 32 | +import lombok.RequiredArgsConstructor; |
| 33 | +import lombok.extern.slf4j.Slf4j; |
| 34 | + |
| 35 | +import java.util.UUID; |
| 36 | + |
| 37 | +@Path("/v1/private/llm-provider-key") |
| 38 | +@Produces(MediaType.APPLICATION_JSON) |
| 39 | +@Consumes(MediaType.APPLICATION_JSON) |
| 40 | +@Timed |
| 41 | +@Slf4j |
| 42 | +@RequiredArgsConstructor(onConstructor_ = @Inject) |
| 43 | +@Tag(name = "LlmProviderKey", description = "LLM Provider Key") |
| 44 | +public class LlmProviderApiKeyResource { |
| 45 | + |
| 46 | + private final @NonNull LlmProviderApiKeyService llmProviderApiKeyService; |
| 47 | + private final @NonNull Provider<RequestContext> requestContext; |
| 48 | + |
| 49 | + @GET |
| 50 | + @Path("{id}") |
| 51 | + @Operation(operationId = "getLlmProviderApiKeyById", summary = "Get LLM Provider's ApiKey by id", description = "Get LLM Provider's ApiKey by id", responses = { |
| 52 | + @ApiResponse(responseCode = "200", description = "ProviderApiKey resource", content = @Content(schema = @Schema(implementation = ProviderApiKey.class))), |
| 53 | + @ApiResponse(responseCode = "404", description = "Not found", content = @Content(schema = @Schema(implementation = ErrorMessage.class)))}) |
| 54 | + @JsonView({ProviderApiKey.View.Public.class}) |
| 55 | + public Response getById(@PathParam("id") UUID id) { |
| 56 | + |
| 57 | + String workspaceId = requestContext.get().getWorkspaceId(); |
| 58 | + |
| 59 | + log.info("Getting Provider's ApiKey by id '{}' on workspace_id '{}'", id, workspaceId); |
| 60 | + |
| 61 | + ProviderApiKey providerApiKey = llmProviderApiKeyService.get(id, workspaceId); |
| 62 | + |
| 63 | + log.info("Got Provider's ApiKey by id '{}' on workspace_id '{}'", id, workspaceId); |
| 64 | + |
| 65 | + return Response.ok().entity(providerApiKey).build(); |
| 66 | + } |
| 67 | + |
| 68 | + @POST |
| 69 | + @Operation(operationId = "storeLlmProviderApiKey", summary = "Store LLM Provider's ApiKey", description = "Store LLM Provider's ApiKey", responses = { |
| 70 | + @ApiResponse(responseCode = "201", description = "Created", headers = { |
| 71 | + @Header(name = "Location", required = true, example = "${basePath}/v1/private/proxy/api_key/{apiKeyId}", schema = @Schema(implementation = String.class))}), |
| 72 | + @ApiResponse(responseCode = "401", description = "Bad Request", content = @Content(schema = @Schema(implementation = ErrorMessage.class))), |
| 73 | + @ApiResponse(responseCode = "403", description = "Access forbidden", content = @Content(schema = @Schema(implementation = ErrorMessage.class))) |
| 74 | + }) |
| 75 | + public Response saveApiKey( |
| 76 | + @RequestBody(content = @Content(schema = @Schema(implementation = ProviderApiKey.class))) @JsonView(ProviderApiKey.View.Write.class) @Valid ProviderApiKey providerApiKey, |
| 77 | + @Context UriInfo uriInfo) { |
| 78 | + String workspaceId = requestContext.get().getWorkspaceId(); |
| 79 | + String userName = requestContext.get().getUserName(); |
| 80 | + log.info("Save api key for provider '{}', on workspace_id '{}'", providerApiKey.provider(), workspaceId); |
| 81 | + var providerApiKeyId = llmProviderApiKeyService.saveApiKey(providerApiKey, userName, workspaceId).id(); |
| 82 | + log.info("Saved api key for provider '{}', on workspace_id '{}'", providerApiKey.provider(), workspaceId); |
| 83 | + |
| 84 | + var uri = uriInfo.getAbsolutePathBuilder().path("/%s".formatted(providerApiKeyId)).build(); |
| 85 | + |
| 86 | + return Response.created(uri).build(); |
| 87 | + } |
| 88 | + |
| 89 | + @PATCH |
| 90 | + @Path("{id}") |
| 91 | + @Operation(operationId = "updateLlmProviderApiKey", summary = "Update LLM Provider's ApiKey", description = "Update LLM Provider's ApiKey", responses = { |
| 92 | + @ApiResponse(responseCode = "204", description = "No Content"), |
| 93 | + @ApiResponse(responseCode = "401", description = "Bad Request", content = @Content(schema = @Schema(implementation = ErrorMessage.class))), |
| 94 | + @ApiResponse(responseCode = "403", description = "Access forbidden", content = @Content(schema = @Schema(implementation = ErrorMessage.class))), |
| 95 | + @ApiResponse(responseCode = "404", description = "Not found", content = @Content(schema = @Schema(implementation = ErrorMessage.class))) |
| 96 | + }) |
| 97 | + public Response updateApiKey(@PathParam("id") UUID id, |
| 98 | + @RequestBody(content = @Content(schema = @Schema(implementation = ProviderApiKeyUpdate.class))) @Valid ProviderApiKeyUpdate providerApiKeyUpdate) { |
| 99 | + String workspaceId = requestContext.get().getWorkspaceId(); |
| 100 | + String userName = requestContext.get().getUserName(); |
| 101 | + |
| 102 | + log.info("Updating api key for provider with id '{}' on workspaceId '{}'", id, workspaceId); |
| 103 | + llmProviderApiKeyService.updateApiKey(id, providerApiKeyUpdate, userName, workspaceId); |
| 104 | + log.info("Updated api key for provider with id '{}' on workspaceId '{}'", id, workspaceId); |
| 105 | + |
| 106 | + return Response.noContent().build(); |
| 107 | + } |
| 108 | +} |
0 commit comments