Updated Node SDK to expose custom import functions

This commit is contained in:
Wallace Breza 2016-04-20 14:14:31 -07:00
Родитель faf1ce2120
Коммит a48f86ee34
42 изменённых файлов: 208 добавлений и 84 удалений

6
.gitignore поставляемый
Просмотреть файл

@ -1,2 +1,8 @@
node_modules/
typings/
lib/client/**/*.js
lib/client/**/*.js.map
lib/client/**/*.d.ts
lib/core/**/*.js
lib/core/**/*.js.map
lib/core/**/*.d.ts

Просмотреть файл

@ -1,5 +1,5 @@
pushd "%~dp0"
rm -rf client
AutoRest.exe -CodeGenerator NodeJS -Modeler Swagger -Input swagger.json -Namespace powerbi -output lib/client -name PowerBIClient -AddCredentials
AutoRest.exe -CodeGenerator NodeJS -Modeler Swagger -Input swagger.json -Namespace powerbi -output lib/autorest -name PowerBIClient -AddCredentials
popd
pause

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

@ -1473,11 +1473,6 @@ Datasets.prototype.setAllConnections = function (collectionName, workspaceId, da
if (parameters === null || parameters === undefined || typeof parameters !== 'object') {
throw new Error('parameters cannot be null or undefined and it must be of type object.');
}
for(var valueElement in parameters) {
if (parameters[valueElement] !== null && parameters[valueElement] !== undefined && typeof parameters[valueElement] !== 'object') {
throw new Error('parameters[valueElement] must be of type object.');
}
}
} catch (error) {
return callback(error);
}
@ -1520,7 +1515,7 @@ Datasets.prototype.setAllConnections = function (collectionName, workspaceId, da
required: false,
serializedName: 'ObjectElementType',
type: {
name: 'Object'
name: 'String'
}
}
}

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

@ -0,0 +1,5 @@
export interface ImportFileOptions {
datasetDisplayName?: string,
nameConflict?: string,
customHeaders?: { [headerName: string]: string; }
}

Просмотреть файл

@ -0,0 +1,2 @@
export * from './importFileOptions';
export * from '../../autorest/models';

Просмотреть файл

