From 92f91bf8ef72eb4c92998712c04fafd3d9b2c42d Mon Sep 17 00:00:00 2001 From: Tadayoshi Sato Date: Wed, 18 Sep 2024 21:32:17 +0900 Subject: [PATCH] Consolidate API exceptions --- examples/mnist.java | 3 +- examples/register_mnist.java | 4 +- .../tadayosi/torchserve/client/Inference.java | 13 +- .../torchserve/client/Management.java | 25 ++-- .../tadayosi/torchserve/client/Metrics.java | 6 +- .../client/impl/DefaultInference.java | 35 +++-- .../client/impl/DefaultManagement.java | 133 +++++++++++------- .../client/impl/DefaultMetrics.java | 11 +- .../client/model/{API.java => Api.java} | 12 +- .../torchserve/client/model/ApiException.java | 44 ++++++ .../torchserve/client/ManagementTest.java | 2 +- 11 files changed, 198 insertions(+), 90 deletions(-) rename src/main/java/com/github/tadayosi/torchserve/client/model/{API.java => Api.java} (84%) create mode 100644 src/main/java/com/github/tadayosi/torchserve/client/model/ApiException.java diff --git a/examples/mnist.java b/examples/mnist.java index bc66e7a..f726593 100755 --- a/examples/mnist.java +++ b/examples/mnist.java @@ -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 { @@ -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(); } } diff --git a/examples/register_mnist.java b/examples/register_mnist.java index 6a2d932..0d17b6d 100755 --- a/examples/register_mnist.java +++ b/examples/register_mnist.java @@ -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; @@ -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(); } } } diff --git a/src/main/java/com/github/tadayosi/torchserve/client/Inference.java b/src/main/java/com/github/tadayosi/torchserve/client/Inference.java index 33f03ee..016bf49 100644 --- a/src/main/java/com/github/tadayosi/torchserve/client/Inference.java +++ b/src/main/java/com/github/tadayosi/torchserve/client/Inference.java @@ -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; /** @@ -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; } diff --git a/src/main/java/com/github/tadayosi/torchserve/client/Management.java b/src/main/java/com/github/tadayosi/torchserve/client/Management.java index 8fcb1a8..66556c7 100644 --- a/src/main/java/com/github/tadayosi/torchserve/client/Management.java +++ b/src/main/java/com/github/tadayosi/torchserve/client/Management.java @@ -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; @@ -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 describeModel(String modelName) throws Exception; + List 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 describeModel(String modelName, String modelVersion) throws Exception; + List 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; } diff --git a/src/main/java/com/github/tadayosi/torchserve/client/Metrics.java b/src/main/java/com/github/tadayosi/torchserve/client/Metrics.java index 296e4f3..884cb64 100644 --- a/src/main/java/com/github/tadayosi/torchserve/client/Metrics.java +++ b/src/main/java/com/github/tadayosi/torchserve/client/Metrics.java @@ -1,5 +1,7 @@ package com.github.tadayosi.torchserve.client; +import com.github.tadayosi.torchserve.client.model.ApiException; + /** * Metrics API */ @@ -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; } diff --git a/src/main/java/com/github/tadayosi/torchserve/client/impl/DefaultInference.java b/src/main/java/com/github/tadayosi/torchserve/client/impl/DefaultInference.java index 69ecd94..a2853b6 100644 --- a/src/main/java/com/github/tadayosi/torchserve/client/impl/DefaultInference.java +++ b/src/main/java/com/github/tadayosi/torchserve/client/impl/DefaultInference.java @@ -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 { @@ -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 diff --git a/src/main/java/com/github/tadayosi/torchserve/client/impl/DefaultManagement.java b/src/main/java/com/github/tadayosi/torchserve/client/impl/DefaultManagement.java index 560bc03..067e858 100644 --- a/src/main/java/com/github/tadayosi/torchserve/client/impl/DefaultManagement.java +++ b/src/main/java/com/github/tadayosi/torchserve/client/impl/DefaultManagement.java @@ -1,13 +1,12 @@ package com.github.tadayosi.torchserve.client.impl; import java.util.List; -import java.util.Map; import com.github.tadayosi.torchserve.client.Management; import com.github.tadayosi.torchserve.client.management.api.DefaultApi; import com.github.tadayosi.torchserve.client.management.invoker.ApiClient; -import com.github.tadayosi.torchserve.client.model.API; -import com.github.tadayosi.torchserve.client.model.Model; +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; @@ -29,81 +28,121 @@ public DefaultManagement(int port) { } @Override - public Response registerModel(String url, RegisterModelOptions options) throws Exception { - return Response.from(api.registerModel(url, null, - options.getModelName(), - options.getHandler(), - options.getRuntime(), - options.getBatchSize(), - options.getMaxBatchDelay(), - options.getResponseTimeout(), - options.getInitialWorkers(), - options.getSynchronous(), - options.getS3SseKms())); + public Response registerModel(String url, RegisterModelOptions options) throws ApiException { + try { + return Response.from(api.registerModel(url, null, + options.getModelName(), + options.getHandler(), + options.getRuntime(), + options.getBatchSize(), + options.getMaxBatchDelay(), + options.getResponseTimeout(), + options.getInitialWorkers(), + options.getSynchronous(), + options.getS3SseKms())); + } catch (com.github.tadayosi.torchserve.client.management.invoker.ApiException e) { + throw new ApiException(e); + } } @Override - public Response setAutoScale(String modelName, SetAutoScaleOptions options) throws Exception { - return Response.from(api.setAutoScale(modelName, - options.getMinWorker(), - options.getMaxWorker(), - options.getNumberGpu(), - options.getSynchronous(), - options.getTimeout())); + public Response setAutoScale(String modelName, SetAutoScaleOptions options) throws ApiException { + try { + return Response.from(api.setAutoScale(modelName, + options.getMinWorker(), + options.getMaxWorker(), + options.getNumberGpu(), + options.getSynchronous(), + options.getTimeout())); + } catch (com.github.tadayosi.torchserve.client.management.invoker.ApiException e) { + throw new ApiException(e); + } } @Override - public Response setAutoScale(String modelName, String modelVersion, SetAutoScaleOptions options) throws Exception { - return Response.from(api.versionSetAutoScale(modelName, modelVersion, - options.getMinWorker(), - options.getMaxWorker(), - options.getNumberGpu(), - options.getSynchronous(), - options.getTimeout())); + public Response setAutoScale(String modelName, String modelVersion, SetAutoScaleOptions options) throws ApiException { + try { + return Response.from(api.versionSetAutoScale(modelName, modelVersion, + options.getMinWorker(), + options.getMaxWorker(), + options.getNumberGpu(), + options.getSynchronous(), + options.getTimeout())); + } catch (com.github.tadayosi.torchserve.client.management.invoker.ApiException e) { + throw new ApiException(e); + } } @Override - public List describeModel(String modelName) throws Exception { - return ModelDetail.from(api.describeModel(modelName)); + public List describeModel(String modelName) throws ApiException { + try { + return ModelDetail.from(api.describeModel(modelName)); + } catch (com.github.tadayosi.torchserve.client.management.invoker.ApiException e) { + throw new ApiException(e); + } } @Override - public List describeModel(String modelName, String modelVersion) throws Exception { - return ModelDetail.from(api.versionDescribeModel(modelName, modelVersion)); + public List describeModel(String modelName, String modelVersion) throws ApiException { + try { + return ModelDetail.from(api.versionDescribeModel(modelName, modelVersion)); + } catch (com.github.tadayosi.torchserve.client.management.invoker.ApiException e) { + throw new ApiException(e); + } } @Override - public Response unregisterModel(String modelName, UnregisterModelOptions options) throws Exception { - return Response.from(api.unregisterModel(modelName, - options.getSynchronous(), - options.getTimeout())); + public Response unregisterModel(String modelName, UnregisterModelOptions options) throws ApiException { + try { + return Response.from(api.unregisterModel(modelName, + options.getSynchronous(), + options.getTimeout())); + } catch (com.github.tadayosi.torchserve.client.management.invoker.ApiException e) { + throw new ApiException(e); + } } @Override public Response unregisterModel(String modelName, String modelVersion, UnregisterModelOptions options) - throws Exception { - return Response.from(api.versionUnregisterModel(modelName, modelVersion, - options.getSynchronous(), - options.getTimeout())); + throws ApiException { + try { + return Response.from(api.versionUnregisterModel(modelName, modelVersion, + options.getSynchronous(), + options.getTimeout())); + } catch (com.github.tadayosi.torchserve.client.management.invoker.ApiException e) { + throw new ApiException(e); + } } @Override - public ModelList listModels(Integer limit, String nextPageToken) throws Exception { - return ModelList.from(api.listModels(limit, nextPageToken)); + public ModelList listModels(Integer limit, String nextPageToken) throws ApiException { + try { + return ModelList.from(api.listModels(limit, nextPageToken)); + } catch (com.github.tadayosi.torchserve.client.management.invoker.ApiException e) { + throw new ApiException(e); + } } @Override - public Response setDefault(String modelName, String modelVersion) throws Exception { - return Response.from(api.setDefault(modelName, modelVersion)); + public Response setDefault(String modelName, String modelVersion) throws ApiException { + try { + return Response.from(api.setDefault(modelName, modelVersion)); + } catch (com.github.tadayosi.torchserve.client.management.invoker.ApiException e) { + throw new ApiException(e); + } } @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.management.invoker.ApiException e) { + throw new ApiException(e); + } } @Override - public Object token(String type) throws Exception { + public Object token(String type) throws ApiException { throw new UnsupportedOperationException("Not supported yet"); } diff --git a/src/main/java/com/github/tadayosi/torchserve/client/impl/DefaultMetrics.java b/src/main/java/com/github/tadayosi/torchserve/client/impl/DefaultMetrics.java index 050f821..872e154 100644 --- a/src/main/java/com/github/tadayosi/torchserve/client/impl/DefaultMetrics.java +++ b/src/main/java/com/github/tadayosi/torchserve/client/impl/DefaultMetrics.java @@ -3,6 +3,7 @@ import com.github.tadayosi.torchserve.client.Metrics; import com.github.tadayosi.torchserve.client.metrics.api.DefaultApi; import com.github.tadayosi.torchserve.client.metrics.invoker.ApiClient; +import com.github.tadayosi.torchserve.client.model.ApiException; public class DefaultMetrics implements Metrics { @@ -18,13 +19,17 @@ public DefaultMetrics(int port) { } @Override - public String metrics() throws Exception { + public String metrics() throws ApiException { return metrics(null); } @Override - public String metrics(String name) throws Exception { - return api.metrics(name); + public String metrics(String name) throws ApiException { + try { + return api.metrics(name); + } catch (com.github.tadayosi.torchserve.client.metrics.invoker.ApiException e) { + throw new ApiException(e); + } } } diff --git a/src/main/java/com/github/tadayosi/torchserve/client/model/API.java b/src/main/java/com/github/tadayosi/torchserve/client/model/Api.java similarity index 84% rename from src/main/java/com/github/tadayosi/torchserve/client/model/API.java rename to src/main/java/com/github/tadayosi/torchserve/client/model/Api.java index 20b576e..90aded2 100644 --- a/src/main/java/com/github/tadayosi/torchserve/client/model/API.java +++ b/src/main/java/com/github/tadayosi/torchserve/client/model/Api.java @@ -3,18 +3,18 @@ import java.util.HashMap; import java.util.Map; -public class API { +public class Api { private String openapi = null; private Map info = new HashMap<>(); private Map paths = new HashMap<>(); - public API() { + public Api() { } @SuppressWarnings("unchecked") - public static API from(com.github.tadayosi.torchserve.client.inference.model.InlineResponse200 src) { - API api = new API(); + public static Api from(com.github.tadayosi.torchserve.client.inference.model.InlineResponse200 src) { + Api api = new Api(); api.setOpenapi(src.getOpenapi()); api.setInfo((Map) src.getInfo()); api.setPaths((Map) src.getPaths()); @@ -22,8 +22,8 @@ public static API from(com.github.tadayosi.torchserve.client.inference.model.Inl } @SuppressWarnings("unchecked") - public static API from(com.github.tadayosi.torchserve.client.management.model.InlineResponse200 src) { - API api = new API(); + public static Api from(com.github.tadayosi.torchserve.client.management.model.InlineResponse200 src) { + Api api = new Api(); api.setOpenapi(src.getOpenapi()); api.setInfo((Map) src.getInfo()); api.setPaths((Map) src.getPaths()); diff --git a/src/main/java/com/github/tadayosi/torchserve/client/model/ApiException.java b/src/main/java/com/github/tadayosi/torchserve/client/model/ApiException.java new file mode 100644 index 0000000..db57eb6 --- /dev/null +++ b/src/main/java/com/github/tadayosi/torchserve/client/model/ApiException.java @@ -0,0 +1,44 @@ +package com.github.tadayosi.torchserve.client.model; + +import java.util.List; +import java.util.Map; + +public class ApiException extends Exception { + + private int code = 0; + private Map> responseHeaders = null; + private String responseBody = null; + + public ApiException(com.github.tadayosi.torchserve.client.inference.invoker.ApiException e) { + super(e.getResponseBody(), e); + this.code = e.getCode(); + this.responseHeaders = e.getResponseHeaders(); + this.responseBody = e.getResponseBody(); + } + + public ApiException(com.github.tadayosi.torchserve.client.management.invoker.ApiException e) { + super(e.getResponseBody(), e); + this.code = e.getCode(); + this.responseHeaders = e.getResponseHeaders(); + this.responseBody = e.getResponseBody(); + } + + public ApiException(com.github.tadayosi.torchserve.client.metrics.invoker.ApiException e) { + super(e.getResponseBody(), e); + this.code = e.getCode(); + this.responseHeaders = e.getResponseHeaders(); + this.responseBody = e.getResponseBody(); + } + + public int getCode() { + return code; + } + + public Map> getResponseHeaders() { + return responseHeaders; + } + + public String getResponseBody() { + return responseBody; + } +} diff --git a/src/test/java/com/github/tadayosi/torchserve/client/ManagementTest.java b/src/test/java/com/github/tadayosi/torchserve/client/ManagementTest.java index dbe6933..bbd6a7b 100644 --- a/src/test/java/com/github/tadayosi/torchserve/client/ManagementTest.java +++ b/src/test/java/com/github/tadayosi/torchserve/client/ManagementTest.java @@ -5,7 +5,7 @@ import com.github.tadayosi.torchserve.client.impl.DefaultInference; 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; import com.github.tadayosi.torchserve.client.model.UnregisterModelOptions;