https://github.com/dlsc-software-consulting-gmbh/retrofitfx
https://github.com/dlsc-software-consulting-gmbh/retrofitfx
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/dlsc-software-consulting-gmbh/retrofitfx
- Owner: dlsc-software-consulting-gmbh
- License: apache-2.0
- Created: 2024-05-21T13:33:21.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-11-07T12:55:25.000Z (6 months ago)
- Last Synced: 2024-11-26T18:11:59.959Z (5 months ago)
- Language: Java
- Size: 98.6 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RetrofitFX
RetrofitFX aims to provide a library of utilities for using the [Retrofit framework](https://square.github.io/retrofit/)
with JavaFX. Retrofit is a type-safe HTTP client for Android and Java.## ServiceInvocation
The main purpose of the `ServiceInvocation` class is to allow your application to perform a very elegant server call via
Retrofit and to process the result on the JavaFX UI thread. This utility class also allows you to define all kinds of
handlers that will be invoked in various situations, e.g. when the server returns an error code such as 500.### Example
Example: assume that `myEndpoints` defines a method called `getAllCustomers()`:
```java
public interface MyService {
@Get("customers/all-customers")
public Call> getAllCustomers();
}
```Then you can create an implementation of `MyService` via `Retrofit` like this:
```java
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.domain.com/")
.build();MyService service = retrofit.create(MyService.class);
```You can now invoke the "all customers" endpoint using the `ServiceInvocation` class like this:
```java
ServiceInvocation.create("Load customers ...", () -> myService.getAllCustomers().execute())
.onSuccess(customers -> {
// here we are back on the JavaFX thread, now do something with the customers
})
.execute();
```This code creates the invocation and executes it immediately. However, in most cases you would want to pass the newly
created invocation to the UI / the main window and execute it via a central `execute(ServiceInvocation)` method. This allows
the UI to observe the invocation and configure it properly (for example to show an error dialog whenever the call to the
endpoint failed).### Handlers / Configuration
Handlers are invoked during the lifecycle of a service invocation. They might be called because the invocation has
started or finished or an exception has occurred or a result has been received. The following lists all available handlers.* `withDelay()` - define a threshold in milliseconds before the call gets executed
* `withSimulatingFailure(boolean)` - intentionally make the call fail and trigger the onFailure() handler
* `onStart(Consumer)` - call the consumer with the name of the service invocation
* `onSuccess(Consumer)` - handler receiving the wrapped result object from the call
* `onSuccessDetailed(Consumer>)` - handler receiving the "raw" response object from the call
* `onAnyStatusCode(BiConsumer)` - handler receiving the name of the call and the raw response object
* `onStatusCode(HttpStatusCode, BiConsumer)` - call the consumer with the name of the service invocation
* `onAnyStatusCodeDefault(BiConsumer)` - handler receiving the name of the call and the raw response object
* `onStatusCodeDefault(HttpStatusCode, BiConsumer
com.dlsc.retrofitfx
retrofitfx
1.0.0```
#### Using Gradle
Add the following dependency to your build.gradle:
```groovy
implementation 'com.dlsc.retrofitfx:retrofitfx:1.0.0'
```