implmented http abstrction

This commit is contained in:
Djeeberjr 2022-03-04 20:00:41 +01:00
parent 1aa93574c8
commit ce45bf2726
6 changed files with 154 additions and 5 deletions

View File

@ -1,3 +1,4 @@
import kernel.http.HTTPRequest.Http;
import util.Observable;
import lib.ui.reactive.TextElement;
import lib.ui.reactive.ReactiveUI;
@ -12,7 +13,22 @@ using util.Extender.LambdaExtender;
class Startup {
public static function main() {
Init.initKernel();
uiTest();
httpTest();
}
private static function httpTest() {
Log.moveToOutput("main");
// var url = "https://mock.codes/400";
// var url = "https://jsonplaceholder.typicode.com/todos/1";
var url = "https://domainnotexsist.net/aaaa";
Http.request(url).handle((outcome)->{
switch outcome{
case Success(data):
Log.debug(data.body);
case Failure(failure):
Log.error(failure.reason);
}
});
}
private static function uiTest() {

View File

@ -1,5 +1,6 @@
package kernel;
import cc.HTTP.HTTPResponse;
import lua.TableTools;
import lua.Coroutine;
import util.Vec.Vec2;
@ -22,8 +23,8 @@ class KernelEvents {
public final onDisk:Signal<String>;
public final onDiskEject:Signal<String>;
public final onHttpCheck:Signal<{url:String, success:Bool, failReason:Any}>;
public final onHttpFailure:Signal<{url:String, failReason:String, handle:Any}>;
public final onHttpSuccess:Signal<{url:String, handle:Any}>;
public final onHttpFailure:Signal<{url:String, failReason:String, handle:HTTPResponse}>;
public final onHttpSuccess:Signal<{url:String, handle:HTTPResponse}>;
public final onKey:Signal<{keyCode:Int, isHeld:Bool}>;
public final onKeyUp:Signal<Int>;
public final onModemMessage:Signal<{
@ -60,8 +61,8 @@ class KernelEvents {
private final onDiskTrigger:SignalTrigger<String> = Signal.trigger();
private final onDiskEjectTrigger:SignalTrigger<String> = Signal.trigger();
private final onHttpCheckTrigger:SignalTrigger<{url:String, success:Bool, failReason:Any}> = Signal.trigger();
private final onHttpFailureTrigger:SignalTrigger<{url:String, failReason:String, handle:Any}> = Signal.trigger();
private final onHttpSuccessTrigger:SignalTrigger<{url:String, handle:Any}> = Signal.trigger();
private final onHttpFailureTrigger:SignalTrigger<{url:String, failReason:String, handle:HTTPResponse}> = Signal.trigger();
private final onHttpSuccessTrigger:SignalTrigger<{url:String, handle:HTTPResponse}> = Signal.trigger();
private final onKeyTrigger:SignalTrigger<{keyCode:Int, isHeld:Bool}> = Signal.trigger();
private final onKeyUpTrigger:SignalTrigger<Int> = Signal.trigger();
private final onModemMessageTrigger:SignalTrigger<{

View File

@ -0,0 +1,22 @@
package kernel.http;
using lua.Table;
class HTTPFailure {
public final reason:String;
public final statusCode:Null<StatusCode>;
public final headers:Map<String,String>;
public final body:String;
@:allow(kernel.http)
private function new(failReason: String, ?handle: cc.HTTP.HTTPResponse) {
this.reason = failReason;
if (handle != null){
this.statusCode = handle.getResponseCode();
this.headers = handle.getResponseHeaders().toMap();
this.body = handle.readAll();
handle.close();
}
}
}

View File

@ -0,0 +1,28 @@
package kernel.http;
using tink.CoreApi;
/**
Wrapper for the native
**/
class Http {
public static function request(url:String,?body:String,?options: String):Future<Outcome<HTTPResponse,HTTPFailure>> {
return new Future<Outcome<HTTPResponse,HTTPFailure>>((resolve) -> {
KernelEvents.instance.onHttpFailure.handle((params)->{
if (params.url == url){
resolve(Failure(new HTTPFailure(params.failReason,params.handle)));
}
});
KernelEvents.instance.onHttpSuccess.handle((params) -> {
if (params.url == url){
resolve(Success(new HTTPResponse(params.handle)));
}
});
cc.HTTP.request(url,body);
return null;
});
}
}

View File

@ -0,0 +1,17 @@
package kernel.http;
using lua.Table;
class HTTPResponse {
public final statusCode:StatusCode;
public final headers:Map<String,String>;
public final body:String;
@:allow(kernel.http)
private function new(handle: cc.HTTP.HTTPResponse) {
this.statusCode = handle.getResponseCode();
this.headers = handle.getResponseHeaders().toMap();
this.body = handle.readAll();
}
}

View File

@ -0,0 +1,65 @@
package kernel.http;
@:enum abstract StatusCode(Int) from Int {
var Continue = 100;
var SwitchingProtocols = 101;
var Processing = 102;
var OK = 200;
var Created = 201;
var Accepted = 202;
var NonAuthoritativeInformation = 203;
var NoContent = 204;
var ResetContent = 205;
var PartialContent = 206;
var MultiStatus = 207;
var AlreadyReported = 208;
var IMUsed = 226;
var MultipleChoices = 300;
var MovedPermanently = 301;
var Found = 302;
var SeeOther = 303;
var NotModified = 304;
var UseProxy = 305;
var SwitchProxy = 306;
var TemporaryRedirect = 307;
var PermanentRedirect = 308;
var BadRequest = 400;
var Unauthorized = 401;
var PaymentRequired = 402;
var Forbidden = 403;
var NotFound = 404;
var MethodNotAllowed = 405;
var NotAcceptable = 406;
var ProxyAuthenticationRequired = 407;
var RequestTimeout = 408;
var Conflict = 409;
var Gone = 410;
var LengthRequired = 411;
var PreconditionFailed = 412;
var PayloadTooLarge = 413;
var URITooLong = 414;
var UnsupportedMediaType = 415;
var RangeNotSatisfiable = 416;
var ExpectationFailed = 417;
var ImATeapot = 418;
var MisdirectedRequest = 421;
var UnprocessableEntity = 422;
var Locked = 423;
var FailedDependency = 424;
var UpgradeRequired = 426;
var PreconditionRequired = 428;
var TooManyRequests = 429;
var RequestHeaderFieldsTooLarge = 431;
var UnavailableForLegalReasons = 451;
var InternalServerError = 500;
var NotImplemented = 501;
var BadGateway = 502;
var ServiceUnavailable = 503;
var GatewayTimeout = 504;
var HTTPVersionNotSupported = 505;
var VariantAlsoNegotiates = 506;
var InsufficientStorage = 507;
var LoopDetected = 508;
var NotExtended = 510;
var NetworkAuthenticationRequired = 511;
}