All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dart.libraries.dio.api_client.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{>header}}
import 'package:dio/dio.dart';{{#useBuiltValue}}
import 'package:built_value/serializer.dart';
import 'package:{{pubName}}/{{sourceFolder}}/serializers.dart';{{/useBuiltValue}}
import 'package:{{pubName}}/{{sourceFolder}}/auth/api_key_auth.dart';
import 'package:{{pubName}}/{{sourceFolder}}/auth/basic_auth.dart';
import 'package:{{pubName}}/{{sourceFolder}}/auth/bearer_auth.dart';
import 'package:{{pubName}}/{{sourceFolder}}/auth/oauth.dart';
{{#apiInfo}}{{#apis}}import 'package:{{pubName}}/{{sourceFolder}}/{{apiPackage}}/{{classFilename}}.dart';
{{/apis}}{{/apiInfo}}
class {{clientName}} {
  static const String basePath = r'{{{basePath}}}';

  final Dio dio;
{{#useBuiltValue}}
  final Serializers serializers;

{{/useBuiltValue}}
  {{clientName}}({
    Dio? dio,{{#useBuiltValue}}
    Serializers? serializers,{{/useBuiltValue}}
    String? basePathOverride,
    List? interceptors,
  })  : {{#useBuiltValue}}this.serializers = serializers ?? standardSerializers,{{/useBuiltValue}}
        this.dio = dio ??
            Dio(BaseOptions(
              baseUrl: basePathOverride ?? basePath,
              connectTimeout: const Duration(milliseconds: 5000),
              receiveTimeout: const Duration(milliseconds: 3000),
            )) {
    if (interceptors == null) {
      this.dio.interceptors.addAll([
        OAuthInterceptor(),
        BasicAuthInterceptor(),
        BearerAuthInterceptor(),
        ApiKeyAuthInterceptor(),
      ]);
    } else {
      this.dio.interceptors.addAll(interceptors);
    }
  }

  void setOAuthToken(String name, String token) {
    if (this.dio.interceptors.any((i) => i is OAuthInterceptor)) {
      (this.dio.interceptors.firstWhere((i) => i is OAuthInterceptor) as OAuthInterceptor).tokens[name] = token;
    }
  }

  void setBearerAuth(String name, String token) {
    if (this.dio.interceptors.any((i) => i is BearerAuthInterceptor)) {
      (this.dio.interceptors.firstWhere((i) => i is BearerAuthInterceptor) as BearerAuthInterceptor).tokens[name] = token;
    }
  }

  void setBasicAuth(String name, String username, String password) {
    if (this.dio.interceptors.any((i) => i is BasicAuthInterceptor)) {
      (this.dio.interceptors.firstWhere((i) => i is BasicAuthInterceptor) as BasicAuthInterceptor).authInfo[name] = BasicAuthInfo(username, password);
    }
  }

  void setApiKey(String name, String apiKey) {
    if (this.dio.interceptors.any((i) => i is ApiKeyAuthInterceptor)) {
      (this.dio.interceptors.firstWhere((element) => element is ApiKeyAuthInterceptor) as ApiKeyAuthInterceptor).apiKeys[name] = apiKey;
    }
  }{{#apiInfo}}{{#apis}}

  /// Get {{classname}} instance, base route and serializer can be overridden by a given but be careful,
  /// by doing that all interceptors will not be executed
  {{classname}} get{{classname}}() {
    return {{classname}}(dio{{#useBuiltValue}}, serializers{{/useBuiltValue}});
  }{{/apis}}{{/apiInfo}}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy