diff --git a/README.md b/README.md index e405a08..842535f 100644 --- a/README.md +++ b/README.md @@ -52,30 +52,35 @@ public interface MyApi { ```java import wasapi.WasapiClient; -MyApi api = WasapiClient.createService(MyApi.class, "https://api.example.com/"); +MyApiServices api = new WasapiClient.Builder() + .baseUrl("https://api.example.com/") + .build(MyApiServices.class); ``` ### 3. Make API calls using `wasapi.Caller` ```java -wasapi.Caller.call(api.getData(), new ApiCallback() { - @Override - public void onSuccess(MyResponse response) { - // handle success - } +import retrofit2.Call; +import wasapi.WasapiUtilities; + +class MyApi extends WasapiUtilities { - @Override - public void onError(Throwable t) { - // handle error + MyApiServices api = new WasapiClient.Builder() + .baseUrl("https://api.example.com/") + .build(MyApiServices.class); + + public static void main(String[] args) { + Call apicall = api.myApiCall(); + perform(apicall); } -}); +} ``` ## Structure * **`wasapi.WasapiClient`**: Handles Retrofit service instantiation and base URL setup. * **`wasapi.Caller`**: Utility to execute API calls and process callbacks in a consistent way. -* **`wasapi.WasapiUtilities`**: A place for common utilities (e.g., logging, checking response success, error parsing, etc.) +* **`wasapi.WasapiUtilities`**: A utility class that provides utility methods for building multipart requests from files, monitoring HTTP response codes and validating fields in API responses. ## Contributing diff --git a/src/main/java/wasapi/Caller.java b/src/main/java/wasapi/Caller.java index d1de4d2..f5ef8ab 100644 --- a/src/main/java/wasapi/Caller.java +++ b/src/main/java/wasapi/Caller.java @@ -74,6 +74,27 @@ protected static ReturnType perform( return response.isSuccessful() ? (ReturnType) response.body() : getErrorBody(response, errorModels); } + /** + * Performs the given call and processes the response. This method provides advanced error handling capabilities. + * + * @param call The call to be executed. This is a retrofit2.Call object, which represents a request that has been prepared for execution. + * @param errorModels Varargs parameter. Each ErrorModel class is used to try to parse the error response if the call was not successful. + * + * @return A ResponseType object. If the call was successful, this is the body of the response. If the call was not successful and strict is false, this may be a parsed error response, or null if parsing the error response failed. + * + * @throws FailedCallException If strict is true, and the call failed or the response was not successful. + * + * @param The type of the successful response body. + * @param The type of the return value in this method. This is either SuccessModel or ErrorModel. + */ + @SuppressWarnings("unchecked") + protected static ReturnType perform( + Call call, + Class... errorModels){ + Response response = call(call, false, false, getPreviousMethodName()); + return response.isSuccessful() ? (ReturnType) response.body() : getErrorBody(response, errorModels); + } + /** * Gets the response from an API call and logs the results. *