part of {{pubName}}.api;

{{#operations}}


class {{classname}} {
  final ApiClient apiClient;

  {{classname}}([ApiClient apiClient]) : apiClient = apiClient ?? defaultApiClient;

  {{#operation}}
  /// {{summary}}
  ///
  /// {{notes}}
  {{#returnType}}Future<{{{returnType}}}> {{/returnType}}{{^returnType}}Future {{/returnType}}{{nickname}}({{#allParams}}{{#required}}{{{dataType}}} {{paramName}}, {{/required}}{{/allParams}} { {{#allParams}}{{^required}} {{{dataType}}} {{paramName}}, {{/required}}{{/allParams}} bool justIgnoreThisFlag: true}) async {
    if (!justIgnoreThisFlag) {
      print('Why???   Just trust me, I only need this variable inside the mustache codegen template.');
      // This code may be removed as soon as dart accepts trailing spaces (has already been implemented).
    }
    Object postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};

    // verify required params are set
    {{#allParams}}
      {{#required}}
    if({{paramName}} == null) {
     throw new ApiException(400, "Missing required param: {{paramName}}");
    }
      {{/required}}
    {{/allParams}}

    // create path and map variables
    String path = "{{path}}".replaceAll("{format}","json"){{#pathParams}}.replaceAll("{" + "{{paramName}}" + "}", {{{paramName}}}.toString()){{/pathParams}};

    // query params
    List<QueryParam> queryParams = [];
    Map<String, String> headerParams = {};
    Map<String, String> formParams = {};
    {{#queryParams}}
    if("null" != {{paramName}}) {
      queryParams.addAll(_convertParametersForCollectionFormat("{{collectionFormat}}", "{{baseName}}", {{paramName}}));
    }
    {{/queryParams}}
    {{#headerParams}}headerParams["{{baseName}}"] = {{paramName}};
    {{/headerParams}}

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

    String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
    List<String> authNames = [{{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}}];

    if(contentType.startsWith("multipart/form-data")) {
      bool hasFields = false;
      MultipartRequest mp = new MultipartRequest(null, null);
      {{#formParams}}{{#notFile}}
      if ({{paramName}} != null) {
        hasFields = true;
        mp.fields['{{baseName}}'] = apiClient.parameterToString({{paramName}});
      }
      {{/notFile}}{{#isFile}}
      if ({{paramName}} != null) {
        hasFields = true;
        mp.fields['{{baseName}}'] = {{paramName}}.field;
        mp.files.add({{paramName}});
      }
      {{/isFile}}{{/formParams}}
      if(hasFields)
        postBody = mp;
    }
    else {
      {{#formParams}}{{#notFile}}if ({{paramName}} != null)
        formParams['{{baseName}}'] = apiClient.parameterToString({{paramName}});{{/notFile}}
      {{/formParams}}
    }

    var response = await apiClient.invokeAPI(path,
                                             '{{httpMethod}}',
                                             queryParams,
                                             postBody,
                                             headerParams,
                                             formParams,
                                             contentType,
                                             authNames);

    if(response.statusCode >= 400) {
      throw new ApiException(response.statusCode, response.body);
    } else if(response.body != null) {
      return {{#returnType}} apiClient.deserialize(response.body, '{{{returnType}}}') {{/returnType}};
    } else {
      return {{#returnType}}null{{/returnType}};
    }
  }
  {{/operation}}
}
{{/operations}}
