Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload progress #127

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions haxe_libraries/tink_core.hxml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
-D tink_core=1.24.0
# @install: lix --silent download "haxelib:/tink_core#1.24.0" into tink_core/1.24.0/haxelib
-cp ${HAXE_LIBCACHE}/tink_core/1.24.0/haxelib/src
# @install: lix --silent download "gh://github.com/haxetink/tink_core#a14155a83f41279a9804d2733114f1404608d24f" into tink_core/1.27.1/github/a14155a83f41279a9804d2733114f1404608d24f
-cp ${HAXE_LIBCACHE}/tink_core/1.27.1/github/a14155a83f41279a9804d2733114f1404608d24f/src
-D tink_core=1.27.1
11 changes: 8 additions & 3 deletions src/tink/http/Client.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tink.http;

import haxe.extern.Rest;
import tink.core.Progress;
import tink.http.Request;
import tink.http.Response;
import tink.http.Fetch;
Expand Down Expand Up @@ -29,7 +30,11 @@ interface ClientObject {
* @param req - The HTTP request
* @return The HTTP response
*/
function request(req:OutgoingRequest):Promise<IncomingResponse>;
function request(req:OutgoingRequest, ?handlers:ClientRequestHandlers):Promise<IncomingResponse>;
}

typedef ClientRequestHandlers = {
?upload:ProgressValue->Void,
}

