本文整理匯總了TypeScript中ms-rest.ServiceCallback類的典型用法代碼示例。如果您正苦於以下問題:TypeScript ServiceCallback類的具體用法?TypeScript ServiceCallback怎麽用?TypeScript ServiceCallback使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了ServiceCallback類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: uploadFile
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'] + '//v1.0/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
//.........這裏部分代碼省略.........
示例2: callback
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);
});