|
12 | 12 |
|
13 | 13 | import stream = require("stream"); |
14 | 14 | import * as restm from 'typed-rest-client/RestClient'; |
| 15 | +import * as httpm from 'typed-rest-client//HttpClient'; |
15 | 16 | import VsoBaseInterfaces = require('./interfaces/common/VsoBaseInterfaces'); |
16 | 17 | import FileContainerApiBase = require("./FileContainerApiBase"); |
17 | 18 | import FileContainerInterfaces = require("./interfaces/FileContainerInterfaces"); |
18 | 19 | import vsom = require('./VsoClient'); |
19 | 20 |
|
20 | 21 | export interface IFileContainerApi extends FileContainerApiBase.IFileContainerApiBase { |
21 | 22 | createItem(contentStream: NodeJS.ReadableStream, uncompressedLength: number, containerId: number, itemPath: string, scope: string, options: any): Promise<FileContainerInterfaces.FileContainerItem>; |
| 23 | + getItem(containerId: number, scope?: string, itemPath?: string, downloadFileName?: string): Promise<restm.IRestResponse<NodeJS.ReadableStream>>; |
22 | 24 | } |
23 | 25 |
|
24 | 26 | export class FileContainerApi extends FileContainerApiBase.FileContainerApiBase implements IFileContainerApi { |
25 | 27 | constructor(baseUrl: string, handlers: VsoBaseInterfaces.IRequestHandler[]) { |
26 | 28 | super(baseUrl, handlers); |
27 | 29 | } |
28 | | - |
| 30 | + |
| 31 | + /** |
| 32 | + * @param {number} containerId |
| 33 | + * @param {string} scope |
| 34 | + * @param {string} itemPath |
| 35 | + * @param {string} downloadFileName |
| 36 | + */ |
| 37 | + public async getItem( |
| 38 | + containerId: number, |
| 39 | + scope?: string, |
| 40 | + itemPath?: string, |
| 41 | + downloadFileName?: string |
| 42 | + ): Promise<restm.IRestResponse<NodeJS.ReadableStream>> { |
| 43 | + |
| 44 | + return new Promise<restm.IRestResponse<NodeJS.ReadableStream>>(async (resolve, reject) => { |
| 45 | + let routeValues: any = { |
| 46 | + containerId: containerId |
| 47 | + }; |
| 48 | + |
| 49 | + let queryValues: any = { |
| 50 | + scope: scope, |
| 51 | + itemPath: itemPath, |
| 52 | + '$format': "OctetStream", |
| 53 | + downloadFileName: downloadFileName |
| 54 | + }; |
| 55 | + |
| 56 | + try { |
| 57 | + let verData: vsom.ClientVersioningData = await this.vsoClient.getVersioningData( |
| 58 | + "3.2-preview.4", |
| 59 | + "Container", |
| 60 | + "e4f5c81e-e250-447b-9fef-bd48471bea5e", |
| 61 | + routeValues, |
| 62 | + queryValues); |
| 63 | + |
| 64 | + let url: string = verData.requestUrl; |
| 65 | + let options: restm.IRequestOptions = this.createRequestOptions('application/octet-stream', |
| 66 | + verData.apiVersion); |
| 67 | + |
| 68 | + let res = await this.http.get(url); |
| 69 | + |
| 70 | + let rres: restm.IRestResponse<NodeJS.ReadableStream> = <restm.IRestResponse<NodeJS.ReadableStream>>{}; |
| 71 | + let statusCode = res.message.statusCode; |
| 72 | + rres.statusCode = statusCode; |
| 73 | + // not found leads to null obj returned |
| 74 | + if (statusCode == httpm.HttpCodes.NotFound) { |
| 75 | + resolve(rres); |
| 76 | + } |
| 77 | + |
| 78 | + if (statusCode > 299) { |
| 79 | + let msg; |
| 80 | + // if exception/error in body, attempt to get better error |
| 81 | + let contents = await res.readBody(); |
| 82 | + let obj; |
| 83 | + if (contents && contents.length > 0) { |
| 84 | + obj = JSON.parse(contents); |
| 85 | + if (options && options.responseProcessor) { |
| 86 | + rres.result = options.responseProcessor(obj); |
| 87 | + } |
| 88 | + else { |
| 89 | + rres.result = obj; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if (obj && obj.message) { |
| 94 | + msg = obj.message; |
| 95 | + } |
| 96 | + else { |
| 97 | + msg = "Failed request: (" + statusCode + ") " + res.message.url; |
| 98 | + } |
| 99 | + reject(new Error(msg)); |
| 100 | + } |
| 101 | + else { |
| 102 | + rres.result = res.message; |
| 103 | + resolve(rres); |
| 104 | + } |
| 105 | + } |
| 106 | + catch (err) { |
| 107 | + reject(err); |
| 108 | + } |
| 109 | + }); |
| 110 | + } |
| 111 | + |
29 | 112 | public createItem(contentStream: NodeJS.ReadableStream, uncompressedLength: number, containerId: number, itemPath: string, scope: string, options: any): Promise<FileContainerInterfaces.FileContainerItem> { |
30 | 113 | return new Promise<FileContainerInterfaces.FileContainerItem>((resolve, reject) => { |
31 | 114 | let chunkStream = new ChunkStream(this, uncompressedLength, containerId, itemPath, scope, options); |
@@ -61,7 +144,7 @@ export class FileContainerApi extends FileContainerApiBase.FileContainerApiBase |
61 | 144 | customHeaders["Content-Type"] = ""; |
62 | 145 |
|
63 | 146 |
|
64 | | - this.vsoClient.getVersioningData("2.2-preview.3", "Container", "e4f5c81e-e250-447b-9fef-bd48471bea5e", routeValues, queryValues) |
| 147 | + this.vsoClient.getVersioningData("3.2-preview.4", "Container", "e4f5c81e-e250-447b-9fef-bd48471bea5e", routeValues, queryValues) |
65 | 148 | .then((versioningData: vsom.ClientVersioningData) => { |
66 | 149 | var url: string = versioningData.requestUrl; |
67 | 150 | var serializationData = { responseTypeMetadata: FileContainerInterfaces.TypeInfo.FileContainerItem, responseIsCollection: false }; |
|
0 commit comments