Skip to content

Commit aa3acd7

Browse files
author
AJ Hsu
committed
Remove redundant classes
1 parent 232f425 commit aa3acd7

9 files changed

Lines changed: 52 additions & 110 deletions

File tree

File renamed without changes.

src/auth/Auth.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/auth/ClientCredentialsFlow.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/auth/TokenFetcher.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/auth/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import axios from 'axios';
2+
import { authError } from '../catchError';
3+
import { Token as ENDPOINT_TOKEN } from '../Endpoint';
4+
5+
/**
6+
* Implements various KKBOX OAuth 2.0 authorization flows.
7+
*/
8+
export default class Auth {
9+
/**
10+
* Initialize the Auth object with client id and client secret.
11+
*
12+
* @param {string} clientID
13+
* @param {string} clientSecret
14+
* @example new Auth(clientID, clientSecret);
15+
*/
16+
constructor(clientID, clientSecret) {
17+
this.clientID = clientID;
18+
this.clientSecret = clientSecret;
19+
}
20+
21+
/**
22+
* Fetch access token.
23+
*
24+
* @return {Promise}
25+
* @example auth.fetchAccessToken();
26+
*/
27+
fetchAccessToken() {
28+
return axios({
29+
method: 'POST',
30+
url: ENDPOINT_TOKEN,
31+
headers: {
32+
'content-type': 'application/x-www-form-urlencoded'
33+
},
34+
data: 'grant_type=client_credentials',
35+
auth: {
36+
username: this.clientID,
37+
password: this.clientSecret
38+
}
39+
})
40+
.catch(authError);
41+
}
42+
}

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { default as Auth } from './auth/Auth';
2-
export { default as Api } from './api/Api';
1+
export { default as Auth } from './auth';
2+
export { default as Api } from './api';

src/test/apitest.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,17 @@ import MoodStationFetcher from '../api/MoodStationFetcher';
1212
import GenreStationFetcher from '../api/GenreStationFetcher';
1313
import ChartFetcher from '../api/ChartFetcher';
1414
import TrackFetcher from '../api/TrackFetcher';
15-
import TokenFetcher from '../auth/TokenFetcher';
16-
import ClientCredentials from '../auth/ClientCredentialsFlow';
15+
import Auth from '../auth/';
1716
import { kkbox_sdk } from '../../client_secrets.json';
1817
const CLIENT_ID = kkbox_sdk.client_id;
1918
const CLIENT_SECRET = kkbox_sdk.client_secret;
2019

2120
describe('Api Begin to Test', () => {
22-
describe('ClientCredentials', () => {
21+
describe('Auth', () => {
2322
describe('#fetch the access token()', () => {
2423
it('should fetch access token', () => {
25-
const tokenFetcher = new TokenFetcher(CLIENT_ID, CLIENT_SECRET);
26-
const clientCredentials = new ClientCredentials(tokenFetcher);
27-
return clientCredentials.fetchAccessToken().then(
24+
const auth = new Auth(CLIENT_ID, CLIENT_SECRET);
25+
return auth.fetchAccessToken().then(
2826
response => {
2927
const access_token = response.data.access_token;
3028
access_token.should.be.ok;

src/test/authtest.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import should from 'should';
2-
import TokenFetcher from '../auth/TokenFetcher';
3-
import ClientCredentialsFlow from '../auth/ClientCredentialsFlow';
2+
import Auth from '../auth/';
43
import { kkbox_sdk } from '../../client_secrets.json';
54

65
const CLIENT_ID = kkbox_sdk.client_id;
76
const CLIENT_SECRET = kkbox_sdk.client_secret;
87

98
describe('Auth Begin to Test', () => {
10-
const tokenFetcher = new TokenFetcher(CLIENT_ID, CLIENT_SECRET);
9+
const auth = new Auth(CLIENT_ID, CLIENT_SECRET);
1110
describe('clientCredentialsFlow', () => {
1211
describe('#fetchAccessToken()', () => {
1312
it('should get access token', () => {
14-
const clientCredentialsFlow = new ClientCredentialsFlow(tokenFetcher);
15-
return clientCredentialsFlow.fetchAccessToken().then(response => {
13+
return auth.fetchAccessToken().then(response => {
1614
const access_token = response.data.access_token;
1715
access_token.should.be.ok;
1816
});

src/test/sdktest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('SDK Begin to Test', () => {
99
describe('Auth', () => {
1010
it('should get access token', () => {
1111
const auth = new Auth(CLIENT_ID, CLIENT_SECRET);
12-
return auth.clientCredentialsFlow.fetchAccessToken().then(response => {
12+
return auth.fetchAccessToken().then(response => {
1313
describe('Api With Fake Token', () => {
1414
const api = new Api('FAKE_TOKEN', 'HK');
1515
describe('Property checking', () => {

0 commit comments

Comments
 (0)