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

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

There is a newer version: 7.9.0
Show newest version
{{>header}}
import 'dart:async';
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:built_value/serializer.dart';

{{#operations}}
{{#modelImports}}import 'package:{{pubName}}/model/{{.}}.dart';
{{/modelImports}}
{{#fullImports}}import '{{.}}';
{{/fullImports}}

class {{classname}} {

    final Dio _dio;

    final Serializers _serializers;

    const {{classname}}(this._dio, this._serializers);

    {{#operation}}
    /// {{{summary}}}
    ///
    /// {{{notes}}}
    Future> {{nickname}}({{^hasRequiredParams}}{ {{/hasRequiredParams}}{{#requiredParams}}
        {{{dataType}}} {{paramName}},{{#-last}} { {{/-last}}{{/requiredParams}}{{#optionalParams}}
        {{{dataType}}} {{paramName}},{{/optionalParams}}
        CancelToken cancelToken,
        Map headers,
        Map extra,
        ValidateStatus validateStatus,
        ProgressCallback onSendProgress,
        ProgressCallback onReceiveProgress,
    }) async {
        final String _path = '{{{path}}}'{{#pathParams}}.replaceAll('{' r'{{baseName}}' '}', {{{paramName}}}.toString()){{/pathParams}};

        final queryParams = {};
        final headerParams = { {{#httpUserAgent}}
            'User-Agent': '{{{.}}}',{{/httpUserAgent}}
            if (headers != null) ...headers,
        };
        dynamic bodyData;

        {{#headerParams}}
        headerParams[r'{{baseName}}'] = {{paramName}};
        {{/headerParams}}
        {{#queryParams}}
        queryParams[r'{{baseName}}'] = {{paramName}};
        {{/queryParams}}
        queryParams.removeWhere((key, dynamic value) => value == null);
        headerParams.removeWhere((key, dynamic value) => value == null);

        final contentTypes = [{{^hasConsumes}}];{{/hasConsumes}}{{#hasConsumes}}{{#consumes}}
            '{{{mediaType}}}',{{/consumes}}
        ];{{/hasConsumes}}
        {{#hasFormParams}}

        final formData = {
            {{#formParams}}
            {{^required}}{{^nullable}}if ({{paramName}} != null) {{/nullable}}{{/required}}r'{{baseName}}': {{#isFile}}MultipartFile.fromBytes({{paramName}}, filename: r'{{baseName}}'){{/isFile}}{{^isFile}}parameterToString(_serializers, {{paramName}}){{/isFile}},
            {{/formParams}}
        };
        {{#isMultipart}}
        bodyData = FormData.fromMap(formData);
        {{/isMultipart}}
        {{^isMultipart}}
        bodyData = formData;
        {{/isMultipart}}
        {{/hasFormParams}}
        {{#bodyParam}}

        {{#isContainer}}
        {{#isArray}}
        const type = FullType(Built{{#uniqueItems}}Built{{/uniqueItems}}{{^uniqueItems}}List{{/uniqueItems}}, [FullType({{baseType}})]);
        final serializedBody = _serializers.serialize({{paramName}}, specifiedType: type);
        {{/isArray}}
        {{#isMap}}
        const type = FullType(BuiltMap, [FullType(String), FullType({{baseType}})]);
        final serializedBody = _serializers.serialize({{paramName}}, specifiedType: type);
        {{/isMap}}
        {{/isContainer}}
        {{^isContainer}}
        {{#isPrimitiveType}}
        final serializedBody = {{paramName}};
        {{/isPrimitiveType}}
        {{^isPrimitiveType}}
        final bodySerializer = _serializers.serializerForType({{{baseType}}}) as Serializer<{{{baseType}}}>;
        final serializedBody = _serializers.serializeWith(bodySerializer, {{paramName}});
        {{/isPrimitiveType}}
        {{/isContainer}}
        final json{{paramName}} = json.encode(serializedBody);
        bodyData = json{{paramName}};
        {{/bodyParam}}

        return _dio.request(
            _path,
            queryParameters: queryParams,
            data: bodyData,
            options: Options(
                method: '{{httpMethod}}'.toUpperCase(),
                {{#isResponseFile}}
                responseType: ResponseType.bytes,
                {{/isResponseFile}}
                headers: headerParams,
                extra: {
                    'secure': >[{{^hasAuthMethods}}],{{/hasAuthMethods}}{{#hasAuthMethods}}
                        {{#authMethods}}{
                            'type': '{{type}}',
                            'name': '{{name}}',{{#isApiKey}}
                            'keyName': '{{keyParamName}}',
                            'where': '{{#isKeyInQuery}}query{{/isKeyInQuery}}{{#isKeyInHeader}}header{{/isKeyInHeader}}',{{/isApiKey}}
                        },{{/authMethods}}
                    ],{{/hasAuthMethods}}
                    if (extra != null) ...extra,
                },
                validateStatus: validateStatus,
                contentType: contentTypes.isNotEmpty ? contentTypes[0] : 'application/json',
            ),
            cancelToken: cancelToken,
            onSendProgress: onSendProgress,
            onReceiveProgress: onReceiveProgress,
        ){{#returnType}}.then((response) {
        {{#isResponseFile}}
            final data = response.data;
        {{/isResponseFile}}
        {{^isResponseFile}}
        {{#returnSimpleType}}
        {{#returnTypeIsPrimitive}}
            final data = response.data as {{{returnType}}};
        {{/returnTypeIsPrimitive}}
        {{^returnTypeIsPrimitive}}
            final serializer = _serializers.serializerForType({{{returnType}}}) as Serializer<{{{returnType}}}>;
            final data = _serializers.deserializeWith<{{{returnType}}}>(
                serializer,
                response.data is String ? jsonDecode(response.data as String) : response.data,
            );
        {{/returnTypeIsPrimitive}}
        {{/returnSimpleType}}
        {{^returnSimpleType}}
            const type = FullType(Built{{#isMap}}Map{{/isMap}}{{#isArray}}{{#uniqueItems}}Set{{/uniqueItems}}{{^uniqueItems}}List{{/uniqueItems}}{{/isArray}}, [{{#isMap}}FullType(String), {{/isMap}}FullType({{{returnBaseType}}})]);
            final data = _serializers.deserialize(
                response.data is String
                ? jsonDecode(response.data as String)
                : response.data,
                specifiedType: type,
            ) as {{{returnType}}};
        {{/returnSimpleType}}
        {{/isResponseFile}}

            return Response<{{{returnType}}}>(
                data: data,
                headers: response.headers,
                isRedirect: response.isRedirect,
                request: response.request,
                redirects: response.redirects,
                statusCode: response.statusCode,
                statusMessage: response.statusMessage,
                extra: response.extra,
            );
        }){{/returnType}};
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy