Skip to content

Interceptors

Ravi Teja Gudapati edited this page Aug 29, 2018 · 2 revisions

Interceptors are functions that can be run before and after a request is made by Jaguar Resty, Client and Retrofit. They are defined as following:

typedef FutureOr<void> Before(RouteBase route);

typedef FutureOr<void> After<T>(Response<T> response);

During its execution, Before interceptor can change the request (For example: add headers, query, path, body, etc). Similarly, After can act upon the Response object.

Example

Before

Lets say we want to set a and b query parameters in the request.

resty.Before setQuery(int a, int b) => (resty.RouteBase route) {
      route.query("a", a);
      route.query("b", b);
    };

After

Lets say we want to print the result in the interceptor after the request is made:

void printResult(resty.Response<String> resp) {
  print(resp.body);
}

Complete example

Future client() async {
  await resty
      .get('/math/addition')
      .http('localhost:8000')
      .interceptBefore(setQuery(5, 20))
      .interceptAfter(printResult)
      .go();
}

For complete example, please take a look at https://github.com/Jaguar-dart/client/blob/master/resty/example/interceptor.dart.

Clone this wiki locally