Skip to content

Commit

Permalink
Refactory SDK to remove dependency to GatewayConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagobustamante committed Mar 27, 2018
1 parent 0c9a07c commit 854755c
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 47 deletions.
7 changes: 6 additions & 1 deletion src/admin/config/cli-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Configuration } from '../../configuration';
import { Inject } from 'typescript-ioc';
import * as fs from 'fs-extra-promise';
import * as YAML from 'yamljs';
import { getSwaggerHost, getSwaggerUrl, generateSecurityToken } from '../../utils/config';

export class Cli {
@Inject private config: Configuration;
Expand All @@ -21,7 +22,11 @@ export class Cli {
}

async processCommand(): Promise<void> {
this.sdk = await SDK.initialize(this.config.gateway);
this.sdk = await SDK.initialize({
defaultHost: getSwaggerHost(this.config.gateway),
swaggerUrl: getSwaggerUrl(this.config.gateway),
token: generateSecurityToken(this.config.gateway)
});
return this.doCommand();
}

Expand Down
7 changes: 1 addition & 6 deletions src/admin/config/sdk-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import * as _ from 'lodash';
import * as fs from 'fs-extra-promise';
import * as path from 'path';
import * as request from 'request';
import { GatewayConfig } from '../../config/gateway';
import { getSwaggerHost } from '../../utils/config';
import { getResponseBody, checkStatus, invoke } from './utils';

export interface Middleware {
Expand Down Expand Up @@ -91,12 +89,9 @@ export class MiddlewareClient implements Middleware {
private authToken: string;
private middlewareRequest: any;

constructor(swaggerClient: any, authToken: string, gateway: GatewayConfig) {
constructor(swaggerClient: any, authToken: string) {
this.swaggerClient = swaggerClient;
this.authToken = authToken;
if (!this.swaggerClient.spec || !this.swaggerClient.spec.host) {
this.swaggerClient.spec.host = getSwaggerHost(gateway);
}
if (!this.swaggerClient.spec || !this.swaggerClient.spec.schemes ||
!this.swaggerClient.spec.schemes.length || !this.swaggerClient.spec.host) {
throw new Error('Invalid swagger specification. Can not found the target endpoint to call.');
Expand Down
55 changes: 23 additions & 32 deletions src/admin/config/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,49 @@ import { Config, ConfigClient } from './sdk-config-package';
import { Users, UsersClient } from './sdk-users';
import { Gateway, GatewayClient } from './sdk-gateway';
import { Middleware, MiddlewareClient } from './sdk-middleware';
import { GatewayConfig } from '../../config/gateway';
import * as path from 'path';
import { getSwaggerHost } from '../../utils/config';
import * as jwt from 'jsonwebtoken';

const swagger = require('swagger-client');

export interface SDKOptions {
swaggerUrl: string;
token: string;
defaultHost?: string;
}

export class SDK {
private apisClient: Apis;
private gatewayClient: Gateway;
private middlewareClient: Middleware;
private usersClient: Users;
private configClient: Config;

private constructor(swaggerClient: any, authToken: string, gateway: GatewayConfig) {
private constructor(swaggerClient: any, authToken: string) {
this.apisClient = new ApisClient(swaggerClient);
this.configClient = new ConfigClient(swaggerClient);
this.gatewayClient = new GatewayClient(swaggerClient);
this.middlewareClient = new MiddlewareClient(swaggerClient, authToken, gateway);
this.middlewareClient = new MiddlewareClient(swaggerClient, authToken);
this.usersClient = new UsersClient(swaggerClient);
}

static async initialize(gateway: GatewayConfig): Promise<SDK> {
const token: string = SDK.generateSecurityToken(gateway);
const swaggerUrl = SDK.getSwaggerUrl(gateway);
const swaggerClient = await swagger(swaggerUrl, {
static async initialize(options: SDKOptions): Promise<SDK> {
if (!options || !options.token ||
!options.swaggerUrl) {
throw new Error('Invalid parameters. You must informa a valid authentication token and a valid URL to the gateway swagger file.');
}

const swaggerClient = await swagger(options.swaggerUrl, {
authorizations: {
Bearer: `Bearer ${token}`
Bearer: `Bearer ${options.token}`
}
});
return new SDK(swaggerClient, token, gateway);
}

private static generateSecurityToken(gateway: GatewayConfig) {
const dataToken = {
login: 'treeGateway SDK',
name: 'treeGateway SDK',
roles: ['admin', 'config']
};

const token = jwt.sign(dataToken, gateway.admin.userService.jwtSecret, {
expiresIn: 7200
});
return token;
}

private static getSwaggerUrl(gateway: GatewayConfig) {
if (gateway && gateway.admin && gateway.admin.apiDocs) {
const protocol = (gateway.admin.protocol.https ? 'https' : 'http');
return `${protocol}://` + path.posix.join(`${getSwaggerHost(gateway)}`, gateway.admin.apiDocs.path, 'json');
if (!swaggerClient.spec) {
swaggerClient.spec = {};
}
if (!swaggerClient.spec.host && options.defaultHost) {
swaggerClient.spec.host = options.defaultHost;
}
throw new Error('No admin apiDocs configured. Can not access the server rest API');

return new SDK(swaggerClient, options.token);
}

get apis(): Apis {
Expand Down
33 changes: 28 additions & 5 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import { GatewayConfig } from '../config/gateway';
import * as _ from 'lodash';
import * as path from 'path';
import * as jwt from 'jsonwebtoken';

export function getSwaggerHost(gateway: GatewayConfig) {
let host;
Expand All @@ -15,12 +17,33 @@ export function getSwaggerHost(gateway: GatewayConfig) {
return host;
}

export function castArray(obj: any, path: string) {
if (!obj || !path) {
export function getSwaggerUrl(gateway: GatewayConfig) {
if (gateway && gateway.admin && gateway.admin.apiDocs) {
const protocol = (gateway.admin.protocol.https ? 'https' : 'http');
return `${protocol}://` + path.posix.join(`${getSwaggerHost(gateway)}`, gateway.admin.apiDocs.path, 'json');
}
throw new Error('No admin apiDocs configured. Can not access the server rest API');
}

export function generateSecurityToken(gateway: GatewayConfig) {
const dataToken = {
login: 'treeGateway CLI',
name: 'treeGateway CLI',
roles: ['admin', 'config']
};

const token = jwt.sign(dataToken, gateway.admin.userService.jwtSecret, {
expiresIn: 7200
});
return token;
}

export function castArray(obj: any, objPath: string) {
if (!obj || !objPath) {
return;
}
if (_.has(obj, path)) {
const value = _.get(obj, path, null);
_.set(obj, path, _.castArray(value));
if (_.has(obj, objPath)) {
const value = _.get(obj, objPath, null);
_.set(obj, objPath, _.castArray(value));
}
}
7 changes: 6 additions & 1 deletion test/unit/admin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {ApiConfig} from '../../src/config/api';
import {Container} from 'typescript-ioc';
import {Configuration} from '../../src/configuration';
import {SDK} from '../../src/admin/config/sdk';
import { generateSecurityToken, getSwaggerUrl, getSwaggerHost } from '../../src/utils/config';

const expect = chai.expect;
// tslint:disable:no-unused-expression
Expand Down Expand Up @@ -60,7 +61,11 @@ describe('Gateway Admin Tasks', () => {
config = Container.get(Configuration);
adminRequest = request.defaults({baseUrl: `http://localhost:${config.gateway.admin.protocol.http.listenPort}`});

sdk = await SDK.initialize(config.gateway);
sdk = await SDK.initialize({
defaultHost: getSwaggerHost(config.gateway),
swaggerUrl: getSwaggerUrl(config.gateway),
token: generateSecurityToken(config.gateway)
});
await createUsers();
});

Expand Down
7 changes: 6 additions & 1 deletion test/unit/install-apis.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {SDK} from '../../src/admin/config/sdk';
import * as YAML from 'yamljs';
import { ApiConfig } from '../../src/config/api';
import { ConfigPackage } from '../../src/config/config-package';
import { getSwaggerHost, getSwaggerUrl, generateSecurityToken } from '../../src/utils/config';

chai.use(chaiPromises);

Expand Down Expand Up @@ -262,7 +263,11 @@ describe('Gateway SDKs', () => {
await gateway.start();
await gateway.startAdmin();
gateway.server.set('env', 'test');
sdk = await SDK.initialize(config.gateway);
sdk = await SDK.initialize({
defaultHost: getSwaggerHost(config.gateway),
swaggerUrl: getSwaggerUrl(config.gateway),
token: generateSecurityToken(config.gateway)
});
}

function timeout(ms: number) {
Expand Down
7 changes: 6 additions & 1 deletion test/unit/uninstall-apis.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {Configuration} from '../../src/configuration';
import {Gateway} from '../../src/gateway';
import {Database} from '../../src/database';
import {SDK} from '../../src/admin/config/sdk';
import { getSwaggerHost, getSwaggerUrl, generateSecurityToken } from '../../src/utils/config';

// tslint:disable:no-unused-expression
// tslint:disable:no-console
Expand All @@ -24,7 +25,11 @@ describe('Gateway APIs uninstall', () => {
database = Container.get(Database);
gateway = Container.get(Gateway);

sdk = await SDK.initialize(config.gateway);
sdk = await SDK.initialize({
defaultHost: getSwaggerHost(config.gateway),
swaggerUrl: getSwaggerUrl(config.gateway),
token: generateSecurityToken(config.gateway)
});
});

it('should be able to uninstall APIs', async () => {
Expand Down

0 comments on commit 854755c

Please sign in to comment.