91 lines
2.6 KiB
Java
91 lines
2.6 KiB
Java
package com.thebrokenrail.mtudining.api;
|
|
|
|
import android.os.Handler;
|
|
import android.os.Looper;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import com.squareup.moshi.JsonAdapter;
|
|
import com.squareup.moshi.Moshi;
|
|
import com.thebrokenrail.mtudining.util.Constants;
|
|
|
|
import java.io.IOException;
|
|
import java.util.function.Consumer;
|
|
|
|
import okhttp3.Call;
|
|
import okhttp3.Callback;
|
|
import okhttp3.OkHttpClient;
|
|
import okhttp3.Request;
|
|
import okhttp3.Response;
|
|
import okhttp3.ResponseBody;
|
|
|
|
public class Connection {
|
|
/**
|
|
* HTTP Client
|
|
*/
|
|
private final OkHttpClient client = new OkHttpClient();
|
|
|
|
/**
|
|
* Link to main thread.
|
|
*/
|
|
private final Handler mainThread = new Handler(Looper.getMainLooper());
|
|
|
|
/**
|
|
* Send API method.
|
|
* @param method API method
|
|
* @param success Callback on success
|
|
* @param error Callback on error
|
|
* @param <T> Response type
|
|
*/
|
|
public <T> void send(Method<T> method, Consumer<T> success, Runnable error) {
|
|
// Build URL
|
|
String url = Constants.API_BASE + method.getPath();
|
|
|
|
// Build Request
|
|
Request request = new Request.Builder()
|
|
.url(url)
|
|
.build();
|
|
|
|
// Call
|
|
client.newCall(request).enqueue(new Callback() {
|
|
private void error(Exception e) {
|
|
// Print Error
|
|
e.printStackTrace();
|
|
// Callback
|
|
mainThread.post(error);
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
|
error(e);
|
|
}
|
|
|
|
@Override
|
|
public void onResponse(@NonNull Call call, @NonNull Response response) {
|
|
try {
|
|
try (ResponseBody responseBody = response.body()) {
|
|
// Check Response
|
|
if (!response.isSuccessful()) {
|
|
// Run Error Callback
|
|
mainThread.post(error);
|
|
return;
|
|
}
|
|
|
|
// Deserialize Body
|
|
Moshi moshi = new Moshi.Builder().build();
|
|
JsonAdapter<T> jsonAdapter = moshi.adapter(method.getResponseClass());
|
|
assert responseBody != null;
|
|
T obj = jsonAdapter.fromJson(responseBody.string());
|
|
|
|
// Run Callback
|
|
mainThread.post(() -> success.accept(obj));
|
|
}
|
|
} catch (Exception e) {
|
|
// Run Error Callback
|
|
error(e);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|