forked from LimeChain/Fruzhin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed error handling and refactored some code
- Loading branch information
Georgi Grigorov
committed
Aug 15, 2024
1 parent
2b6de7c
commit d88d09d
Showing
2 changed files
with
16 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,36 @@ | ||
package com.limechain.teavm; | ||
|
||
import lombok.extern.java.Log; | ||
import org.teavm.interop.Async; | ||
import org.teavm.interop.AsyncCallback; | ||
import org.teavm.jso.JSBody; | ||
import org.teavm.jso.JSFunctor; | ||
import org.teavm.jso.JSObject; | ||
import org.teavm.jso.core.JSError; | ||
|
||
import java.io.IOException; | ||
import java.util.logging.Level; | ||
|
||
@Log | ||
public class HttpRequest { | ||
@JSFunctor | ||
interface HttpRequestCallback extends JSObject { | ||
void apply(JSError error, String response); | ||
} | ||
|
||
@Async | ||
public static native String asyncHttpRequest(String method, String url, JSObject body); | ||
private static void asyncHttpRequest(String method, String url, JSObject body, AsyncCallback<String> callback) { | ||
public static native String asyncHttpRequest(String method, String url, String body); | ||
|
||
private static void asyncHttpRequest(String method, String url, String body, AsyncCallback<String> callback) { | ||
createAsyncHttpRequest(method, url, body, (error, response) -> { | ||
if (error != null) { | ||
callback.error(new IOException(error.getMessage())); | ||
log.log(Level.WARNING, error.getMessage()); | ||
} else { | ||
callback.complete(response); | ||
} | ||
}); | ||
} | ||
|
||
@JSBody(params = {"method", "url", "body", "callback"}, script = "return asyncHttpRequest(method, url, body, callback);") | ||
public static native void createAsyncHttpRequest(String method, String url, JSObject body, HttpRequestCallback callback); | ||
public static native void createAsyncHttpRequest(String method, String url, String body, HttpRequestCallback callback); | ||
|
||
@JSFunctor | ||
private interface HttpRequestCallback extends JSObject { | ||
void apply(JSError error, String response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters