Coder Social home page Coder Social logo

How to configure with Dio? about brick HOT 2 CLOSED

getdutchie avatar getdutchie commented on July 17, 2024
How to configure with Dio?

from brick.

Comments (2)

tshedor avatar tshedor commented on July 17, 2024 1

@MilesAdamson so this is an interesting problem and it led me down a rabbit hole of dependencies. I'll summarize as best I can before getting to the solution:

  1. Dio uses HttpClient which is from dart:io:HttpClient. Brick uses HttpClient from package:http/http.dart
  2. Really, all you need is a BaseClient to pass Dart's type checker. So I checked out client.dart which has a useful note about the IOClient. I wasn't aware that there were two different http client implementations or that the http package didn't make its own clients, so uncharted territory here.
  3. According to the docs, composition should be used to take advantage of multiple http clients (commonly annotated as the "inner" client). The Offline Queue implements this pattern to intercept and store requests if you'd like to see a fleshed out example. IOClient in the http package allows a composed inner client.

So, the solution, all said and done, is this (I think; untested code):

import 'package:http/io_client.dart';

(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
  (client) {
    final wrappedHttpClient = IOClient(client);
    Cache.configure(dio.options.baseUrl, wrappedHttpClient, "database.db");
  };

If this was useful, please comment back or submit a PR/Wiki documenting how you integrated Dio with Brick. I'm unfamiliar with dio but it seems to be a popular package and no doubt there's a large user base that would benefit from a brief tutorial.

from brick.

MilesAdamson avatar MilesAdamson commented on July 17, 2024

I tried it out yesterday and it does compile, but it doesn't use the Dio level features I was hoping for. For example dio has connect timeouts and the headers already configured in the app but they aren't getting added and the on request hook isn't getting fired.

Thanks for looking into it! But I'm not sure if it's there much point in getting Dio's client referenced in you package. For my purposes it seems easiest to just configure the headers again with brick's plugin and let brick handle the api client.

Examples of the Dio config which doesn't seem to effect requests made inside brick:

    dio.options.baseUrl = 'https://${this.subDomain}.website.ca/api';
    dio.options.connectTimeout = 10000;
    dio.options.receiveTimeout = 30000;
    dio.interceptors
        .add(InterceptorsWrapper(onRequest: (RequestOptions options) async {
      // Do something before request is sent
      options.headers["Authorization"] = "Bearer $token";

from brick.

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.