Skip to content

Commit

Permalink
BREAKING CHANGE: Redesign SmapiClient class for better extensibility …
Browse files Browse the repository at this point in the history
…to support other auth methods (#597)
  • Loading branch information
ShenChen93 committed Dec 26, 2019
1 parent e1ec8cd commit a0769b0
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 141 deletions.
22 changes: 20 additions & 2 deletions ask-smapi-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,31 @@ Using the `Client ID`, `Client Secret` and `Refresh Token` retrieved in the prev
#### For Node.js
```js
const Alexa = require('ask-smapi-sdk');
const smapiClient = new Alexa.StandardSmapiClientBuilder(clientId, clientSecret, refreshToken).client();

// specify the refreshTokenConfig with clientId, clientSecrect and refreshToken generated in the previous step
const refreshTokenConfig = {
clientId,
clientSecrect,
refreshToken
}
const smapiClient = new Alexa.StandardSmapiClientBuilder()
.withRefreshTokenConfig(refreshTokenConfig)
.client();
```

#### For typescript
```ts
import * as Alexa from 'ask-smapi-sdk';
const smapiClient = new Alexa.StandardSmapiClientBuilder(clientId, clientSecret, refreshToken).client();

// specify the refreshTokenConfig with clientId, clientSecrect and refreshToken generated in the previous step
const refreshTokenConfig : Alexa.RefreshTokenConfig = {
clientId,
clientSecrect,
refreshToken
}
const smapiClient = new Alexa.StandardSmapiClientBuilder()
.withRefreshTokenConfig(refreshTokenConfig)
.client();
```

### List Skills
Expand Down
18 changes: 3 additions & 15 deletions ask-smapi-sdk/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/

export { SmapiClientBuilder } from './smapiClientBuilder/SmapiClientBuilder';
export { StandardSmapiClientBuilder, CustomSmapiClientBuilder } from './smapiClientBuilder/SmapiClientBuilderFactory';
export { SmapiClientBuilder } from './smapiClientBuilder/AbstractSmapiClientBuilder';
export { StandardSmapiClientBuilder, CustomSmapiClientBuilder } from './smapiClientBuilder/SmapiClientBuilder';
export { getValueFromHeader } from './util/Util';
export { RefreshTokenConfig } from './smapiClientBuilder/authMethods/AuthMethods';
42 changes: 42 additions & 0 deletions ask-smapi-sdk/lib/smapiClientBuilder/AbstractSmapiClientBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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 { services } from 'ask-smapi-model';
import { RefreshTokenConfig } from './authMethods/AuthMethods';

/**
* Abstract Builder class which should be implemented.
* @export
* @class SmapiClientBuilder
*/
export class SmapiClientBuilder {

protected customUserAgent : string;
protected refreshTokenConfig : RefreshTokenConfig;

public withCustomUserAgent(userAgent : string) : SmapiClientBuilder {
this.customUserAgent = userAgent;

return this;
}

public withRefreshTokenConfig(refreshTokenConfig : RefreshTokenConfig) : SmapiClientBuilder {
this.refreshTokenConfig = refreshTokenConfig;

return this;
}

public client() : services.skillManagement.SkillManagementServiceClient {
throw new Error('client funtion is not implemented');
}
}
86 changes: 77 additions & 9 deletions ask-smapi-sdk/lib/smapiClientBuilder/SmapiClientBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,90 @@
* permissions and limitations under the License.
*/

import { services } from 'ask-smapi-model';
import { runtime, services } from 'ask-smapi-model';
import ApiConfiguration = runtime.ApiConfiguration;
import ApiClient = runtime.ApiClient;
import AuthenticationConfiguration = runtime.AuthenticationConfiguration;
import DefaultApiClient = runtime.DefaultApiClient;

import { SmapiClientBuilder } from './AbstractSmapiClientBuilder';

const DEFAULT_API_ENDPOINT = 'https://api.amazonalexa.com';

/**
* StandardSmapiClientBuilder class use default ApiClient and default ApiEndpoint
*/
export class StandardSmapiClientBuilder extends SmapiClientBuilder {

/**
* Funtion used to generate SkillManagementService instance.
*/
public client() : services.skillManagement.SkillManagementServiceClient {

if (this.refreshTokenConfig) {
const apiConfiguration : ApiConfiguration = {
apiClient: new DefaultApiClient(),
apiEndpoint: DEFAULT_API_ENDPOINT,
authorizationValue: null,
};
const authenticationConfiguration : AuthenticationConfiguration = {
clientId: this.refreshTokenConfig.clientId,
clientSecret: this.refreshTokenConfig.clientSecret,
refreshToken: this.refreshTokenConfig.refreshToken,
};

return new services.skillManagement.SkillManagementServiceClient(apiConfiguration, authenticationConfiguration, this.customUserAgent);
}

throw new Error('Please provide refreshToken Config to build smapi client');
}
}

/**
* Abstract Builder class which should be implemented.
* @export
* @class SmapiClientBuilder
* CustomSmapiClientBuilder give user ability to configure Apiclient and ApiEndpoint
*/
export class SmapiClientBuilder {
export class CustomSmapiClientBuilder extends StandardSmapiClientBuilder {
private apiClient : ApiClient;
private apiEndpoint : string;

protected apiEndpoint : string;
public withApiEndpoint(apiEndpoint : string) : SmapiClientBuilder {
this.apiEndpoint = apiEndpoint;

public setApiEndpoint(value : string) : void {
this.apiEndpoint = value;
return this;
}

public withApiClient(apiClient : ApiClient) : SmapiClientBuilder {
this.apiClient = apiClient;

return this;
}

/**
* Funtion used to generate SkillManagementService instance.
*/
public client() : services.skillManagement.SkillManagementServiceClient {
throw new Error('client funtion is not implemented');
if (!this.apiEndpoint) {
this.apiEndpoint = DEFAULT_API_ENDPOINT;
}
if (!this.apiClient) {
this.apiClient = new DefaultApiClient();
}

if (this.refreshTokenConfig) {
const apiConfiguration : ApiConfiguration = {
apiClient: this.apiClient,
apiEndpoint: this.apiEndpoint,
authorizationValue: null,
};
const authenticationConfiguration : AuthenticationConfiguration = {
clientId: this.refreshTokenConfig.clientId,
clientSecret: this.refreshTokenConfig.clientSecret,
refreshToken: this.refreshTokenConfig.refreshToken,
};

return new services.skillManagement.SkillManagementServiceClient(apiConfiguration, authenticationConfiguration, this.customUserAgent);
}

throw new Error('Please provide refreshToken Config to build smapi client');
}
}
110 changes: 0 additions & 110 deletions ask-smapi-sdk/lib/smapiClientBuilder/SmapiClientBuilderFactory.ts

This file was deleted.

18 changes: 18 additions & 0 deletions ask-smapi-sdk/lib/smapiClientBuilder/authMethods/AuthMethods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/

export interface RefreshTokenConfig {
clientId : string;
clientSecret : string;
refreshToken : string;
}
10 changes: 5 additions & 5 deletions ask-smapi-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ask-smapi-sdk",
"version": "1.0.0",
"version": "1.1.0",
"description": "Core package for SMAPI Skills Kit SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -15,14 +15,14 @@
"SMAPI",
"SDK"
],
"peerDependencies": {
"ask-smapi-model": "^1.0.0"
"dependencies": {
"ask-smapi-model": "^1.1.0"
},
"devDependencies": {
"@types/chai": "^4.1.2",
"@types/mocha": "^5.0.0",
"@types/node": "^9.6.1",
"@types/sinon": "^4.3.0",
"ask-smapi-model": "^1.0.0",
"chai": "^4.1.2",
"del": "^3.0.0",
"gulp": "^4.0.0",
Expand All @@ -32,7 +32,7 @@
"sinon": "^4.5.0",
"ts-node": "^6.0.1",
"tslint": "^5.9.1",
"typescript": "^2.8.1"
"typescript": "^3.7.3"
},
"repository": "github:alexa/alexa-skills-kit-sdk-for-nodejs",
"bugs": "https://github.com/alexa/alexa-skill-sdk-for-nodejs/issues",
Expand Down

0 comments on commit a0769b0

Please sign in to comment.