{{>licenseInfo}}
import { Inject, Injectable, Optional }                      from '@angular/core';
import { Http, Headers, URLSearchParams }                    from '@angular/http';
import { RequestMethod, RequestOptions, RequestOptionsArgs } from '@angular/http';
import { Response, ResponseContentType }                     from '@angular/http';

import { Observable }                                        from 'rxjs/Observable';
import 'rxjs/add/operator/map';

import * as models                                           from '../model/models';
import { BASE_PATH, COLLECTION_FORMATS }                     from '../variables';
import { Configuration }                                     from '../configuration';

/* tslint:disable:no-unused-variable member-ordering */

{{#operations}}

{{#description}}
/**
 * {{&description}}
 */
{{/description}}
@Injectable()
export class {{classname}} {
    protected basePath = '{{{basePath}}}';
    public defaultHeaders: Headers = new Headers();
    public configuration: Configuration = new Configuration();

    constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
        if (basePath) {
            this.basePath = basePath;
        }
        if (configuration) {
            this.configuration = configuration;
        }
    }

{{#operation}}
    /**
     * {{summary}}
     * {{notes}}
     {{#allParams}}* @param {{paramName}} {{description}}
     {{/allParams}}*/
    public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: any): Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}> {
        return this.{{nickname}}WithHttpInfo({{#allParams}}{{paramName}}, {{/allParams}}extraHttpRequestParams)
            .map((response: Response) => {
                if (response.status === 204) {
                    return undefined;
                } else {
                    return response.json();
                }
            });
    }

{{/operation}}

{{#operation}}
    /**
     * {{summary}}
     * {{notes}}
     {{#allParams}}* @param {{paramName}} {{description}}
     {{/allParams}}*/
    public {{nickname}}WithHttpInfo({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: any): Observable<Response> {
        const path = this.basePath + `{{path}}`;

        let queryParameters = new URLSearchParams();
        let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
{{#hasFormParams}}
        let formParams = new URLSearchParams();

{{/hasFormParams}}
{{#allParams}}
{{#required}}
        // verify required parameter '{{paramName}}' is not null or undefined
        if ({{paramName}} === null || {{paramName}} === undefined) {
            throw new Error('Required parameter {{paramName}} was null or undefined when calling {{nickname}}.');
        }
{{/required}}
{{/allParams}}
{{#queryParams}}
        {{#isListContainer}}
        if ({{paramName}}) {
        {{#isCollectionFormatMulti}}
            {{paramName}}.forEach((element) => {
                queryParameters.append('{{baseName}}', <any>element);
            })
        {{/isCollectionFormatMulti}}
        {{^isCollectionFormatMulti}}
            queryParameters.set('{{baseName}}', {{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}']));
        {{/isCollectionFormatMulti}}
        }
        {{/isListContainer}}
        {{^isListContainer}}
        if ({{paramName}} !== undefined) {
            if({{paramName}} instanceof Date) {
                queryParameters.set('{{baseName}}', <any>{{paramName}}.d.toISOString());
            } else {
                queryParameters.set('{{baseName}}', <any>{{paramName}});
            }
        }
        {{/isListContainer}}

{{/queryParams}}
{{#headerParams}}
        {{#isListContainer}}
        if ({{paramName}}) {
            headers.set('{{baseName}}', {{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}']));
        }
        {{/isListContainer}}
        {{^isListContainer}}
        headers.set('{{baseName}}', String({{paramName}}));
        {{/isListContainer}}

{{/headerParams}}
        // to determine the Content-Type header
        let consumes: string[] = [
            {{#consumes}}
            '{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}
            {{/consumes}}
        ];

        // to determine the Accept header
        let produces: string[] = [
            {{#produces}}
            '{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}
            {{/produces}}
        ];

{{#authMethods}}
        // authentication ({{name}}) required
{{#isApiKey}}
{{#isKeyInHeader}}
        if (this.configuration.apiKey) {
            headers.set('{{keyParamName}}', this.configuration.apiKey);
        }

{{/isKeyInHeader}}
{{#isKeyInQuery}}
        if (this.configuration.apiKey) {
            queryParameters.set('{{keyParamName}}', this.configuration.apiKey);
        }

{{/isKeyInQuery}}
{{/isApiKey}}
{{#isBasic}}
        // http basic authentication required
        if (this.configuration.username || this.configuration.password) {
            headers.set('Authorization', 'Basic ' + btoa(this.configuration.username + ':' + this.configuration.password));
        }

{{/isBasic}}
{{#isOAuth}}
        // oauth required
        if (this.configuration.accessToken) {
            let accessToken = typeof this.configuration.accessToken === 'function'
                ? this.configuration.accessToken()
                : this.configuration.accessToken;
            headers.set('Authorization', 'Bearer ' + accessToken);
        }

{{/isOAuth}}
{{/authMethods}}
{{#hasFormParams}}
        headers.set('Content-Type', 'application/x-www-form-urlencoded');

{{/hasFormParams}}
{{#bodyParam}}
        headers.set('Content-Type', 'application/json');

{{/bodyParam}}
{{#formParams}}
        {{#isListContainer}}
        if ({{paramName}}) {
        {{#isCollectionFormatMulti}}
            {{paramName}}.forEach((element) => {
                formParams.append('{{baseName}}', <any>element);
            })
        {{/isCollectionFormatMulti}}
        {{^isCollectionFormatMulti}}
            formParams.set('{{baseName}}', {{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}']));
        {{/isCollectionFormatMulti}}
        }
        {{/isListContainer}}
        {{^isListContainer}}
        if ({{paramName}} !== undefined) {
            formParams.set('{{baseName}}', <any>{{paramName}});
        }
        {{/isListContainer}}

{{/formParams}}
        let requestOptions: RequestOptionsArgs = new RequestOptions({
            method: {{httpMethod}},
            headers: headers,
{{#bodyParam}}
            body: {{paramName}} == null ? '' : JSON.stringify({{paramName}}), // https://github.com/angular/angular/issues/10612
{{/bodyParam}}
{{#hasFormParams}}
            body: formParams.toString(),
{{/hasFormParams}}
            search: queryParameters
        });

        // https://github.com/swagger-api/swagger-codegen/issues/4037
        if (extraHttpRequestParams) {
            requestOptions = (<any>Object).assign(requestOptions, extraHttpRequestParams);
        }

        return this.http.request(path, requestOptions);
    }

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