11package com .sap .ai .sdk .core ;
22
3- import static com .sap .ai .sdk .core .DestinationResolver .AI_CLIENT_TYPE_KEY ;
4- import static com .sap .ai .sdk .core .DestinationResolver .AI_CLIENT_TYPE_VALUE ;
5-
63import com .fasterxml .jackson .annotation .JsonAutoDetect ;
74import com .fasterxml .jackson .annotation .JsonInclude ;
85import com .fasterxml .jackson .annotation .PropertyAccessor ;
129import com .sap .cloud .sdk .cloudplatform .connectivity .DefaultHttpDestination ;
1310import com .sap .cloud .sdk .cloudplatform .connectivity .Destination ;
1411import com .sap .cloud .sdk .cloudplatform .connectivity .DestinationProperty ;
12+ import com .sap .cloud .sdk .cloudplatform .connectivity .HttpDestination ;
1513import com .sap .cloud .sdk .cloudplatform .connectivity .exception .DestinationAccessException ;
1614import com .sap .cloud .sdk .cloudplatform .connectivity .exception .DestinationNotFoundException ;
1715import com .sap .cloud .sdk .services .openapi .apiclient .ApiClient ;
18- import io .github .cdimascio .dotenv .Dotenv ;
1916import java .util .NoSuchElementException ;
2017import java .util .function .BiFunction ;
2118import java .util .function .Function ;
2219import javax .annotation .Nonnull ;
23- import lombok .RequiredArgsConstructor ;
2420import lombok .extern .slf4j .Slf4j ;
2521import lombok .val ;
2622import org .springframework .http .client .BufferingClientHttpRequestFactory ;
3127
3228/** Connectivity convenience methods for AI Core. */
3329@ Slf4j
34- @ RequiredArgsConstructor
3530public class AiCoreService implements AiCoreDestination {
36-
37- Function <AiCoreService , Destination > baseDestinationHandler ;
38- final BiFunction <AiCoreService , Destination , ApiClient > clientHandler ;
39- final BiFunction <AiCoreService , Destination , DefaultHttpDestination .Builder > builderHandler ;
31+ static final String AI_CLIENT_TYPE_KEY = "URL.headers.AI-Client-Type" ;
32+ static final String AI_CLIENT_TYPE_VALUE = "AI SDK Java" ;
33+ static final String AI_RESOURCE_GROUP = "URL.headers.AI-Resource-Group" ;
4034
4135 private static final DeploymentCache DEPLOYMENT_CACHE = new DeploymentCache ();
4236
43- private static final String AI_RESOURCE_GROUP = "URL.headers.AI-Resource-Group" ;
37+ @ Nonnull private final DestinationResolver destinationResolver ;
4438
45- /** loads the .env file from the root of the project */
46- private static final Dotenv DOTENV = Dotenv .configure ().ignoreIfMissing ().load ();
39+ @ Nonnull private Function <AiCoreService , HttpDestination > baseDestinationHandler ;
40+ @ Nonnull private final BiFunction <AiCoreService , HttpDestination , ApiClient > clientHandler ;
41+
42+ @ Nonnull
43+ private final BiFunction <AiCoreService , HttpDestination , DefaultHttpDestination .Builder >
44+ builderHandler ;
4745
4846 /** The resource group is defined by AiCoreDeployment.withResourceGroup(). */
4947 @ Nonnull String resourceGroup ;
@@ -53,8 +51,16 @@ public class AiCoreService implements AiCoreDestination {
5351
5452 /** The default constructor. */
5553 public AiCoreService () {
56- this (AiCoreService ::getApiClient , AiCoreService ::getDestinationBuilder , "default" , "" );
54+ this (new DestinationResolver ());
55+ }
56+
57+ AiCoreService (@ Nonnull final DestinationResolver destinationResolver ) {
58+ this .destinationResolver = destinationResolver ;
5759 baseDestinationHandler = AiCoreService ::getBaseDestination ;
60+ clientHandler = AiCoreService ::buildApiClient ;
61+ builderHandler = AiCoreService ::getDestinationBuilder ;
62+ resourceGroup = "default" ;
63+ deploymentId = "" ;
5864 }
5965
6066 @ Nonnull
@@ -66,7 +72,7 @@ public ApiClient client() {
6672
6773 @ Nonnull
6874 @ Override
69- public Destination destination () {
75+ public HttpDestination destination () {
7076 val dest = baseDestinationHandler .apply (this );
7177 val builder = builderHandler .apply (this , dest );
7278 if (!deploymentId .isEmpty ()) {
@@ -107,7 +113,7 @@ protected void destinationSetHeaders(@Nonnull final DefaultHttpDestination.Build
107113 * @return The AI Core Service based on the provided destination.
108114 */
109115 @ Nonnull
110- public AiCoreService withDestination (@ Nonnull final Destination destination ) {
116+ public AiCoreService withDestination (@ Nonnull final HttpDestination destination ) {
111117 baseDestinationHandler = service -> destination ;
112118 return this ;
113119 }
@@ -161,10 +167,9 @@ public AiCoreDeployment forDeploymentByScenario(@Nonnull final String scenarioId
161167 * @throws DestinationNotFoundException If the destination cannot be found.
162168 */
163169 @ Nonnull
164- protected Destination getBaseDestination ()
170+ protected HttpDestination getBaseDestination ()
165171 throws DestinationAccessException , DestinationNotFoundException {
166- val serviceKey = DOTENV .get ("AICORE_SERVICE_KEY" );
167- return DestinationResolver .getDestination (serviceKey );
172+ return destinationResolver .getDestination ();
168173 }
169174
170175 /**
@@ -186,14 +191,13 @@ protected DefaultHttpDestination.Builder getDestinationBuilder(
186191 }
187192
188193 /**
189- * Get a destination using the default service binding loading logic .
194+ * Build an {@link ApiClient} that can be used for executing plain REST HTTP calls .
190195 *
191- * @return The destination.
192- * @throws DestinationAccessException If the destination cannot be accessed.
193- * @throws DestinationNotFoundException If the destination cannot be found.
196+ * @param destination The destination to use as basis for the client.
197+ * @return The new API client.
194198 */
195199 @ Nonnull
196- protected ApiClient getApiClient (@ Nonnull final Destination destination ) {
200+ protected ApiClient buildApiClient (@ Nonnull final Destination destination ) {
197201 val objectMapper =
198202 new Jackson2ObjectMapperBuilder ()
199203 .modules (new JavaTimeModule ())
0 commit comments