Coder Social home page Coder Social logo

Comments (4)

issue-label-bot avatar issue-label-bot commented on June 15, 2024 2

Issue-Label Bot is automatically applying the label question to this issue, with a confidence of 0.76. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

from http_interceptor.

CodingAleCR avatar CodingAleCR commented on June 15, 2024

Hi, there are a couple of things wrong in terms of usage, let's see them:

  1. Currently the plugin does not support the 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).
  2. I imagine that the reason you are doing this "wrapper" is to avoid creating multiple instances of the same client+interceptor combination. I would change a couple of things, I would avoid using the requests method as an enum or string if you are using this particular plugin, this because I think it's more sustainable to use the methods exposed by the plugin to make requests. Anyway, here's the way I would implement it, hope it helps:
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.

from http_interceptor.

smedic avatar smedic commented on June 15, 2024

Thanks for suggestions. I did not know that send is not supported. I really appreciate your work on this. +1 :)

from http_interceptor.

CodingAleCR avatar CodingAleCR commented on June 15, 2024

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!

from http_interceptor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.