Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyResponse>() {
@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

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/wasapi/Caller.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ protected static <SuccessModel, ReturnType> 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 <SuccessModel> The type of the successful response body.
* @param <ReturnType> The type of the return value in this method. This is either SuccessModel or ErrorModel.
*/
@SuppressWarnings("unchecked")
protected static <SuccessModel, ReturnType> ReturnType perform(
Call<SuccessModel> 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.
*
Expand Down