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.8.0
Show newest version
import 'dart:async';
import 'dart:io';
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:built_collection/built_collection.dart';
import 'package:built_value/serializer.dart';

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

class {{classname}} {
    final Dio _dio;
    Serializers _serializers;

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

    {{#operation}}
        /// {{summary}}
        ///
        /// {{notes}}
        Future{{/returnType}}>{{nickname}}({{#allParams}}{{#required}}{{{dataType}}} {{paramName}},{{/required}}{{/allParams}}{ {{#allParams}}{{^required}}{{{dataType}}} {{paramName}},{{/required}}{{/allParams}}CancelToken cancelToken, Map headers,}) async {

        String _path = "{{{path}}}"{{#pathParams}}.replaceAll("{" r'{{baseName}}' "}", {{{paramName}}}.toString()){{/pathParams}};

        Map queryParams = {};
        Map headerParams = Map.from(headers ?? {});
        dynamic bodyData;

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

        List contentTypes = [{{#consumes}}"{{{mediaType}}}"{{#hasMore}},{{/hasMore}}{{/consumes}}];

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

        {{#bodyParam}}
            {{#isListContainer}}
            final type = const FullType(BuiltList, const [const FullType({{baseType}})]);
            var serializedBody = _serializers.serialize(BuiltList<{{baseType}}>.from({{paramName}}), specifiedType: type);
            {{/isListContainer}}
            {{^isListContainer}}
            var serializedBody = _serializers.serialize({{paramName}});
            {{/isListContainer}}
            var json{{paramName}} = json.encode(serializedBody);
            bodyData = json{{paramName}};
        {{/bodyParam}}

            return _dio.request(
            _path,
            queryParameters: queryParams,
            data: bodyData,
            options: Options(
            method: '{{httpMethod}}'.toUpperCase(),
            headers: headerParams,
            contentType: contentTypes.isNotEmpty ? contentTypes[0] : "application/json",
            ),
            cancelToken: cancelToken,
            ){{#returnType}}.then((response) {

        {{#isListContainer}}
                final FullType type = const FullType(BuiltList, const [const FullType({{returnBaseType}})]);
                BuiltList<{{returnBaseType}}> dataList = _serializers.deserialize(response.data, specifiedType: type);
                var data = dataList.toList();
        {{/isListContainer}}
        {{^isListContainer}}
        var serializer = _serializers.serializerForType({{{returnType}}});
        var data = _serializers.deserializeWith<{{{returnType}}}>(serializer, response.data);
        {{/isListContainer}}

            return Response<{{{returnType}}}>(
                data: data,
                headers: response.headers,
                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