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.
added async http request and removed old sync impl
- Loading branch information
Georgi Grigorov
committed
Aug 14, 2024
1 parent
64b0339
commit 2b6de7c
Showing
3 changed files
with
47 additions
and
16 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,8 +1,32 @@ | ||
package com.limechain.teavm; | ||
|
||
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; | ||
|
||
public class HttpRequest { | ||
@JSBody(params = {"method", "url", "body"}, script = "return httpRequestSync(method, url, body);") | ||
public static native String httpRequestSync(String method, String url, String body); | ||
@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) { | ||
createAsyncHttpRequest(method, url, body, (error, response) -> { | ||
if (error != null) { | ||
callback.error(new IOException(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); | ||
} |
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
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,15 +1,22 @@ | ||
function httpRequestSync(method, url, body) { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open(method, url, false); // false for synchronous request | ||
xhr.setRequestHeader('Content-Type', 'application/json'); | ||
if (method === 'POST' && body) { | ||
xhr.send(body); | ||
} else { | ||
xhr.send(); | ||
} | ||
if (xhr.status === 200) { | ||
return xhr.responseText; | ||
} else { | ||
throw new Error('Request failed with status ' + xhr.status); | ||
async function asyncHttpRequest(method = 'GET', url, body = null, callback) { | ||
try { | ||
const response = await fetch(url, { | ||
method: method, | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: method === 'POST' ? JSON.stringify(body) : undefined | ||
}); | ||
|
||
if (!response.ok) { | ||
callback(new Error(`Request failed with status: ${response?.status}`), null); | ||
return; | ||
} | ||
|
||
let result = await response.text(); | ||
callback(null, result); | ||
|
||
} catch (error) { | ||
callback(new Error(`Error during sending request: ${error.message}`), null); | ||
} | ||
} |