{{#operations}}
#import "{{classname}}.h"
#import "{{classPrefix}}QueryParamCollection.h"
{{#imports}}#import "{{import}}.h"
{{/imports}}
{{newline}}

@interface {{classname}} ()

@property (nonatomic, strong) NSMutableDictionary *defaultHeaders;

@end

@implementation {{classname}}

NSString* k{{classname}}ErrorDomain = @"{{classname}}ErrorDomain";
NSInteger k{{classname}}MissingParamErrorCode = 234513;

@synthesize apiClient = _apiClient;

#pragma mark - Initialize methods

- (instancetype) init {
    self = [super init];
    if (self) {
        {{classPrefix}}Configuration *config = [{{classPrefix}}Configuration sharedConfig];
        if (config.apiClient == nil) {
            config.apiClient = [[{{classPrefix}}ApiClient alloc] init];
        }
        _apiClient = config.apiClient;
        _defaultHeaders = [NSMutableDictionary dictionary];
    }
    return self;
}

- (id) initWithApiClient:({{classPrefix}}ApiClient *)apiClient {
    self = [super init];
    if (self) {
        _apiClient = apiClient;
        _defaultHeaders = [NSMutableDictionary dictionary];
    }
    return self;
}

#pragma mark -

+ (instancetype)sharedAPI {
    static {{classname}} *sharedAPI;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        sharedAPI = [[self alloc] init];
    });
    return sharedAPI;
}

-(NSString*) defaultHeaderForKey:(NSString*)key {
    return self.defaultHeaders[key];
}

-(void) addHeader:(NSString*)value forKey:(NSString*)key {
    [self setDefaultHeaderValue:value forKey:key];
}

-(void) setDefaultHeaderValue:(NSString*) value forKey:(NSString*)key {
    [self.defaultHeaders setValue:value forKey:key];
}

-(NSUInteger) requestQueueSize {
    return [{{classPrefix}}ApiClient requestQueueSize];
}

#pragma mark - Api Methods

{{#operation}}
///
/// {{{summary}}}
/// {{{notes}}}
/// {{#allParams}} @param {{paramName}} {{{description}}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
///
/// {{/allParams}} @returns {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
///
-(NSNumber*) {{#vendorExtensions.x-objc-operationId}}{{vendorExtensions.x-objc-operationId}}{{/vendorExtensions.x-objc-operationId}}{{^vendorExtensions.x-objc-operationId}}{{nickname}}{{#hasParams}}With{{vendorExtensions.firstParamAltName}}{{/hasParams}}{{^hasParams}}WithCompletionHandler: {{/hasParams}}{{/vendorExtensions.x-objc-operationId}}{{#allParams}}{{#secondaryParam}}
    {{paramName}}{{/secondaryParam}}: ({{{dataType}}}) {{paramName}}{{/allParams}}
    {{#hasParams}}completionHandler: {{/hasParams}}(void (^)({{#returnBaseType}}{{{returnType}}} output, {{/returnBaseType}}NSError* error)) handler {
    {{#allParams}}
    {{#required}}
    // verify the required parameter '{{paramName}}' is set
    if ({{paramName}} == nil) {
        NSParameterAssert({{paramName}});
        if(handler) {
            NSDictionary * userInfo = @{NSLocalizedDescriptionKey : [NSString stringWithFormat:NSLocalizedString(@"Missing required parameter '%@'", nil),@"{{paramName}}"] };
            NSError* error = [NSError errorWithDomain:k{{classname}}ErrorDomain code:k{{classname}}MissingParamErrorCode userInfo:userInfo];
            handler({{#returnType}}nil, {{/returnType}}error);
        }
        return nil;
    }

    {{/required}}
    {{/allParams}}
    NSMutableString* resourcePath = [NSMutableString stringWithFormat:@"{{path}}"];

    // remove format in URL if needed
    [resourcePath replaceOccurrencesOfString:@".{format}" withString:@".json" options:0 range:NSMakeRange(0,resourcePath.length)];

    NSMutableDictionary *pathParams = [[NSMutableDictionary alloc] init];
    {{#pathParams}}
    if ({{paramName}} != nil) {
        pathParams[@"{{baseName}}"] = {{paramName}};
    }
    {{/pathParams}}

    NSMutableDictionary* queryParams = [[NSMutableDictionary alloc] init];
    {{#queryParams}}
    if ({{paramName}} != nil) {
        {{#collectionFormat}}
        queryParams[@"{{baseName}}"] = [[{{classPrefix}}QueryParamCollection alloc] initWithValuesAndFormat: {{paramName}} format: @"{{collectionFormat}}"];
        {{/collectionFormat}}
        {{^collectionFormat}}queryParams[@"{{baseName}}"] = {{paramName}};{{/collectionFormat}}
    }
    {{/queryParams}}
    NSMutableDictionary* headerParams = [NSMutableDictionary dictionaryWithDictionary:self.apiClient.configuration.defaultHeaders];
    [headerParams addEntriesFromDictionary:self.defaultHeaders];
    {{#headerParams}}
    if ({{paramName}} != nil) {
        headerParams[@"{{baseName}}"] = {{paramName}};
    }
    {{/headerParams}}
    // HTTP header `Accept`
    NSString *acceptHeader = [self.apiClient.sanitizer selectHeaderAccept:@[{{#produces}}@"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}}]];
    if(acceptHeader.length > 0) {
        headerParams[@"Accept"] = acceptHeader;
    }

    // response content type
    NSString *responseContentType = [[acceptHeader componentsSeparatedByString:@", "] firstObject] ?: @"";

    // request content type
    NSString *requestContentType = [self.apiClient.sanitizer selectHeaderContentType:@[{{#consumes}}@"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}}]];

    // Authentication setting
    NSArray *authSettings = @[{{#authMethods}}@"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}}];

    id bodyParam = nil;
    NSMutableDictionary *formParams = [[NSMutableDictionary alloc] init];
    NSMutableDictionary *localVarFiles = [[NSMutableDictionary alloc] init];
    {{#bodyParam}}
    bodyParam = {{paramName}};
    {{/bodyParam}}
    {{^bodyParam}}
    {{#formParams}}
    {{#notFile}}
    if ({{paramName}}) {
        formParams[@"{{baseName}}"] = {{paramName}};
    }
    {{/notFile}}
    {{#isFile}}
    localVarFiles[@"{{paramName}}"] = {{paramName}};
    {{/isFile}}
    {{/formParams}}
    {{/bodyParam}}

    return [self.apiClient requestWithPath: resourcePath
                                    method: @"{{httpMethod}}"
                                pathParams: pathParams
                               queryParams: queryParams
                                formParams: formParams
                                     files: localVarFiles
                                      body: bodyParam
                              headerParams: headerParams
                              authSettings: authSettings
                        requestContentType: requestContentType
                       responseContentType: responseContentType
                              responseType: {{^returnType}}nil{{/returnType}}{{#returnType}}@"{{{ returnType }}}"{{/returnType}}
                           completionBlock: ^(id data, NSError *error) {
                                if(handler) {
                                    handler({{#returnType}}({{{ returnType }}})data, {{/returnType}}error);
                                }
                           }
          ];
}

{{/operation}}

{{newline}}
{{/operations}}
@end
