Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interceptor to change the URL #53

Open
jaumard opened this issue May 30, 2019 · 2 comments
Open

Interceptor to change the URL #53

jaumard opened this issue May 30, 2019 · 2 comments
Assignees

Comments

@jaumard
Copy link
Contributor

jaumard commented May 30, 2019

Hey :)

Just trying to do the following:

class HostInterceptor extends Interceptor {
  final Connectivity _connectivity;
  final PreferencesProvider _preferencesProvider;
  final LocalServerProvider _serverProvider;
  String _host;
  ConnectivityResult _previousConnectivity;
  String _previousWifiIp;

  HostInterceptor({
    Connectivity connectivity,
    LocalServerProvider serverProvider,
    PreferencesProvider preferencesProvider,
  })  : _connectivity = connectivity ?? Connectivity(),
        _serverProvider = serverProvider ?? LocalServerProvider(),
        _preferencesProvider = preferencesProvider ?? PreferencesProvider() {
    if(Platform.isAndroid || Platform.isIOS) {
      _connectivity.onConnectivityChanged.listen((connectivityResult) async {
        if (_previousConnectivity != connectivityResult) {
          _host = null; //reset host to trigger another search on next request
        } else if (connectivityResult == ConnectivityResult.wifi) {
          final ip = await connectivity.getWifiIP();
          if (_previousWifiIp != ip) {
            _host = null; //reset host to trigger another search on next request because we change wifi network
          }
        }

        _previousConnectivity = connectivityResult;
      });
    }
  }

  @override
  FutureOr<void> before(RouteBase route) async {
      final url = route.getUrl;
      final prefExternalUrl = _preferencesProvider.prefs.getString(PreferencesProvider.keyExternalUrl);
          _setExternalUrl(route, url, prefExternalUrl);
    return null;
  }

  _setExternalUrl(RouteBase route, String url, String externalUrl) {
    final urlParts = Uri.parse(url);
    final externalUrlParts = Uri.parse(externalUrl);
    final finalUrl = urlParts.replace(host: externalUrlParts.host, scheme: externalUrlParts.scheme, port: externalUrlParts.port);
    route.url(finalUrl.toString());
  }

  @override
  FutureOr after(StringResponse response) {
    return response;
  }
}

Problem is that it duplicate the path where it shouldn't.

Example:

url on the route is: http://localhost/api/v1/test
external url on pref is: http://myDomain.com
so finalUrl become: http://myDomain.com/api/v1/test

But the url fails because jaguar actually does the request to: http://myDomain.com/api/v1/test/api/v1/test

Didn't find out why :/

@tejainece
Copy link
Member

I will take a look.

@tejainece tejainece self-assigned this Jun 9, 2019
@droplet-js
Copy link

droplet-js commented Jun 17, 2019

It's best to be like okhttp Interceptor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants