|
1 | 1 | package com.sap.ai.sdk.core; |
2 | 2 |
|
3 | | -import com.sap.cloud.sdk.cloudplatform.connectivity.DefaultHttpDestination; |
4 | 3 | import com.sap.cloud.sdk.cloudplatform.connectivity.Destination; |
5 | | -import com.sap.cloud.sdk.cloudplatform.connectivity.DestinationProperty; |
| 4 | +import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException; |
| 5 | +import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationNotFoundException; |
6 | 6 | import com.sap.cloud.sdk.services.openapi.apiclient.ApiClient; |
7 | | -import java.util.function.Function; |
| 7 | +import java.util.function.Supplier; |
8 | 8 | import javax.annotation.Nonnull; |
9 | 9 | import lombok.AccessLevel; |
10 | | -import lombok.Getter; |
11 | 10 | import lombok.RequiredArgsConstructor; |
12 | 11 |
|
13 | 12 | /** Connectivity convenience methods for AI Core with deployment. */ |
14 | | -@RequiredArgsConstructor(access = AccessLevel.PROTECTED) |
| 13 | +@RequiredArgsConstructor(access = AccessLevel.PACKAGE) |
15 | 14 | public class AiCoreDeployment implements AiCoreDestination { |
| 15 | + private final AiCoreService aiCoreService; |
16 | 16 |
|
17 | | - private static final String AI_RESOURCE_GROUP = "URL.headers.AI-Resource-Group"; |
18 | | - |
19 | | - // the delegating AI Core Service instance |
20 | | - @Nonnull private final AiCoreService service; |
21 | | - |
22 | | - // the deployment id handler to be used, based on instance |
23 | | - @Nonnull private final Function<AiCoreDeployment, String> deploymentId; |
24 | | - |
25 | | - // the resource group, "default" if null |
26 | | - @Getter(AccessLevel.PROTECTED) |
27 | | - @Nonnull |
28 | | - private final String resourceGroup; |
| 17 | + @Nonnull private final Supplier<String> deploymentId; |
29 | 18 |
|
30 | 19 | /** |
31 | | - * Default constructor with "default" resource group. |
| 20 | + * Set the resource group. |
32 | 21 | * |
33 | | - * @param service The AI Core service. |
34 | | - * @param deploymentId The deployment id handler. |
| 22 | + * @param resourceGroup The resource group, default value "default". |
| 23 | + * @return A new instance of the AI Core service. |
35 | 24 | */ |
36 | | - public AiCoreDeployment( |
37 | | - @Nonnull final AiCoreService service, |
38 | | - @Nonnull final Function<AiCoreDeployment, String> deploymentId) { |
39 | | - this(service, deploymentId, "default"); |
| 25 | + @Nonnull |
| 26 | + public AiCoreDeployment withResourceGroup(@Nonnull final String resourceGroup) { |
| 27 | + aiCoreService.resourceGroup = resourceGroup; |
| 28 | + return this; |
40 | 29 | } |
41 | 30 |
|
42 | 31 | @Nonnull |
43 | 32 | @Override |
44 | | - public Destination destination() { |
45 | | - final var dest = service.baseDestinationHandler.apply(service); |
46 | | - final var builder = service.builderHandler.apply(service, dest); |
47 | | - destinationSetUrl(builder, dest); |
48 | | - destinationSetHeaders(builder); |
49 | | - return builder.build(); |
| 33 | + public Destination destination() throws DestinationAccessException, DestinationNotFoundException { |
| 34 | + aiCoreService.deploymentId = deploymentId.get(); |
| 35 | + return aiCoreService.destination(); |
50 | 36 | } |
51 | 37 |
|
52 | 38 | @Nonnull |
53 | 39 | @Override |
54 | 40 | public ApiClient client() { |
55 | | - final var destination = destination(); |
56 | | - return service.clientHandler.apply(service, destination); |
57 | | - } |
58 | | - |
59 | | - /** |
60 | | - * Update and set the URL for the destination. |
61 | | - * |
62 | | - * @param builder The destination builder. |
63 | | - * @param dest The original destination reference. |
64 | | - */ |
65 | | - protected void destinationSetUrl( |
66 | | - @Nonnull final DefaultHttpDestination.Builder builder, @Nonnull final Destination dest) { |
67 | | - String uri = dest.get(DestinationProperty.URI).get(); |
68 | | - if (!uri.endsWith("/")) { |
69 | | - uri = uri + "/"; |
70 | | - } |
71 | | - builder.uri(uri + "v2/inference/deployments/%s/".formatted(getDeploymentId())); |
72 | | - } |
73 | | - |
74 | | - /** |
75 | | - * Update and set the default request headers for the destination. |
76 | | - * |
77 | | - * @param builder The destination builder. |
78 | | - */ |
79 | | - protected void destinationSetHeaders(@Nonnull final DefaultHttpDestination.Builder builder) { |
80 | | - builder.property(AI_RESOURCE_GROUP, getResourceGroup()); |
81 | | - } |
82 | | - |
83 | | - /** |
84 | | - * Set the resource group. |
85 | | - * |
86 | | - * @param resourceGroup The resource group. |
87 | | - * @return A new instance of the AI Core service. |
88 | | - */ |
89 | | - @Nonnull |
90 | | - public AiCoreDeployment withResourceGroup(@Nonnull final String resourceGroup) { |
91 | | - return new AiCoreDeployment(service, deploymentId, resourceGroup); |
92 | | - } |
93 | | - |
94 | | - /** |
95 | | - * Get the deployment id. |
96 | | - * |
97 | | - * @return The deployment id. |
98 | | - */ |
99 | | - @Nonnull |
100 | | - protected String getDeploymentId() { |
101 | | - return deploymentId.apply(this); |
| 41 | + aiCoreService.deploymentId = deploymentId.get(); |
| 42 | + return aiCoreService.client(); |
102 | 43 | } |
103 | 44 | } |
0 commit comments