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

dart-dio.apilib.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{>header}}
library {{pubName}}.api;

import 'package:dio/dio.dart';
import 'package:built_value/serializer.dart';
import 'package:{{pubName}}/serializers.dart';
import 'package:{{pubName}}/auth/api_key_auth.dart';
import 'package:{{pubName}}/auth/basic_auth.dart';
import 'package:{{pubName}}/auth/oauth.dart';
{{#apiInfo}}{{#apis}}import 'package:{{pubName}}/api/{{classFilename}}.dart';
{{/apis}}{{/apiInfo}}

final _defaultInterceptors = [
  OAuthInterceptor(),
  BasicAuthInterceptor(),
  ApiKeyAuthInterceptor(),
];

class {{clientName}} {

    static const String basePath = r'{{{basePath}}}';

    final Dio dio;

    final Serializers serializers;

    {{clientName}}({
      Dio dio,
      Serializers serializers,
      String basePathOverride,
      List interceptors,
    })  : this.serializers = serializers ?? standardSerializers,
          this.dio = dio ??
              Dio(BaseOptions(
                baseUrl: basePathOverride ?? basePath,
                connectTimeout: 5000,
                receiveTimeout: 3000,
              )) {
      if (interceptors == null) {
        this.dio.interceptors.addAll(_defaultInterceptors);
      } else {
        this.dio.interceptors.addAll(interceptors);
      }
    }

    void setOAuthToken(String name, String token) {
        (this.dio.interceptors.firstWhere((element) => element is OAuthInterceptor, orElse: null) as OAuthInterceptor)?.tokens[name] = token;
    }

    void setBasicAuth(String name, String username, String password) {
        (this.dio.interceptors.firstWhere((element) => element is BasicAuthInterceptor, orElse: null) as BasicAuthInterceptor)?.authInfo[name] = BasicAuthInfo(username, password);
    }

    void setApiKey(String name, String apiKey) {
        (this.dio.interceptors.firstWhere((element) => element is ApiKeyAuthInterceptor, orElse: null) 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, serializers);
    }

{{/apis}}{{/apiInfo}}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy