Make HTTPRequest rely less on macros

This commit is contained in:
Joshua Granick
2016-11-09 13:34:20 -08:00
parent 5bb2816934
commit 97285c4881
5 changed files with 175 additions and 174 deletions

View File

@@ -21,7 +21,7 @@ import lime.net.HTTPRequest;
class FlashHTTPRequest { class FlashHTTPRequest {
private var parent:IHTTPRequest; private var parent:_IHTTPRequest;
private var urlLoader:URLLoader; private var urlLoader:URLLoader;
private var urlRequest:URLRequest; private var urlRequest:URLRequest;
@@ -111,7 +111,7 @@ class FlashHTTPRequest {
} }
public function init (parent:IHTTPRequest):Void { public function init (parent:_IHTTPRequest):Void {
this.parent = parent; this.parent = parent;

View File

@@ -15,7 +15,7 @@ class HTML5HTTPRequest {
private var binary:Bool; private var binary:Bool;
private var parent:IHTTPRequest; private var parent:_IHTTPRequest;
private var request:XMLHttpRequest; private var request:XMLHttpRequest;
@@ -37,7 +37,7 @@ class HTML5HTTPRequest {
} }
public function init (parent:IHTTPRequest):Void { public function init (parent:_IHTTPRequest):Void {
this.parent = parent; this.parent = parent;

View File

@@ -20,7 +20,7 @@ class NativeHTTPRequest {
private var bytesLoaded:Int; private var bytesLoaded:Int;
private var bytesTotal:Int; private var bytesTotal:Int;
private var curl:CURL; private var curl:CURL;
private var parent:IHTTPRequest; private var parent:_IHTTPRequest;
private var promise:Promise<Bytes>; private var promise:Promise<Bytes>;
@@ -44,7 +44,7 @@ class NativeHTTPRequest {
} }
public function init (parent:IHTTPRequest):Void { public function init (parent:_IHTTPRequest):Void {
this.parent = parent; this.parent = parent;

View File

@@ -1181,7 +1181,7 @@ class Image {
#else #else
throw "ImageBuffer.loadFromBytes not supported on this target"; throw "Image.fromBytes not supported on this target";
#end #end

View File

@@ -1,16 +1,27 @@
package lime.net; #if !macro package lime.net; #if !macro
#if (!macro && !display) import haxe.io.Bytes;
import lime.app.Event;
import lime.app.Future;
import lime.app.Promise;
#if display
class HTTPRequest<T> {
#else
@:genericBuild(lime.net.HTTPRequest.build()) @:genericBuild(lime.net.HTTPRequest.build())
#end class HTTPRequest<T> extends AbstractHTTPRequest<T> {}
private class AbstractHTTPRequest<T> implements _IHTTPRequest {
class HTTPRequest<T> implements IHTTPRequest {
#end
public var contentType:String; public var contentType:String;
public var data:haxe.io.Bytes; public var data:Bytes;
public var enableResponseHeaders:Bool; public var enableResponseHeaders:Bool;
public var followRedirects:Bool; public var followRedirects:Bool;
public var formData:Map<String, Dynamic>; public var formData:Map<String, Dynamic>;
@@ -23,13 +34,7 @@ class HTTPRequest<T> implements IHTTPRequest {
public var uri:String; public var uri:String;
public var userAgent:String; public var userAgent:String;
#if flash private var backend:HTTPRequestBackend;
private var backend = new lime._backend.flash.FlashHTTPRequest ();
#elseif (js && html5)
private var backend = new lime._backend.html5.HTML5HTTPRequest ();
#else
private var backend = new lime._backend.native.NativeHTTPRequest ();
#end
public function new (uri:String = null) { public function new (uri:String = null) {
@@ -43,6 +48,7 @@ class HTTPRequest<T> implements IHTTPRequest {
method = GET; method = GET;
timeout = 30000; timeout = 30000;
backend = new HTTPRequestBackend ();
backend.init (this); backend.init (this);
} }
@@ -55,7 +61,7 @@ class HTTPRequest<T> implements IHTTPRequest {
} }
public function load (uri:String = null):lime.app.Future<T> { public function load (uri:String = null):Future<T> {
return null; return null;
@@ -65,7 +71,95 @@ class HTTPRequest<T> implements IHTTPRequest {
} }
@:dox(hide) @:noCompletion interface IHTTPRequest { #if !display
class _HTTPRequest_Bytes<T> extends AbstractHTTPRequest<T> {
public function new (uri:String = null) {
super (uri);
}
private function fromBytes (bytes:Bytes):T {
return cast bytes;
}
public override function load (uri:String = null):Future<T> {
if (uri != null) {
this.uri = uri;
}
var promise = new lime.app.Promise<T> ();
var future = backend.loadData (this.uri);
future.onProgress (promise.progress);
future.onError (promise.error);
future.onComplete (function (bytes) {
responseData = fromBytes (bytes);
promise.complete (responseData);
});
return promise.future;
}
}
class _HTTPRequest_String<T> extends AbstractHTTPRequest<T> {
public function new (uri:String = null) {
super (uri);
}
public override function load (uri:String = null):Future<T> {
if (uri != null) {
this.uri = uri;
}
var promise = new lime.app.Promise<T> ();
var future = backend.loadText (this.uri);
future.onProgress (promise.progress);
future.onError (promise.error);
future.onComplete (function (text) {
responseData = cast text;
promise.complete (responseData);
});
return promise.future;
}
}
interface _IHTTPRequest {
public var contentType:String; public var contentType:String;
public var data:haxe.io.Bytes; public var data:haxe.io.Bytes;
@@ -86,6 +180,18 @@ class HTTPRequest<T> implements IHTTPRequest {
} }
#end
#if flash
private typedef HTTPRequestBackend = lime._backend.flash.FlashHTTPRequest;
#elseif (js && html5)
private typedef HTTPRequestBackend = lime._backend.html5.HTML5HTTPRequest;
#else
private typedef HTTPRequestBackend = lime._backend.native.NativeHTTPRequest;
#end
#else #else
@@ -171,161 +277,71 @@ class HTTPRequest {
} }
var typeParamString = typeString; if (typeString == "String" || stringAbstract) {
return TPath ( { pack: [ "lime", "net" ], name: "HTTPRequest", sub: "_HTTPRequest_String", params: [ TPType (paramType.toComplexType ()) ] } ).toType ();
} else if (typeString == "haxe.io.Bytes" || bytesAbstract) {
return TPath ( { pack: [ "lime", "net" ], name: "HTTPRequest", sub: "_HTTPRequest_Bytes", params: [ TPType (paramType.toComplexType ()) ] } ).toType ();
} else {
if (typeArgs.length > 0) { var typeParamString = typeString;
typeParamString += "<"; if (typeArgs.length > 0) {
for (i in 0...typeArgs.length) {
if (i > 0) typeParamString += ","; typeParamString += "<";
typeParamString += typeArgs[i].toString ();
} for (i in 0...typeArgs.length) {
typeParamString += ">";
}
var flattenedTypeString = typeParamString;
flattenedTypeString = StringTools.replace (flattenedTypeString, "->", "_");
flattenedTypeString = StringTools.replace (flattenedTypeString, ".", "_");
flattenedTypeString = StringTools.replace (flattenedTypeString, "<", "_");
flattenedTypeString = StringTools.replace (flattenedTypeString, ">", "_");
var name = "_HTTPRequest_" + flattenedTypeString;
try {
Context.getType ("lime.net." + name);
} catch (e:Dynamic) {
var pos = Context.currentPos ();
var fields = Context.getBuildFields ();
var load = null;
for (field in fields) {
if (field.name == "load") {
load = field; if (i > 0) typeParamString += ",";
break; typeParamString += typeArgs[i].toString ();
} }
typeParamString += ">";
} }
switch (load.kind) { var flattenedTypeString = typeParamString;
flattenedTypeString = StringTools.replace (flattenedTypeString, "->", "_");
flattenedTypeString = StringTools.replace (flattenedTypeString, ".", "_");
flattenedTypeString = StringTools.replace (flattenedTypeString, "<", "_");
flattenedTypeString = StringTools.replace (flattenedTypeString, ">", "_");
var name = "_HTTPRequest_" + flattenedTypeString;
try {
case FFun (fun): Context.getType ("lime.net." + name);
} catch (e:Dynamic) {
var pos = Context.currentPos ();
var fields = [
{ name: "new", access: [ APublic ], kind: FFun({ args: [ { name: "uri", type: macro :String, opt: true } ], expr: macro { super (uri); }, params: [], ret: macro :Void }), pos: Context.currentPos () },
{ name: "fromBytes", access: [ APrivate, AOverride ], kind: FFun ( { args: [ { name: "bytes", type: macro :haxe.io.Bytes } ], expr: Context.parse ("return " + typeString + ".fromBytes (bytes)", pos), params: [], ret: paramType.toComplexType () } ), pos: pos }
];
Context.defineType ({
if (typeString == "String") { name: name,
pack: [ "lime", "net" ],
fun.expr = macro { kind: TDClass ({ pack: [ "lime", "net" ], name: "HTTPRequest", sub: "_HTTPRequest_Bytes", params: [ TPType (paramType.toComplexType ()) ] }, null, false),
fields: fields,
if (uri != null) { pos: pos
this.uri = uri; });
}
return cast backend.loadText (this.uri);
};
} else if (stringAbstract) {
fun.expr = macro {
if (uri != null) {
this.uri = uri;
}
var promise = new lime.app.Promise<T> ();
var future = backend.loadText (this.uri);
future.onProgress (promise.progress);
future.onError (promise.error);
future.onComplete (function (text) {
responseData = cast text;
promise.complete (responseData);
});
return promise.future;
};
} else {
fun.expr = macro {
if (uri != null) {
this.uri = uri;
}
var promise = new lime.app.Promise<T> ();
var future = backend.loadData (this.uri);
future.onProgress (promise.progress);
future.onError (promise.error);
future.onComplete (function (bytes) {
responseData = cast fromBytes (bytes);
promise.complete (responseData);
});
return promise.future;
};
var fromBytes = null;
if (typeString == "haxe.io.Bytes" || bytesAbstract) {
fromBytes = macro { return bytes; }
} else {
fromBytes = Context.parse ("return " + typeString + ".fromBytes (bytes)", pos);
}
fields.push ( { name: "fromBytes", access: [ APrivate, AInline ], kind: FFun ( { args: [ { name: "bytes", type: macro :haxe.io.Bytes } ], expr: fromBytes, params: [], ret: paramType.toComplexType () } ), pos: pos } );
}
default:
} }
Context.defineType ({ return TPath ( { pack: [ "lime", "net" ], name: name, params: [] } ).toType ();
pos: pos,
pack: [ "lime", "net" ],
name: name,
kind: TDClass (null, [ { pack: [ "lime", "net" ], name: "HTTPRequest", sub: "IHTTPRequest", params: [] } ], null),
fields: fields,
params: [ { name: "T" } ],
meta: [ { name: ":dox", params: [ macro hide ], pos: pos }, { name: ":noCompletion", pos: pos } ]
});
} }
return TPath ( { pack: [ "lime", "net" ], name: name, params: [ TPType (paramType.toComplexType ()) ] } ).toType ();
} }
@@ -337,7 +353,7 @@ class HTTPRequest {
case TInst (t, _): case TInst (t, _):
return isBytesType (t.get ()); return t.get ().module == "haxe.io.Bytes";
case TAbstract (t, _): case TAbstract (t, _):
@@ -356,21 +372,6 @@ class HTTPRequest {
} }
private static function isBytesType (type:ClassType):Bool {
while (true) {
if (type.module == "haxe.io.Bytes") return true;
if (type.superClass == null) break;
type = type.superClass.t.get ();
}
return false;
}
private static function isStringAbstract (type:AbstractType):Bool { private static function isStringAbstract (type:AbstractType):Bool {
while (type != null) { while (type != null) {