Generate enum from api key auth methods. Use this to set api key of authentication. Update test client to set multiple keys.

This commit is contained in:
Alexander Fisher 2016-04-07 10:19:16 -04:00
parent 155458012e
commit b5dabee20a
3 changed files with 96 additions and 99 deletions

View File

@ -96,12 +96,18 @@ class VoidAuth implements Authentication {
* {{&description}} * {{&description}}
*/ */
{{/description}} {{/description}}
export enum {{classname}}ApiKeys {
{{#authMethods}}
{{#isApiKey}}
{{name}},
{{/isApiKey}}
{{/authMethods}}
}
export class {{classname}} { export class {{classname}} {
protected basePath = '{{basePath}}'; protected basePath = '{{basePath}}';
protected defaultHeaders : any = {}; protected defaultHeaders : any = {};
public authentications = { public authentications = {
'default': <Authentication>new VoidAuth(), 'default': <Authentication>new VoidAuth(),
{{#authMethods}} {{#authMethods}}
@ -140,6 +146,10 @@ export class {{classname}} {
} }
} }
} }
public setApiKey(key: {{classname}}ApiKeys, value: string) {
this.authentications[{{classname}}ApiKeys[key]].apiKey = value;
}
{{#authMethods}} {{#authMethods}}
{{#isBasic}} {{#isBasic}}
@ -151,12 +161,6 @@ export class {{classname}} {
this.authentications.{{name}}.password = password; this.authentications.{{name}}.password = password;
} }
{{/isBasic}} {{/isBasic}}
{{#isApiKey}}
set apiKey(key: string) {
this.authentications.{{name}}.apiKey = key;
}
{{/isApiKey}}
{{#isOAuth}} {{#isOAuth}}
set accessToken(token: string) { set accessToken(token: string) {

View File

@ -8,21 +8,33 @@ import http = require('http');
/* tslint:disable:no-unused-variable */ /* tslint:disable:no-unused-variable */
export class Animal {
"className": string;
}
export class Cat extends Animal {
"declawed": boolean;
}
export class Category { export class Category {
"id": number; "id": number;
"name": string; "name": string;
} }
export class Dog extends Animal {
"breed": string;
}
export class InlineResponse200 { export class InlineResponse200 {
"tags": Array<Tag>; "photoUrls": Array<string>;
"name": string;
"id": number; "id": number;
"category": any; "category": any;
"tags": Array<Tag>;
/** /**
* pet status in the store * pet status in the store
*/ */
"status": InlineResponse200.StatusEnum; "status": InlineResponse200.StatusEnum;
"name": string;
"photoUrls": Array<string>;
} }
export namespace InlineResponse200 { export namespace InlineResponse200 {
@ -32,16 +44,26 @@ export namespace InlineResponse200 {
sold = <any> 'sold' sold = <any> 'sold'
} }
} }
/**
* Model for testing model name starting with number
*/
export class Model200Response { export class Model200Response {
"name": number; "name": number;
} }
/**
* Model for testing reserved words
*/
export class ModelReturn { export class ModelReturn {
"return": number; "return": number;
} }
/**
* Model for testing model name same as property name
*/
export class Name { export class Name {
"name": number; "name": number;
"snakeCase": number;
} }
export class Order { export class Order {
@ -154,21 +176,27 @@ class VoidAuth implements Authentication {
} }
} }
export enum PetApiApiKeys {
test_api_client_id,
test_api_client_secret,
api_key,
test_api_key_query,
test_api_key_header,
}
export class PetApi { export class PetApi {
protected basePath = 'http://petstore.swagger.io/v2'; protected basePath = 'http://petstore.swagger.io/v2';
protected defaultHeaders : any = {}; protected defaultHeaders : any = {};
public authentications = { public authentications = {
'default': <Authentication>new VoidAuth(), 'default': <Authentication>new VoidAuth(),
'test_api_key_header': new ApiKeyAuth('header', 'test_api_key_header'), 'petstore_auth': new OAuth(),
'test_api_client_id': new ApiKeyAuth('header', 'x-test_api_client_id'),
'test_api_client_secret': new ApiKeyAuth('header', 'x-test_api_client_secret'),
'api_key': new ApiKeyAuth('header', 'api_key'), 'api_key': new ApiKeyAuth('header', 'api_key'),
'test_http_basic': new HttpBasicAuth(), 'test_http_basic': new HttpBasicAuth(),
'test_api_client_secret': new ApiKeyAuth('header', 'x-test_api_client_secret'),
'test_api_client_id': new ApiKeyAuth('header', 'x-test_api_client_id'),
'test_api_key_query': new ApiKeyAuth('query', 'test_api_key_query'), 'test_api_key_query': new ApiKeyAuth('query', 'test_api_key_query'),
'petstore_auth': new OAuth(), 'test_api_key_header': new ApiKeyAuth('header', 'test_api_key_header'),
} }
constructor(basePath?: string); constructor(basePath?: string);
@ -187,12 +215,12 @@ export class PetApi {
} }
} }
set apiKey(key: string) { public setApiKey(key: PetApiApiKeys, value: string) {
this.authentications.test_api_key_header.apiKey = key; this.authentications[PetApiApiKeys[key]].apiKey = value;
} }
set apiKey(key: string) { set accessToken(token: string) {
this.authentications.api_key.apiKey = key; this.authentications.petstore_auth.accessToken = token;
} }
set username(username: string) { set username(username: string) {
@ -202,22 +230,6 @@ export class PetApi {
set password(password: string) { set password(password: string) {
this.authentications.test_http_basic.password = password; this.authentications.test_http_basic.password = password;
} }
set apiKey(key: string) {
this.authentications.test_api_client_secret.apiKey = key;
}
set apiKey(key: string) {
this.authentications.test_api_client_id.apiKey = key;
}
set apiKey(key: string) {
this.authentications.test_api_key_query.apiKey = key;
}
set accessToken(token: string) {
this.authentications.petstore_auth.accessToken = token;
}
private extendObj<T1,T2>(objA: T1, objB: T2) { private extendObj<T1,T2>(objA: T1, objB: T2) {
for(let key in objB){ for(let key in objB){
if(objB.hasOwnProperty(key)){ if(objB.hasOwnProperty(key)){
@ -525,10 +537,10 @@ export class PetApi {
json: true, json: true,
} }
this.authentications.api_key.applyToRequest(requestOptions);
this.authentications.petstore_auth.applyToRequest(requestOptions); this.authentications.petstore_auth.applyToRequest(requestOptions);
this.authentications.api_key.applyToRequest(requestOptions);
this.authentications.default.applyToRequest(requestOptions); this.authentications.default.applyToRequest(requestOptions);
if (Object.keys(formParams).length) { if (Object.keys(formParams).length) {
@ -583,10 +595,10 @@ export class PetApi {
json: true, json: true,
} }
this.authentications.api_key.applyToRequest(requestOptions);
this.authentications.petstore_auth.applyToRequest(requestOptions); this.authentications.petstore_auth.applyToRequest(requestOptions);
this.authentications.api_key.applyToRequest(requestOptions);
this.authentications.default.applyToRequest(requestOptions); this.authentications.default.applyToRequest(requestOptions);
if (Object.keys(formParams).length) { if (Object.keys(formParams).length) {
@ -641,10 +653,10 @@ export class PetApi {
json: true, json: true,
} }
this.authentications.api_key.applyToRequest(requestOptions);
this.authentications.petstore_auth.applyToRequest(requestOptions); this.authentications.petstore_auth.applyToRequest(requestOptions);
this.authentications.api_key.applyToRequest(requestOptions);
this.authentications.default.applyToRequest(requestOptions); this.authentications.default.applyToRequest(requestOptions);
if (Object.keys(formParams).length) { if (Object.keys(formParams).length) {
@ -854,21 +866,27 @@ export class PetApi {
return localVarDeferred.promise; return localVarDeferred.promise;
} }
} }
export enum StoreApiApiKeys {
test_api_client_id,
test_api_client_secret,
api_key,
test_api_key_query,
test_api_key_header,
}
export class StoreApi { export class StoreApi {
protected basePath = 'http://petstore.swagger.io/v2'; protected basePath = 'http://petstore.swagger.io/v2';
protected defaultHeaders : any = {}; protected defaultHeaders : any = {};
public authentications = { public authentications = {
'default': <Authentication>new VoidAuth(), 'default': <Authentication>new VoidAuth(),
'test_api_key_header': new ApiKeyAuth('header', 'test_api_key_header'), 'petstore_auth': new OAuth(),
'test_api_client_id': new ApiKeyAuth('header', 'x-test_api_client_id'),
'test_api_client_secret': new ApiKeyAuth('header', 'x-test_api_client_secret'),
'api_key': new ApiKeyAuth('header', 'api_key'), 'api_key': new ApiKeyAuth('header', 'api_key'),
'test_http_basic': new HttpBasicAuth(), 'test_http_basic': new HttpBasicAuth(),
'test_api_client_secret': new ApiKeyAuth('header', 'x-test_api_client_secret'),
'test_api_client_id': new ApiKeyAuth('header', 'x-test_api_client_id'),
'test_api_key_query': new ApiKeyAuth('query', 'test_api_key_query'), 'test_api_key_query': new ApiKeyAuth('query', 'test_api_key_query'),
'petstore_auth': new OAuth(), 'test_api_key_header': new ApiKeyAuth('header', 'test_api_key_header'),
} }
constructor(basePath?: string); constructor(basePath?: string);
@ -887,12 +905,12 @@ export class StoreApi {
} }
} }
set apiKey(key: string) { public setApiKey(key: StoreApiApiKeys, value: string) {
this.authentications.test_api_key_header.apiKey = key; this.authentications[StoreApiApiKeys[key]].apiKey = value;
} }
set apiKey(key: string) { set accessToken(token: string) {
this.authentications.api_key.apiKey = key; this.authentications.petstore_auth.accessToken = token;
} }
set username(username: string) { set username(username: string) {
@ -902,22 +920,6 @@ export class StoreApi {
set password(password: string) { set password(password: string) {
this.authentications.test_http_basic.password = password; this.authentications.test_http_basic.password = password;
} }
set apiKey(key: string) {
this.authentications.test_api_client_secret.apiKey = key;
}
set apiKey(key: string) {
this.authentications.test_api_client_id.apiKey = key;
}
set apiKey(key: string) {
this.authentications.test_api_key_query.apiKey = key;
}
set accessToken(token: string) {
this.authentications.petstore_auth.accessToken = token;
}
private extendObj<T1,T2>(objA: T1, objB: T2) { private extendObj<T1,T2>(objA: T1, objB: T2) {
for(let key in objB){ for(let key in objB){
if(objB.hasOwnProperty(key)){ if(objB.hasOwnProperty(key)){
@ -1164,10 +1166,10 @@ export class StoreApi {
json: true, json: true,
} }
this.authentications.test_api_key_header.applyToRequest(requestOptions);
this.authentications.test_api_key_query.applyToRequest(requestOptions); this.authentications.test_api_key_query.applyToRequest(requestOptions);
this.authentications.test_api_key_header.applyToRequest(requestOptions);
this.authentications.default.applyToRequest(requestOptions); this.authentications.default.applyToRequest(requestOptions);
if (Object.keys(formParams).length) { if (Object.keys(formParams).length) {
@ -1246,21 +1248,27 @@ export class StoreApi {
return localVarDeferred.promise; return localVarDeferred.promise;
} }
} }
export enum UserApiApiKeys {
test_api_client_id,
test_api_client_secret,
api_key,
test_api_key_query,
test_api_key_header,
}
export class UserApi { export class UserApi {
protected basePath = 'http://petstore.swagger.io/v2'; protected basePath = 'http://petstore.swagger.io/v2';
protected defaultHeaders : any = {}; protected defaultHeaders : any = {};
public authentications = { public authentications = {
'default': <Authentication>new VoidAuth(), 'default': <Authentication>new VoidAuth(),
'test_api_key_header': new ApiKeyAuth('header', 'test_api_key_header'), 'petstore_auth': new OAuth(),
'test_api_client_id': new ApiKeyAuth('header', 'x-test_api_client_id'),
'test_api_client_secret': new ApiKeyAuth('header', 'x-test_api_client_secret'),
'api_key': new ApiKeyAuth('header', 'api_key'), 'api_key': new ApiKeyAuth('header', 'api_key'),
'test_http_basic': new HttpBasicAuth(), 'test_http_basic': new HttpBasicAuth(),
'test_api_client_secret': new ApiKeyAuth('header', 'x-test_api_client_secret'),
'test_api_client_id': new ApiKeyAuth('header', 'x-test_api_client_id'),
'test_api_key_query': new ApiKeyAuth('query', 'test_api_key_query'), 'test_api_key_query': new ApiKeyAuth('query', 'test_api_key_query'),
'petstore_auth': new OAuth(), 'test_api_key_header': new ApiKeyAuth('header', 'test_api_key_header'),
} }
constructor(basePath?: string); constructor(basePath?: string);
@ -1279,12 +1287,12 @@ export class UserApi {
} }
} }
set apiKey(key: string) { public setApiKey(key: UserApiApiKeys, value: string) {
this.authentications.test_api_key_header.apiKey = key; this.authentications[UserApiApiKeys[key]].apiKey = value;
} }
set apiKey(key: string) { set accessToken(token: string) {
this.authentications.api_key.apiKey = key; this.authentications.petstore_auth.accessToken = token;
} }
set username(username: string) { set username(username: string) {
@ -1294,22 +1302,6 @@ export class UserApi {
set password(password: string) { set password(password: string) {
this.authentications.test_http_basic.password = password; this.authentications.test_http_basic.password = password;
} }
set apiKey(key: string) {
this.authentications.test_api_client_secret.apiKey = key;
}
set apiKey(key: string) {
this.authentications.test_api_client_id.apiKey = key;
}
set apiKey(key: string) {
this.authentications.test_api_key_query.apiKey = key;
}
set accessToken(token: string) {
this.authentications.petstore_auth.accessToken = token;
}
private extendObj<T1,T2>(objA: T1, objB: T2) { private extendObj<T1,T2>(objA: T1, objB: T2) {
for(let key in objB){ for(let key in objB){
if(objB.hasOwnProperty(key)){ if(objB.hasOwnProperty(key)){
@ -1524,7 +1516,7 @@ export class UserApi {
/** /**
* Get user by user name * Get user by user name
* *
* @param username The name that needs to be fetched. Use user1 for testing. * @param username The name that needs to be fetched. Use user1 for testing.
*/ */
public getUserByName (username: string) : Promise<{ response: http.ClientResponse; body: User; }> { public getUserByName (username: string) : Promise<{ response: http.ClientResponse; body: User; }> {
const localVarPath = this.basePath + '/user/{username}' const localVarPath = this.basePath + '/user/{username}'

View File

@ -2,7 +2,8 @@ import api = require('./api');
import fs = require('fs'); import fs = require('fs');
var petApi = new api.PetApi(); var petApi = new api.PetApi();
petApi.apiKey = 'special-key'; petApi.setApiKey(api.PetApiApiKeys.api_key, 'special-key');
petApi.setApiKey(api.PetApiApiKeys.test_api_key_header, 'query-key');
var tag1 = new api.Tag(); var tag1 = new api.Tag();
tag1.id = 18291; tag1.id = 18291;