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

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

There is a newer version: 7.9.0
Show newest version
{{>header}}
import 'dart:async';

{{#includeLibraryTemplate}}api/imports{{/includeLibraryTemplate}}
import 'package:dio/dio.dart';

{{#operations}}
{{#imports}}import '{{.}}';
{{/imports}}

class {{classname}} {

  final Dio _dio;

{{#includeLibraryTemplate}}api/constructor{{/includeLibraryTemplate}}

  {{#operation}}
  /// {{summary}}{{^summary}}{{nickname}}{{/summary}}
  /// {{notes}}
  ///
  /// Parameters:
  {{#allParams}}
  /// * [{{paramName}}] {{#description}}- {{{.}}}{{/description}}
  {{/allParams}}
  /// * [cancelToken] - A [CancelToken] that can be used to cancel the operation
  /// * [headers] - Can be used to add additional headers to the request
  /// * [extras] - Can be used to add flags to the request
  /// * [validateStatus] - A [ValidateStatus] callback that can be used to determine request success based on the HTTP status of the response
  /// * [onSendProgress] - A [ProgressCallback] that can be used to get the send progress
  /// * [onReceiveProgress] - A [ProgressCallback] that can be used to get the receive progress
  ///
  /// Returns a [Future]{{#returnType}} containing a [Response] with a [{{{.}}}] as data{{/returnType}}
  /// Throws [DioException] if API call or serialization fails
  {{#externalDocs}}
  /// {{description}}
  /// Also see [{{summary}} Documentation]({{url}})
  {{/externalDocs}}
  {{#isDeprecated}}
  @Deprecated('This operation has been deprecated')
  {{/isDeprecated}}
  Future> {{nickname}}({ {{#allParams}}{{#isPathParam}}
    {{#isDeprecated}}@Deprecated('{{paramName}} is deprecated') {{/isDeprecated}}required {{{dataType}}} {{paramName}},{{/isPathParam}}{{#isQueryParam}}
    {{#isDeprecated}}@Deprecated('{{paramName}} is deprecated') {{/isDeprecated}}{{#required}}{{^isNullable}}{{^defaultValue}}required {{/defaultValue}}{{/isNullable}}{{/required}}{{{dataType}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}} {{paramName}}{{^isContainer}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}{{/isContainer}},{{/isQueryParam}}{{#isHeaderParam}}
    {{#isDeprecated}}@Deprecated('{{paramName}} is deprecated') {{/isDeprecated}}{{#required}}{{^isNullable}}{{^defaultValue}}required {{/defaultValue}}{{/isNullable}}{{/required}}{{{dataType}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}} {{paramName}}{{^isContainer}}{{#defaultValue}} = {{{.}}}{{/defaultValue}}{{/isContainer}},{{/isHeaderParam}}{{#isBodyParam}}
    {{#isDeprecated}}@Deprecated('{{paramName}} is deprecated') {{/isDeprecated}}{{#required}}{{^isNullable}}required {{/isNullable}}{{/required}}{{{dataType}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}} {{paramName}},{{/isBodyParam}}{{#isFormParam}}
    {{#isDeprecated}}@Deprecated('{{paramName}} is deprecated') {{/isDeprecated}}{{#required}}{{^isNullable}}required {{/isNullable}}{{/required}}{{{dataType}}}{{#required}}{{#isNullable}}?{{/isNullable}}{{/required}}{{^required}}?{{/required}} {{paramName}},{{/isFormParam}}{{/allParams}}
    CancelToken? cancelToken,
    Map? headers,
    Map? extra,
    ValidateStatus? validateStatus,
    ProgressCallback? onSendProgress,
    ProgressCallback? onReceiveProgress,
  }) async {
    final _path = r'{{{path}}}'{{#pathParams}}.replaceAll('{' r'{{{baseName}}}' '}', {{#includeLibraryTemplate}}api/query_param{{/includeLibraryTemplate}}.toString()){{/pathParams}};
    final _options = Options(
      method: r'{{#lambda.uppercase}}{{httpMethod}}{{/lambda.uppercase}}',
      {{#isResponseFile}}
      responseType: ResponseType.bytes,
      {{/isResponseFile}}
      headers: {
        {{#httpUserAgent}}
        r'User-Agent': r'{{{.}}}',
        {{/httpUserAgent}}
        {{#headerParams}}
        {{^required}}{{^isNullable}}if ({{{paramName}}} != null) {{/isNullable}}{{/required}}r'{{baseName}}': {{paramName}},
        {{/headerParams}}
        ...?headers,
      },
      extra: {
        'secure': >[{{^hasAuthMethods}}],{{/hasAuthMethods}}{{#hasAuthMethods}}
          {{#authMethods}}{
            'type': '{{type}}',{{#scheme}}
            'scheme': '{{.}}',{{/scheme}}
            'name': '{{name}}',{{#isApiKey}}
            'keyName': '{{keyParamName}}',
            'where': '{{#isKeyInQuery}}query{{/isKeyInQuery}}{{#isKeyInHeader}}header{{/isKeyInHeader}}',{{/isApiKey}}
          },{{/authMethods}}
        ],{{/hasAuthMethods}}
        ...?extra,
      },{{#hasConsumes}}
      contentType: '{{#prioritizedContentTypes}}{{#-first}}{{{mediaType}}}{{/-first}}{{/prioritizedContentTypes}}',{{/hasConsumes}}
      validateStatus: validateStatus,
    );{{#hasQueryParams}}

    final _queryParameters = {
      {{#queryParams}}
      {{^required}}{{^isNullable}}if ({{{paramName}}} != null) {{/isNullable}}{{/required}}r'{{baseName}}': {{#includeLibraryTemplate}}api/query_param{{/includeLibraryTemplate}},
      {{/queryParams}}
    };{{/hasQueryParams}}{{#hasBodyOrFormParams}}

    dynamic _bodyData;

    try {
{{#includeLibraryTemplate}}api/serialize{{/includeLibraryTemplate}}
    } catch(error, stackTrace) {
      throw DioException(
         requestOptions: _options.compose(
          _dio.options,
          _path,{{#hasQueryParams}}
          queryParameters: _queryParameters,{{/hasQueryParams}}
        ),
        type: DioExceptionType.unknown,
        error: error,
        stackTrace: stackTrace,
      );
    }{{/hasBodyOrFormParams}}

    final _response = await _dio.request(
      _path,{{#hasBodyOrFormParams}}
      data: _bodyData,{{/hasBodyOrFormParams}}
      options: _options,{{#hasQueryParams}}
      queryParameters: _queryParameters,{{/hasQueryParams}}
      cancelToken: cancelToken,
      onSendProgress: onSendProgress,
      onReceiveProgress: onReceiveProgress,
    );
    {{#returnType}}

    {{{.}}}? _responseData;

    try {
{{#includeLibraryTemplate}}api/deserialize{{/includeLibraryTemplate}}
    } catch (error, stackTrace) {
      throw DioException(
        requestOptions: _response.requestOptions,
        response: _response,
        type: DioExceptionType.unknown,
        error: error,
        stackTrace: stackTrace,
      );
    }

    return Response<{{{returnType}}}>(
      data: _responseData,
      headers: _response.headers,
      isRedirect: _response.isRedirect,
      requestOptions: _response.requestOptions,
      redirects: _response.redirects,
      statusCode: _response.statusCode,
      statusMessage: _response.statusMessage,
      extra: _response.extra,
    );{{/returnType}}{{^returnType}}
    return _response;{{/returnType}}
  }

  {{/operation}}
}
{{/operations}}