@ -0,0 +1,166 @@
import { PowerBIClient } from '../powerBIClient';
import { ServiceClientOptions, RequestOptions, ServiceCallback, WebResource } from 'ms-rest';
import fs = require('fs');
import util = require('util');
import * as operations from '../../autorest/operations';
import * as models from '../models';
let AuorestImports = require('../../autorest/operations/imports');
let msRest = require('ms-rest');
export class Imports implements operations.Imports {
private client: any;
private base: operations.Imports;
constructor(client: PowerBIClient) {
this.base = new AuorestImports(this);
this.client = client;
}
public getImports(collectionName: string, workspaceId: string, callback: ServiceCallback<models.ODataResponseListImport>): void;
public getImports(collectionName: string, workspaceId: string, options: { customHeaders?: { [headerName: string]: string; } }, callback: ServiceCallback<models.ODataResponseListImport>): void;
public getImports(collectionName: string, workspaceId: string, options: any, callback?: ServiceCallback<models.ODataResponseListImport>): void {
this.base.getImports(collectionName, workspaceId, options, callback);
}
public postImport(collectionName: string, workspaceId: string, importInfo: models.ImportInfo, callback: ServiceCallback<models.ImportModel>): void;
public postImport(collectionName: string, workspaceId: string, importInfo: models.ImportInfo, options: models.ImportFileOptions, callback: ServiceCallback<models.ImportModel>): void;
public postImport(collectionName: string, workspaceId: string, importInfo: models.ImportInfo, options: any, callback?: ServiceCallback<models.ImportModel>): void {
this.base.postImport.apply(this, arguments);
}
public getImportById(collectionName: string, workspaceId: string, importId: string, options: models.ImportFileOptions, callback: ServiceCallback<models.ImportModel>): void;
public getImportById(collectionName: string, workspaceId: string, importId: string, callback: ServiceCallback<models.ImportModel>): void;
public getImportById(collectionName: string, workspaceId: string, importId: string, options: any, callback?: ServiceCallback<models.ImportModel>): void {
this.base.getImportById.apply(this, arguments);
}
public uploadFile(collectionName: string, workspaceId: string, filePath: string, callback: ServiceCallback<models.ImportModel>): void;
public uploadFile(collectionName: string, workspaceId: string, filePath: string, options: models.ImportFileOptions, callback: ServiceCallback<models.ImportModel>): void;
public uploadFile(collectionName: string, workspaceId: string, filePath: string, options: any, callback?: ServiceCallback<models.ImportModel>): void {
if (!callback && typeof options === 'function') {
callback = options;
options = null;
}
if (!callback) {
throw new Error('callback cannot be null.');
}
var datasetDisplayName = (options && options.datasetDisplayName !== undefined) ? options.datasetDisplayName : undefined;
var nameConflict = (options && options.nameConflict !== undefined) ? options.nameConflict : undefined;
// Validate
try {
if (collectionName === null || collectionName === undefined || typeof collectionName.valueOf() !== 'string') {
throw new Error('collectionName cannot be null or undefined and it must be of type string.');
}
if (workspaceId === null || workspaceId === undefined || typeof workspaceId.valueOf() !== 'string') {
throw new Error('workspaceId cannot be null or undefined and it must be of type string.');
}
if (datasetDisplayName !== null && datasetDisplayName !== undefined && typeof datasetDisplayName.valueOf() !== 'string') {
throw new Error('datasetDisplayName must be of type string.');
}
if (nameConflict !== null && nameConflict !== undefined && typeof nameConflict.valueOf() !== 'string') {
throw new Error('nameConflict must be of type string.');
}
if (filePath === null || filePath === undefined) {
throw new Error('filePath cannot be null or undefined.');
}
} catch (error) {
return callback(error, null, null, null);
}
// Construct URL
var requestUrl = this.client['baseUri'] + '//beta/collections/{collectionName}/workspaces/{workspaceId}/imports';
requestUrl = requestUrl.replace('{collectionName}', encodeURIComponent(collectionName));
requestUrl = requestUrl.replace('{workspaceId}', encodeURIComponent(workspaceId));
var queryParameters = [];
if (datasetDisplayName !== null && datasetDisplayName !== undefined) {
queryParameters.push('datasetDisplayName=' + encodeURIComponent(datasetDisplayName));
}
if (nameConflict !== null && nameConflict !== undefined) {
queryParameters.push('nameConflict=' + encodeURIComponent(nameConflict));
}
if (queryParameters.length > 0) {
requestUrl += '?' + queryParameters.join('&');
}
// trim all duplicate forward slashes in the url
var regex = /([^:]\/)\/+/gi;
requestUrl = requestUrl.replace(regex, '$1');
var formData = {
file: fs.createReadStream(filePath)
};
// Create HTTP transport objects
var httpRequest = WebResource.post(requestUrl);
httpRequest['url'] = requestUrl;
httpRequest['method'] = 'POST';
httpRequest['formData'] = formData;
httpRequest['headers'] = {};
// Set Headers
if (options) {
for (var headerName in options.customHeaders) {
if (options.customHeaders.hasOwnProperty(headerName)) {
httpRequest.withHeader(headerName, options.customHeaders[headerName]);
}
}
}
// Send Request
return this.client.pipeline(httpRequest, (err, response, responseBody) => {
if (err) {
return callback(err, null, null, null);
}
var statusCode = response.statusCode;
if (statusCode !== 202) {
var error: any = new Error(responseBody);
error.statusCode = response.statusCode;
error.request = msRest.stripRequest(httpRequest);
error.response = msRest.stripResponse(response);
if (responseBody === '') responseBody = null;
var parsedErrorResponse;
try {
parsedErrorResponse = JSON.parse(responseBody);
if (parsedErrorResponse) {
if (parsedErrorResponse.error) parsedErrorResponse = parsedErrorResponse.error;
if (parsedErrorResponse.code) error.code = parsedErrorResponse.code;
if (parsedErrorResponse.message) error.message = parsedErrorResponse.message;
}
} catch (defaultError) {
error.message = util.format('Error "%s" occurred in deserializing the responseBody ' +
'- "%s" for the default response.', defaultError.message, responseBody);
return callback(error, null, null, null);
}
return callback(error, null, null, null);
}
// Create Result
var result = null;
if (responseBody === '') responseBody = null;
// Deserialize Response
if (statusCode === 202) {
var parsedResponse = null;
try {
parsedResponse = JSON.parse(responseBody);
result = JSON.parse(responseBody);
if (parsedResponse !== null && parsedResponse !== undefined) {
var resultMapper = new this.client.models['ImportModel']().mapper();
result = this.client.deserialize(resultMapper, parsedResponse, 'result');
}
} catch (error) {
var deserializationError: any = new Error(util.format('Error "%s" occurred in deserializing the responseBody - "%s"', error, responseBody));
deserializationError.request = msRest.stripRequest(httpRequest);
deserializationError.response = msRest.stripResponse(response);
return callback(deserializationError, null, null, null);
}
}
return callback(null, result, httpRequest, response);
});
}
}

Просмотреть файл