typedef Processors = {
Expand Down Expand Up @@ -60,11 +65,11 @@ private class CustomClient implements ClientObject {
else
value;

public function request(req)
public function request(req, ?handlers)
return
pipe(req, preprocessors)
.next(function (req)
return real.request(req)
return real.request(req, handlers)
.next(pipe.bind(_, postprocessors == null ? null : [for (p in postprocessors) p(req)]))
);

Expand Down
3 changes: 2 additions & 1 deletion src/tink/http/Fetch.hx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Fetch {
client.request(new OutgoingRequest(
new OutgoingRequestHeader(method, url, headers),
body
)).handle(function(res) {
), options.handlers).handle(function(res) {
switch res {
case Success(res):
switch res.header.statusCode {
Expand Down Expand Up @@ -101,6 +101,7 @@ typedef FetchOptions = {
?client:ClientType,
?followRedirect:Bool,
?augment:Processors,
?handlers:ClientRequestHandlers,
}

enum ClientType {
Expand Down
2 changes: 1 addition & 1 deletion src/tink/http/clients/CurlClient.hx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CurlClient implements ClientObject {
if(curl != null) this.curl = curl;
}

public function request(req:OutgoingRequest):Promise<IncomingResponse> {
public function request(req:OutgoingRequest, ?handlers:ClientRequestHandlers):Promise<IncomingResponse> {
return switch Helpers.checkScheme(req.header.url.scheme) {
case Some(e):
Promise.reject(e);
Expand Down
2 changes: 1 addition & 1 deletion src/tink/http/clients/FlashClient.hx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FlashClient implements ClientObject {

public function new() {}

public function request(req:OutgoingRequest):Promise<IncomingResponse> {
public function request(req:OutgoingRequest, ?handlers:ClientRequestHandlers):Promise<IncomingResponse> {
return Future.async(function(cb) {
switch Helpers.checkScheme(req.header.url.scheme) {
case Some(e):
Expand Down
2 changes: 1 addition & 1 deletion src/tink/http/clients/FlashSocketClient.hx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class FlashSocketClient implements ClientObject {
function getSocket(secure:Bool):Socket
return secure ? new SecureSocket() : new Socket();

public function request(req:OutgoingRequest):Promise<IncomingResponse> {
public function request(req:OutgoingRequest, ?handlers:ClientRequestHandlers):Promise<IncomingResponse> {
return Future.async(function(cb) {

switch req.header.byName('connection') {
Expand Down
8 changes: 7 additions & 1 deletion src/tink/http/clients/JsClient.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tink.http.clients;

import haxe.io.Bytes;
import tink.core.Progress;
import tink.http.Client;
import tink.http.Header;
import tink.http.Request;
Expand All @@ -22,7 +23,7 @@ class JsClient implements ClientObject {
if(credentials) this.credentials = true;
}

public function request(req:OutgoingRequest):Promise<IncomingResponse> {
public function request(req:OutgoingRequest, ?handlers:ClientRequestHandlers):Promise<IncomingResponse> {
return Future.async(function(cb) {
var http = getHttp();
// if(req.header.url.scheme == null) cb(Failure(Helper.missingSchemeError()));
Expand Down Expand Up @@ -56,6 +57,11 @@ class JsClient implements ClientObject {
);
}
}
switch handlers {
case null | {upload: null}: // skip
case {upload: upload}:
http.upload.onprogress = function(e) upload(new ProgressValue(e.loaded, Some(e.total)));
}
http.onerror = function(e) {
cb(Failure(Error.withData(502, 'XMLHttpRequest Error', {request: req, error: e})));
}
Expand Down
2 changes: 1 addition & 1 deletion src/tink/http/clients/LocalContainerClient.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class LocalContainerClient implements ClientObject {
this.container = container;
}

public function request(req:OutgoingRequest):Promise<IncomingResponse> {
public function request(req:OutgoingRequest, ?handlers:ClientRequestHandlers):Promise<IncomingResponse> {
return container.serve(new IncomingRequest(
'127.0.0.1',
new IncomingRequestHeader(req.header.method, req.header.url.pathWithQuery, 'HTTP/1.1', @:privateAccess req.header.fields),
Expand Down
2 changes: 1 addition & 1 deletion src/tink/http/clients/NodeClient.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class NodeClient implements ClientObject {

public function new() { }

public function request(req:OutgoingRequest):Promise<IncomingResponse> {
public function request(req:OutgoingRequest, ?handlers:ClientRequestHandlers):Promise<IncomingResponse> {
return switch Helpers.checkScheme(req.header.url.scheme) {
case Some(e):
Promise.reject(e);
Expand Down
2 changes: 1 addition & 1 deletion src/tink/http/clients/PhpClient.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using StringTools;
class PhpClient implements ClientObject {
public function new() {}

public function request(req:OutgoingRequest):Promise<IncomingResponse> {
public function request(req:OutgoingRequest, ?handlers:ClientRequestHandlers):Promise<IncomingResponse> {
return Future.async(function(cb) {
req.body.all().handle(function(chunk) {
var options = php.Lib.associativeArrayOfObject({
Expand Down
2 changes: 1 addition & 1 deletion src/tink/http/clients/SocketClient.hx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SocketClient implements ClientObject {
this.worker = worker.ensure();
}

public function request(req:OutgoingRequest):Promise<IncomingResponse> {
public function request(req:OutgoingRequest, ?handlers:ClientRequestHandlers):Promise<IncomingResponse> {
return Future.async(function(cb) {
switch Helpers.checkScheme(req.header.url.scheme) {
case Some(e):
Expand Down
2 changes: 1 addition & 1 deletion src/tink/http/clients/StdClient.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class StdClient implements ClientObject {
public function new(?worker:Worker) {
this.worker = worker.ensure();
}
public function request(req:OutgoingRequest):Promise<IncomingResponse>
public function request(req:OutgoingRequest, ?handlers:ClientRequestHandlers):Promise<IncomingResponse>
return Future.async(function (cb) {

var r = new haxe.Http(req.header.url);
Expand Down
2 changes: 1 addition & 1 deletion src/tink/http/clients/TcpClient.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using tink.CoreApi;
@:require('tink_tcp')
class TcpClient implements ClientObject {
public function new() {}
public function request(req:OutgoingRequest):Promise<IncomingResponse> {
public function request(req:OutgoingRequest, ?handlers:ClientRequestHandlers):Promise<IncomingResponse> {
return Future.async(function(cb) {
switch Helpers.checkScheme(req.header.url.scheme) {
case Some(e): cb(Failure(e));
Expand Down