Skip to content

Commit

Permalink
AiCoreDeployment.destinationId is a supplier
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesDuboisSAP committed Oct 17, 2024
1 parent 9f5b2cc commit dc597ee
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 30 deletions.
19 changes: 8 additions & 11 deletions core/src/main/java/com/sap/ai/sdk/core/AiCoreDeployment.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,41 @@
import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException;
import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationNotFoundException;
import com.sap.cloud.sdk.services.openapi.apiclient.ApiClient;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;

/** Connectivity convenience methods for AI Core with deployment. */
@RequiredArgsConstructor
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public class AiCoreDeployment implements AiCoreDestination {
private final AiCoreService aiCoreService;

@Nonnull private final Function<AiCoreDeployment, String> deploymentId;
@Nonnull private final Supplier<String> deploymentId;

/**
* Set the resource group.
*
* @param resourceGroup The resource group.
* @param resourceGroup The resource group, default value "default".
* @return A new instance of the AI Core service.
*/
@Nonnull
public AiCoreDeployment withResourceGroup(@Nonnull final String resourceGroup) {
aiCoreService.setResourceGroup(resourceGroup);
aiCoreService.resourceGroup = resourceGroup;
return this;
}

@Nonnull
@Override
public Destination destination() throws DestinationAccessException, DestinationNotFoundException {
aiCoreService.deploymentId = deploymentId.apply(this);
aiCoreService.deploymentId = deploymentId.get();
return aiCoreService.destination();
}

@Nonnull
@Override
public ApiClient client() {
aiCoreService.deploymentId = deploymentId.apply(this);
aiCoreService.deploymentId = deploymentId.get();
return aiCoreService.client();
}

String getResourceGroup() {
return aiCoreService.getResourceGroup();
}
}
24 changes: 6 additions & 18 deletions core/src/main/java/com/sap/ai/sdk/core/AiCoreService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
import java.util.function.BiFunction;
import java.util.function.Function;
import javax.annotation.Nonnull;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
Expand All @@ -44,14 +41,9 @@ public class AiCoreService implements AiCoreDestination {
private static final String AI_RESOURCE_GROUP = "URL.headers.AI-Resource-Group";

/** The resource group is defined by AiCoreDeployment.withResourceGroup(). */
@Getter(AccessLevel.PROTECTED)
@Setter(AccessLevel.PROTECTED)
@Nonnull
private String resourceGroup;
@Nonnull String resourceGroup;

/**
* The deployment id is queried by AiCoreDeployment.destination() or AiCoreDeployment.client().
*/
/** The deployment id is set by AiCoreDeployment.destination() or AiCoreDeployment.client(). */
@Nonnull String deploymentId;

/** The default constructor. */
Expand Down Expand Up @@ -100,7 +92,7 @@ protected void destinationSetUrl(
* @param builder The destination builder.
*/
protected void destinationSetHeaders(@Nonnull final DefaultHttpDestination.Builder builder) {
builder.property(AI_RESOURCE_GROUP, getResourceGroup());
builder.property(AI_RESOURCE_GROUP, resourceGroup);
}

/**
Expand All @@ -123,7 +115,7 @@ public AiCoreService withDestination(@Nonnull final Destination destination) {
*/
@Nonnull
public AiCoreDeployment forDeployment(@Nonnull final String deploymentId) {
return new AiCoreDeployment(this, obj -> deploymentId);
return new AiCoreDeployment(this, () -> deploymentId);
}

/**
Expand All @@ -139,9 +131,7 @@ public AiCoreDeployment forDeploymentByModel(@Nonnull final String modelName)
throws NoSuchElementException {
return new AiCoreDeployment(
this,
obj ->
DEPLOYMENT_CACHE.getDeploymentIdByModel(
this.client(), obj.getResourceGroup(), modelName));
() -> DEPLOYMENT_CACHE.getDeploymentIdByModel(this.client(), resourceGroup, modelName));
}

/**
Expand All @@ -157,9 +147,7 @@ public AiCoreDeployment forDeploymentByScenario(@Nonnull final String scenarioId
throws NoSuchElementException {
return new AiCoreDeployment(
this,
obj ->
DEPLOYMENT_CACHE.getDeploymentIdByScenario(
this.client(), obj.getResourceGroup(), scenarioId));
() -> DEPLOYMENT_CACHE.getDeploymentIdByScenario(client(), resourceGroup, scenarioId));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected ApiClient getApiClient(@Nonnull Destination destination) {
final var destination = customServiceForDeployment.destination().asHttp();
assertThat(destination.getUri()).hasToString("https://ai/v2/inference/deployments/deployment/");

final var resourceGroup = customServiceForDeployment.getResourceGroup();
final var resourceGroup = customService.resourceGroup;
assertThat(resourceGroup).isEqualTo("group");
}
}

0 comments on commit dc597ee

Please sign in to comment.