@ -0,0 +1,4 @@
'use strict'
export { Datasets, Gateways, Reports, Workspaces } from '../../autorest/operations';
export { Imports } from './imports';
export * from '../models';

Просмотреть файл

@ -0,0 +1,14 @@
'use strict';
import AutorestClient = require('../autorest/powerBIClient');
import * as operations from './operations';
import {ServiceClientCredentials, ServiceClientOptions} from 'ms-rest';
export class PowerBIClient extends AutorestClient {
public imports:operations.Imports;
constructor(credentials: ServiceClientCredentials, baseUri: string, options?: ServiceClientOptions) {
super(credentials, baseUri, options);
this.imports = new operations.Imports(this);
}
}

11
lib/core/powerBIToken.d.ts поставляемый
Просмотреть файл

@ -1,11 +0,0 @@
export declare class PowerBIToken {
private claims;
constructor();
static createDevToken(workspaceCollectionName: string, workspaceId: string, expiration?: Date): PowerBIToken;
static createProvisionToken(workspaceCollectionName: string, expiration?: Date): PowerBIToken;
static createReportEmbedToken(workspaceCollectionName: string, workspaceId: string, reportId: string, expiration?: Date): PowerBIToken;
private setTokenExpiration(expiration);
addClaim(key: string, value: any): void;
private static getUnixTime(date);
generate(accessKey: string): string;
}

Просмотреть файл

@ -1,58 +0,0 @@
'use strict';
var jwt = require('jwt-simple');
var PowerBIToken = (function () {
function PowerBIToken() {
this.claims = {
ver: '0.1.0',
aud: 'https://analysis.windows.net/powerbi/api',
iss: 'PowerBISDK'
};
}
PowerBIToken.createDevToken = function (workspaceCollectionName, workspaceId, expiration) {
if (expiration === void 0) { expiration = null; }
var token = new PowerBIToken();
token.addClaim('type', 'dev');
token.addClaim('wcn', workspaceCollectionName);
token.addClaim('wid', workspaceId);
token.setTokenExpiration(expiration);
return token;
};
PowerBIToken.createProvisionToken = function (workspaceCollectionName, expiration) {
if (expiration === void 0) { expiration = null; }
var token = new PowerBIToken();
token.addClaim('type', 'provision');
token.addClaim('wcn', workspaceCollectionName);
token.setTokenExpiration(expiration);
return token;
};
PowerBIToken.createReportEmbedToken = function (workspaceCollectionName, workspaceId, reportId, expiration) {
if (expiration === void 0) { expiration = null; }
var token = new PowerBIToken();
token.addClaim('type', 'provision');
token.addClaim('wcn', workspaceCollectionName);
token.addClaim('wid', workspaceId);
token.addClaim('rid', reportId);
token.setTokenExpiration(expiration);
return token;
};
PowerBIToken.prototype.setTokenExpiration = function (expiration) {
var nowSeconds = PowerBIToken.getUnixTime(new Date());
var expirationSeconds = expiration ? PowerBIToken.getUnixTime(expiration) : nowSeconds + 3600;
if (expirationSeconds <= nowSeconds) {
throw new Error('Token expiration must be in the future');
}
this.addClaim('nbf', nowSeconds);
this.addClaim('exp', expirationSeconds);
};
PowerBIToken.prototype.addClaim = function (key, value) {
this.claims[key] = value;
};
PowerBIToken.getUnixTime = function (date) {
return date.getTime() / 1000 | 0;
};
PowerBIToken.prototype.generate = function (accessKey) {
return jwt.encode(this.claims, accessKey);
};
return PowerBIToken;
}());
exports.PowerBIToken = PowerBIToken;

4
lib/index.d.ts поставляемый
Просмотреть файл

@ -1,3 +1,3 @@
import client = require('./client/powerBIClient');
export * from './client/powerBIClient';
export * from './client/models';
export * from './core/powerBIToken';
export declare let PowerBIClient: typeof client;

Просмотреть файл

@ -2,6 +2,5 @@
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
var client = require('./client/powerBIClient');
__export(require('./client/powerBIClient'));
__export(require('./core/powerBIToken'));
exports.PowerBIClient = client;

Просмотреть файл

@ -1,4 +1,4 @@
'use strict';
import client = require('./client/powerBIClient');
export * from './client/powerBIClient';
export * from './client/models';
export * from './core/powerBIToken';
export let PowerBIClient = client;

Просмотреть файл

@ -25,7 +25,9 @@
},
"homepage": "https://github.com/Microsoft/PowerBI-Node#readme",
"dependencies": {
"form-data": "^0.2.0",
"jwt-simple": "^0.5.0",
"ms-rest": "^1.12.0"
"ms-rest": "^1.12.0",
"request": "^2.71.0"
}
}