Closed smedic closed 4 years ago
Issue-Label Bot is automatically applying the label question
to this issue, with a confidence of 0.76. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!
Links: app homepage, dashboard and code for this bot.
Hi, there are a couple of things wrong in terms of usage, let's see them:
send
method, it is only available due to compatibility issues at the moment (but I hope to fix this in a version soon, along with multipart request support).class MyApiClientWrapper {
final http.Client client = HttpClientWithInterceptor.build(interceptors: [
ApiInterceptor(),
]);
String _parseBody(Object body) {
var newBody = null;
if (body != null) {
if (body is String) {
newBody = body;
} else if (body is List || body is Map<String, dynamic>) {
newBody = jsonEncode(body);
} else {
throw ArgumentError('Invalid request body "$body".');
}
}
return newBody;
}
Future<Response> head(url, {Map<String, String> headers}) =>
client.head(_constructUri(path), headers: _generateHeaders(headers));
Future<Response> get(url,
{Map<String, String> headers, Map<String, String> params}) =>
client.get(
_constructUri(path),
headers: _generateHeaders(headers),
params: params,
);
Future<Response> post(url,
{Map<String, String> headers, body, Encoding encoding}) =>
client.post(
_constructUri(path),
headers: _generateHeaders(headers),
body: _parseBody(body),
encoding: encoding,
);
Future<Response> put(url,
{Map<String, String> headers, body, Encoding encoding}) =>
client.put(
_constructUri(path),
headers: _generateHeaders(headers),
body: _parseBody(body),
encoding: encoding,
);
Future<Response> patch(url,
{Map<String, String> headers, body, Encoding encoding}) =>
client.patch(
_constructUri(path),
headers: _generateHeaders(headers),
body: _parseBody(body),
encoding: encoding,
);
Future<Response> delete(url, {Map<String, String> headers}) =>
client.delete(
_constructUri(path),
headers: _generateHeaders(headers),
);
}
Interceptor:
class ApiInterceptor implements InterceptorContract {
@override
Future<RequestData> interceptRequest({RequestData data}) async {
print("Intercept call");
final token = await getToken();
try {
data.params[HttpHeaders.authorizationHeader] = 'Bearer ${token}';
} catch (e) {
print(e);
}
return data;
}
@override
Future<ResponseData> interceptResponse({ResponseData data}) async => data;
}
Now, if what you needed was a Stream response, as of right now we don't support that.
Thanks for suggestions. I did not know that send is not supported. I really appreciate your work on this. +1 :)
No problem! I am really happy to help, I will probably mark the method somehow to avoid confusions like this because I can definitely understand how this feels like a bug (and since it's probably not well documented it could be referred as one). I hope it helped you out at least a bit. Cheers!
I am trying to put auth token inside of a call, but my request interceptor is never called:
This is how interceptor looks like:
Any idea why? I need to be attached to client so every call passes through it. Thanks