[typescript-angular2] access token function (#4361)

* allow function so access token can be derived for each api call

* update tests

* update type for accessToken to be string or function that returns string
This commit is contained in:
Chris Putnam 2016-12-12 11:39:09 -05:00 committed by wing328
parent 77b92d7d11
commit 0b5a6f25da
9 changed files with 152 additions and 83 deletions

View File

@ -143,7 +143,10 @@ export class {{classname}} {
// oauth required
if (this.configuration.accessToken)
{
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
let accessToken = typeof this.configuration.accessToken === 'function'
? this.configuration.accessToken()
: this.configuration.accessToken;
headers.set('Authorization', 'Bearer ' + accessToken);
}
{{/isOAuth}}
{{/authMethods}}

View File

@ -2,5 +2,5 @@ export class Configuration {
apiKey: string;
username: string;
password: string;
accessToken: string;
accessToken: string | () => string;
}

View File

@ -1,72 +1,67 @@
/**
* Swagger Petstore *_/ ' \" =end \\r\\n \\n \\r
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end
* Swagger Petstore *_/ ' \" =end -- \\r\\n \\n \\r
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end --
*
* OpenAPI spec version: 1.0.0 *_/ ' \" =end \\r\\n \\n \\r
* Contact: apiteam@swagger.io *_/ ' \" =end \\r\\n \\n \\r
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
* Contact: apiteam@swagger.io *_/ ' \" =end -- \\r\\n \\n \\r
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Http, Headers, RequestOptionsArgs, Response, URLSearchParams} from '@angular/http';
import {Injectable, Optional} from '@angular/core';
import {Observable} from 'rxjs/Observable';
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 'rxjs/Rx';
import { BASE_PATH } from '../variables';
import { Configuration } from '../configuration';
/* tslint:disable:no-unused-variable member-ordering */
'use strict';
@Injectable()
export class FakeApi {
protected basePath = 'https://petstore.swagger.io *_/ ' \" =end \\r\\n \\n \\r/v2 *_/ ' \" =end \\r\\n \\n \\r';
public defaultHeaders : Headers = new Headers();
protected basePath = 'https://petstore.swagger.io *_/ ' \" =end -- \\r\\n \\n \\r/v2 *_/ ' \" =end -- \\r\\n \\n \\r';
public defaultHeaders: Headers = new Headers();
public configuration: Configuration = new Configuration();
constructor(protected http: Http, @Optional() basePath: string) {
constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
if (basePath) {
this.basePath = basePath;
}
if (configuration) {
this.configuration = configuration;
}
}
/**
* To test code injection *_/ ' \" =end \\r\\n \\n \\r
*
* @param test code inject * ' " =end rn n r To test code injection *_/ ' \" =end \\r\\n \\n \\r
* Extends object by coping non-existing properties.
* @param objA object to be extended
* @param objB source object
*/
public testCodeInjectEndRnNR (test code inject * &#39; &quot; &#x3D;end rn n r?: string, extraHttpRequestParams?: any ) : Observable<{}> {
const path = this.basePath + '/fake';
private extendObj<T1,T2>(objA: T1, objB: T2) {
for(let key in objB){
if(objB.hasOwnProperty(key)){
(objA as any)[key] = (objB as any)[key];
}
}
return <T1&T2>objA;
}
let queryParameters = new URLSearchParams();
let headerParams = this.defaultHeaders;
let formParams = new URLSearchParams();
headerParams.set('Content-Type', 'application/x-www-form-urlencoded');
formParams['test code inject */ &#39; &quot; &#x3D;end \r\n \n \r'] = test code inject * &#39; &quot; &#x3D;end rn n r;
let requestOptions: RequestOptionsArgs = {
method: 'PUT',
headers: headerParams,
search: queryParameters
};
requestOptions.body = formParams.toString();
return this.http.request(path, requestOptions)
/**
* To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
*
* @param test code inject * &#39; &quot; &#x3D;end rn n r To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
*/
public testCodeInjectEndRnNR(test code inject * &#39; &quot; &#x3D;end rn n r?: string, extraHttpRequestParams?: any): Observable<{}> {
return this.testCodeInjectEndRnNRWithHttpInfo(test code inject * &#39; &quot; &#x3D;end rn n r, extraHttpRequestParams)
.map((response: Response) => {
if (response.status === 204) {
return undefined;
@ -76,4 +71,54 @@ export class FakeApi {
});
}
/**
* To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
*
* @param test code inject * &#39; &quot; &#x3D;end rn n r To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
*/
public testCodeInjectEndRnNRWithHttpInfo(test code inject * &#39; &quot; &#x3D;end rn n r?: string, extraHttpRequestParams?: any): Observable<Response> {
const path = this.basePath + `/fake`;
let queryParameters = new URLSearchParams();
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
let formParams = new URLSearchParams();
// to determine the Content-Type header
let consumes: string[] = [
'application/json',
'*_/ =end -- '
];
// to determine the Accept header
let produces: string[] = [
'application/json',
'*_/ =end -- '
];
headers.set('Content-Type', 'application/x-www-form-urlencoded');
if (test code inject * &#39; &quot; &#x3D;end rn n r !== undefined) {
formParams.set('test code inject */ &#39; &quot; &#x3D;end -- \r\n \n \r', <any>test code inject * &#39; &quot; &#x3D;end rn n r);
}
let requestOptions: RequestOptionsArgs = new RequestOptions({
method: RequestMethod.Put,
headers: headers,
body: formParams.toString(),
search: queryParameters
});
// https://github.com/swagger-api/swagger-codegen/issues/4037
if (extraHttpRequestParams) {
requestOptions = this.extendObj(requestOptions, extraHttpRequestParams);
}
return this.http.request(path, requestOptions);
}
}

View File

@ -0,0 +1,6 @@
export class Configuration {
apiKey: string;
username: string;
password: string;
accessToken: string | () => string;
}

View File

@ -1,2 +1,4 @@
export * from './api/api';
export * from './model/models';
export * from './variables';
export * from './configuration';

View File

@ -1,38 +1,24 @@
/**
* Swagger Petstore *_/ ' \" =end \\r\\n \\n \\r
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end
* Swagger Petstore *_/ ' \" =end -- \\r\\n \\n \\r
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end --
*
* OpenAPI spec version: 1.0.0 *_/ ' \" =end \\r\\n \\n \\r
* Contact: apiteam@swagger.io *_/ ' \" =end \\r\\n \\n \\r
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
* Contact: apiteam@swagger.io *_/ ' \" =end -- \\r\\n \\n \\r
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
import * as models from './models';
/**
* Model for testing reserved words *_/ ' \" =end \\r\\n \\n \\r
* Model for testing reserved words *_/ ' \" =end -- \\r\\n \\n \\r
*/
export interface ModelReturn {
/**
* property description *_/ ' \" =end \\r\\n \\n \\r
* property description *_/ ' \" =end -- \\r\\n \\n \\r
*/
return?: number;
}

View File

@ -0,0 +1,3 @@
import { OpaqueToken } from '@angular/core';
export const BASE_PATH = new OpaqueToken('basePath');

View File

@ -217,7 +217,10 @@ export class PetApi {
// oauth required
if (this.configuration.accessToken)
{
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
let accessToken = typeof this.configuration.accessToken === 'function'
? this.configuration.accessToken()
: this.configuration.accessToken;
headers.set('Authorization', 'Bearer ' + accessToken);
}
@ -271,7 +274,10 @@ export class PetApi {
// oauth required
if (this.configuration.accessToken)
{
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
let accessToken = typeof this.configuration.accessToken === 'function'
? this.configuration.accessToken()
: this.configuration.accessToken;
headers.set('Authorization', 'Bearer ' + accessToken);
}
@ -320,7 +326,10 @@ export class PetApi {
// oauth required
if (this.configuration.accessToken)
{
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
let accessToken = typeof this.configuration.accessToken === 'function'
? this.configuration.accessToken()
: this.configuration.accessToken;
headers.set('Authorization', 'Bearer ' + accessToken);
}
@ -369,7 +378,10 @@ export class PetApi {
// oauth required
if (this.configuration.accessToken)
{
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
let accessToken = typeof this.configuration.accessToken === 'function'
? this.configuration.accessToken()
: this.configuration.accessToken;
headers.set('Authorization', 'Bearer ' + accessToken);
}
@ -415,16 +427,19 @@ export class PetApi {
'application/xml'
];
// authentication (api_key) required
if (this.configuration.apiKey)
{
headers.set('api_key', this.configuration.apiKey);
}
// authentication (petstore_auth) required
// oauth required
if (this.configuration.accessToken)
{
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
let accessToken = typeof this.configuration.accessToken === 'function'
? this.configuration.accessToken()
: this.configuration.accessToken;
headers.set('Authorization', 'Bearer ' + accessToken);
}
// authentication (api_key) required
if (this.configuration.apiKey)
{
headers.set('api_key', this.configuration.apiKey);
}
@ -472,7 +487,10 @@ export class PetApi {
// oauth required
if (this.configuration.accessToken)
{
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
let accessToken = typeof this.configuration.accessToken === 'function'
? this.configuration.accessToken()
: this.configuration.accessToken;
headers.set('Authorization', 'Bearer ' + accessToken);
}
@ -529,7 +547,10 @@ export class PetApi {
// oauth required
if (this.configuration.accessToken)
{
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
let accessToken = typeof this.configuration.accessToken === 'function'
? this.configuration.accessToken()
: this.configuration.accessToken;
headers.set('Authorization', 'Bearer ' + accessToken);
}
headers.set('Content-Type', 'application/x-www-form-urlencoded');
@ -592,7 +613,10 @@ export class PetApi {
// oauth required
if (this.configuration.accessToken)
{
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
let accessToken = typeof this.configuration.accessToken === 'function'
? this.configuration.accessToken()
: this.configuration.accessToken;
headers.set('Authorization', 'Bearer ' + accessToken);
}
headers.set('Content-Type', 'application/x-www-form-urlencoded');

View File

@ -2,5 +2,5 @@ export class Configuration {
apiKey: string;
username: string;
password: string;
accessToken: string;
accessToken: string | () => string;
}