sinew_network

pub package License Docs

SinewHttp (Dio): the auth layer, processApiCall, retry, polling and NetworkChecker.

An HTTP client that handles what every app rewrites: attaching the token only to your backend, refreshing it once for any number of concurrent 401s, mapping responses to typed failures, and retrying and polling with backoff. Use the preset, or add only the interceptors you need to your own Dio.

Features

  • SinewHttp.dio: a Dio with timeouts, headers, auth, error bodies and logging from one SinewConfig
  • SinewAuthInterceptor: the token goes only to baseUrl's host, outside the anonymous paths, with a single-flight refresh through TokenSource and SessionTokenSource
  • processApiCall and processOptionalApiCall: map an Envelope to a Result and every status to a typed SinewException
  • retryWithBackoff and poll, NetworkChecker, pinCertificateAuthorities, and SinewHttpHooks for the devtools

Getting started

flutter pub add sinew_network

Usage

final config = SinewConfig(
  baseUrl: 'https://api.example.com/v1',
  locale: () => 'en',
  anonymousPaths: {'/auth/login'},
);
final dio = SinewHttp.dio(config, tokenSource);

// In a repository: any failure becomes a typed SinewException.
Future<Result<Order>> cancel(String id) =>
    processApiCall('ORD', 'CANCEL', () => api.cancel(id));

API classes use paths relative to baseUrl (get('orders')), since a leading / would replace its own path. On sign-out, call SinewHttp.resetAuth(dio) and tokenSource.clear().

Sinew is one architecture for Flutter and Kotlin, released as a set of packages at one version.

Documentation

License

Apache 2.0, see LICENSE.

Libraries

sinew_network
SinewHttp (Dio), the auth layer, processApiCall, retry, polling and NetworkChecker.