Skip to content

Commit

Permalink
Added onRequestPreparedAsync on hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
b099l3 committed Sep 13, 2022
1 parent ab51514 commit 1dc271a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions protoc-gen-tart/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ Future<List<int>> doProtobufRequest(twirp.Context ctx, Uri url,
req.bodyBytes = msgReq.writeToBuffer();
// call onRequestPrepared hook for user to modify request
ctx = hooks.onRequestPrepared(ctx, req);
ctx = await hooks.onRequestPrepared(ctx, req);
// send data
final res = await httpClient.send(req);
Expand Down Expand Up @@ -282,7 +282,7 @@ Future<String> doJSONRequest(twirp.Context ctx, Uri url,
req.body = json.encode(msgReq.toProto3Json());
// call onRequestPrepared hook for user to modify request
ctx = hooks.onRequestPrepared(ctx, req);
ctx = await hooks.onRequestPrepared(ctx, req);
// send data
final res = await httpClient.send(req);
Expand Down
7 changes: 5 additions & 2 deletions tart/lib/src/hooks.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import 'dart:async';

import 'package:http/http.dart' as http;

import 'context.dart';
import 'error.dart';

/// ClientHooks contains functions that can be used on the generated client constructor.
/// They provide callbacks for before and after the request is sent over the network.
class ClientHooks {
/// onRequestPrepared is after request has been prepared and immediately before request is sent
Context Function(Context ctx, http.Request req) onRequestPrepared;
Future<Context> Function(Context ctx, http.Request req) onRequestPrepared;

/// onResponseReceived is only called when response has been received without error
Function(Context ctx) onResponseReceived;
Expand All @@ -20,7 +23,7 @@ class ClientHooks {
this.onError = defaultOnError,
});

static Context defaultOnRequestPrepared(ctx, req) => ctx;
static Future<Context> defaultOnRequestPrepared(ctx, req) => ctx;

static void defaultOnResponseReceived(ctx) {/* do nothing */}

Expand Down

0 comments on commit 1dc271a

Please sign in to comment.