diff --git a/src/Startup.hx b/src/Startup.hx index 712522d..96c42b0 100644 --- a/src/Startup.hx +++ b/src/Startup.hx @@ -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() { diff --git a/src/kernel/KernelEvents.hx b/src/kernel/KernelEvents.hx index 8828f36..ec2d352 100644 --- a/src/kernel/KernelEvents.hx +++ b/src/kernel/KernelEvents.hx @@ -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; public final onDiskEject:Signal; 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; public final onModemMessage:Signal<{ @@ -60,8 +61,8 @@ class KernelEvents { private final onDiskTrigger:SignalTrigger = Signal.trigger(); private final onDiskEjectTrigger:SignalTrigger = 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 = Signal.trigger(); private final onModemMessageTrigger:SignalTrigger<{ diff --git a/src/kernel/http/HTTPFailure.hx b/src/kernel/http/HTTPFailure.hx new file mode 100644 index 0000000..3cdde20 --- /dev/null +++ b/src/kernel/http/HTTPFailure.hx @@ -0,0 +1,22 @@ +package kernel.http; + +using lua.Table; + +class HTTPFailure { + + public final reason:String; + public final statusCode:Null; + public final headers:Map; + 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(); + } + } +} diff --git a/src/kernel/http/HTTPRequest.hx b/src/kernel/http/HTTPRequest.hx new file mode 100644 index 0000000..a708bc1 --- /dev/null +++ b/src/kernel/http/HTTPRequest.hx @@ -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> { + return new Future>((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; + }); + } +} diff --git a/src/kernel/http/HTTPResponse.hx b/src/kernel/http/HTTPResponse.hx new file mode 100644 index 0000000..5140184 --- /dev/null +++ b/src/kernel/http/HTTPResponse.hx @@ -0,0 +1,17 @@ +package kernel.http; + +using lua.Table; + +class HTTPResponse { + + public final statusCode:StatusCode; + public final headers:Map; + 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(); + } +} diff --git a/src/kernel/http/StatusCode.hx b/src/kernel/http/StatusCode.hx new file mode 100644 index 0000000..c41f015 --- /dev/null +++ b/src/kernel/http/StatusCode.hx @@ -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; +}