Skip to content

Commit

Permalink
Consolidate API exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
tadayosi committed Sep 18, 2024
1 parent f7307ef commit 92f91bf
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 90 deletions.
3 changes: 1 addition & 2 deletions examples/mnist.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.nio.file.Path;

import com.github.tadayosi.torchserve.client.impl.DefaultInference;
import com.github.tadayosi.torchserve.client.inference.invoker.ApiException;
import com.github.tadayosi.torchserve.client.model.ApiException;

public class mnist {

Expand All @@ -21,7 +21,6 @@ public static void main(String... args) throws Exception {
var result1 = inference.predictions(MNIST_MODEL, one);
System.out.println("Answer> " + result1);
} catch (ApiException e) {
System.err.println(e.getResponseBody());
e.printStackTrace();
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/register_mnist.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//DEPS com.github.tadayosi.torchserve:torchserve-client:0.1-SNAPSHOT

import com.github.tadayosi.torchserve.client.impl.DefaultManagement;
import com.github.tadayosi.torchserve.client.management.invoker.ApiException;
import com.github.tadayosi.torchserve.client.model.ApiException;
import com.github.tadayosi.torchserve.client.model.RegisterModelOptions;
import com.github.tadayosi.torchserve.client.model.SetAutoScaleOptions;

Expand All @@ -22,7 +22,7 @@ public static void main(String... args) throws Exception {
.build());
System.out.println("setAutoScale> " + response.getStatus());
} catch (ApiException e) {
System.err.println(e.getResponseBody());
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.tadayosi.torchserve.client;

import com.github.tadayosi.torchserve.client.model.API;
import com.github.tadayosi.torchserve.client.model.Api;
import com.github.tadayosi.torchserve.client.model.ApiException;
import com.github.tadayosi.torchserve.client.model.Response;

/**
Expand All @@ -11,26 +12,26 @@ public interface Inference {
/**
* Get openapi description.
*/
API apiDescription() throws Exception;
Api apiDescription() throws ApiException;

/**
* Get TorchServe status.
*/
Response ping() throws Exception;
Response ping() throws ApiException;

/**
* Predictions entry point to get inference using default model version.
*/
Object predictions(String modelName, Object body) throws Exception;
Object predictions(String modelName, Object body) throws ApiException;

/**
* Predictions entry point to get inference using specific model version.
*/
Object predictions(String modelName, String modelVersion, Object body) throws Exception;
Object predictions(String modelName, String modelVersion, Object body) throws ApiException;

/**
* Not supported yet.
*/
Object explanations(String modelName) throws Exception;
Object explanations(String modelName) throws ApiException;

}
25 changes: 13 additions & 12 deletions src/main/java/com/github/tadayosi/torchserve/client/Management.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import java.util.List;

import com.github.tadayosi.torchserve.client.model.API;
import com.github.tadayosi.torchserve.client.model.Api;
import com.github.tadayosi.torchserve.client.model.ApiException;
import com.github.tadayosi.torchserve.client.model.ModelDetail;
import com.github.tadayosi.torchserve.client.model.ModelList;
import com.github.tadayosi.torchserve.client.model.RegisterModelOptions;
Expand All @@ -18,56 +19,56 @@ public interface Management {
/**
* Register a new model in TorchServe.
*/
Response registerModel(String url, RegisterModelOptions options) throws Exception;
Response registerModel(String url, RegisterModelOptions options) throws ApiException;

/**
* Configure number of workers for a default version of a model. This is an asynchronous call by default. Caller need to call describeModel to check if the model workers has been changed.
*/
Response setAutoScale(String modelName, SetAutoScaleOptions options) throws Exception;
Response setAutoScale(String modelName, SetAutoScaleOptions options) throws ApiException;

/**
* Configure number of workers for a specified version of a model. This is an asynchronous call by default. Caller need to call describeModel to check if the model workers has been changed.
*/
Response setAutoScale(String modelName, String modelVersion, SetAutoScaleOptions options) throws Exception;
Response setAutoScale(String modelName, String modelVersion, SetAutoScaleOptions options) throws ApiException;

/**
* Provides detailed information about the default version of a model.
*/
List<ModelDetail> describeModel(String modelName) throws Exception;
List<ModelDetail> describeModel(String modelName) throws ApiException;

/**
* Provides detailed information about the specified version of a model.If "all" is specified as version, returns the details about all the versions of the model.
*/
List<ModelDetail> describeModel(String modelName, String modelVersion) throws Exception;
List<ModelDetail> describeModel(String modelName, String modelVersion) throws ApiException;

/**
* Unregister the default version of a model from TorchServe if it is the only version available. This is an asynchronous call by default. Caller can call listModels to confirm model is unregistered.
*/
Response unregisterModel(String modelName, UnregisterModelOptions options) throws Exception;
Response unregisterModel(String modelName, UnregisterModelOptions options) throws ApiException;

/**
* Unregister the specified version of a model from TorchServe. This is an asynchronous call by default. Caller can call listModels to confirm model is unregistered.
*/
Response unregisterModel(String modelName, String modelVersion, UnregisterModelOptions options) throws Exception;
Response unregisterModel(String modelName, String modelVersion, UnregisterModelOptions options) throws ApiException;

/**
* List registered models in TorchServe.
*/
ModelList listModels(Integer limit, String nextPageToken) throws Exception;
ModelList listModels(Integer limit, String nextPageToken) throws ApiException;

/**
* Set default version of a model.
*/
Response setDefault(String modelName, String modelVersion) throws Exception;
Response setDefault(String modelName, String modelVersion) throws ApiException;

/**
* Get openapi description.
*/
API apiDescription() throws Exception;
Api apiDescription() throws ApiException;

/**
* Not supported yet.
*/
Object token(String type) throws Exception;
Object token(String type) throws ApiException;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.tadayosi.torchserve.client;

import com.github.tadayosi.torchserve.client.model.ApiException;

/**
* Metrics API
*/
Expand All @@ -8,11 +10,11 @@ public interface Metrics {
/**
* Get TorchServe application metrics in prometheus format.
*/
String metrics() throws Exception;
String metrics() throws ApiException;

/**
* Get TorchServe application metrics in prometheus format.
*/
String metrics(String name) throws Exception;
String metrics(String name) throws ApiException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import com.github.tadayosi.torchserve.client.Inference;
import com.github.tadayosi.torchserve.client.inference.api.DefaultApi;
import com.github.tadayosi.torchserve.client.inference.invoker.ApiClient;
import com.github.tadayosi.torchserve.client.model.API;
import com.github.tadayosi.torchserve.client.model.Api;
import com.github.tadayosi.torchserve.client.model.ApiException;
import com.github.tadayosi.torchserve.client.model.Response;

public class DefaultInference implements Inference {
Expand All @@ -20,23 +21,39 @@ public DefaultInference(int port) {
}

@Override
public API apiDescription() throws Exception {
return API.from(api.apiDescription());
public Api apiDescription() throws ApiException {
try {
return Api.from(api.apiDescription());
} catch (com.github.tadayosi.torchserve.client.inference.invoker.ApiException e) {
throw new ApiException(e);
}
}

@Override
public Response ping() throws Exception {
return Response.from(api.ping());
public Response ping() throws ApiException {
try {
return Response.from(api.ping());
} catch (com.github.tadayosi.torchserve.client.inference.invoker.ApiException e) {
throw new ApiException(e);
}
}

@Override
public Object predictions(String modelName, Object body) throws Exception {
return api.predictions(body, modelName);
public Object predictions(String modelName, Object body) throws ApiException {
try {
return api.predictions(body, modelName);
} catch (com.github.tadayosi.torchserve.client.inference.invoker.ApiException e) {
throw new ApiException(e);
}
}

@Override
public Object predictions(String modelName, String modelVersion, Object body) throws Exception {
return api.versionPredictions(body, modelName, modelVersion);
public Object predictions(String modelName, String modelVersion, Object body) throws ApiException {
try {
return api.versionPredictions(body, modelName, modelVersion);
} catch (com.github.tadayosi.torchserve.client.inference.invoker.ApiException e) {
throw new ApiException(e);
}
}

@Override
Expand Down
Loading

0 comments on commit 92f91bf

Please sign in to comment.