Skip to content

Commit 0f0af8c

Browse files
scottdallamurabryanmacfarlane
authored andcommitted
add getItem to FileContainerApi (#101)
1 parent 7058948 commit 0f0af8c

4 files changed

Lines changed: 141 additions & 4 deletions

File tree

api/FileContainerApi.ts

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,103 @@
1212

1313
import stream = require("stream");
1414
import * as restm from 'typed-rest-client/RestClient';
15+
import * as httpm from 'typed-rest-client//HttpClient';
1516
import VsoBaseInterfaces = require('./interfaces/common/VsoBaseInterfaces');
1617
import FileContainerApiBase = require("./FileContainerApiBase");
1718
import FileContainerInterfaces = require("./interfaces/FileContainerInterfaces");
1819
import vsom = require('./VsoClient');
1920

2021
export interface IFileContainerApi extends FileContainerApiBase.IFileContainerApiBase {
2122
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>>;
2224
}
2325

2426
export class FileContainerApi extends FileContainerApiBase.FileContainerApiBase implements IFileContainerApi {
2527
constructor(baseUrl: string, handlers: VsoBaseInterfaces.IRequestHandler[]) {
2628
super(baseUrl, handlers);
2729
}
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+
29112
public createItem(contentStream: NodeJS.ReadableStream, uncompressedLength: number, containerId: number, itemPath: string, scope: string, options: any): Promise<FileContainerInterfaces.FileContainerItem> {
30113
return new Promise<FileContainerInterfaces.FileContainerItem>((resolve, reject) => {
31114
let chunkStream = new ChunkStream(this, uncompressedLength, containerId, itemPath, scope, options);
@@ -61,7 +144,7 @@ export class FileContainerApi extends FileContainerApiBase.FileContainerApiBase
61144
customHeaders["Content-Type"] = "";
62145

63146

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)
65148
.then((versioningData: vsom.ClientVersioningData) => {
66149
var url: string = versioningData.requestUrl;
67150
var serializationData = { responseTypeMetadata: FileContainerInterfaces.TypeInfo.FileContainerItem, responseIsCollection: false };

samples/filecontainer.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
import * as stream from 'stream';
4+
5+
import * as cm from './common';
6+
import * as vm from 'vso-node-api';
7+
8+
import * as ta from 'vso-node-api/FileContainerApi';
9+
import * as ti from 'vso-node-api/interfaces/FileContainerInterfaces';
10+
11+
export async function run() {
12+
try
13+
{
14+
let vsts: vm.WebApi = await cm.getWebApi();
15+
let fileContainerApi = vsts.getFileContainerApi();
16+
17+
let containers = await fileContainerApi.getContainers(null, null);
18+
if (containers.length > 0) {
19+
let container = containers[0];
20+
console.log("found container " + container.name);
21+
22+
let items = await fileContainerApi.getItems(container.id, null, null, null, null, null, null, false);
23+
console.log("found " + items.length + " items");
24+
25+
let item = items.filter((item) => {
26+
return item.itemType === ti.ContainerItemType.File;
27+
})[0];
28+
29+
if (item) {
30+
console.log("downloading " + item.path);
31+
let restResponse = await fileContainerApi.getItem(container.id, null, item.path, item.path.substring(item.path.lastIndexOf('/') + 1));
32+
33+
let output = "";
34+
await new Promise((resolve, reject) => {
35+
restResponse.result.on('data', (chunk) => {
36+
output += chunk;
37+
});
38+
restResponse.result.on('end', () => {
39+
resolve(output);
40+
});
41+
});
42+
console.log("downloaded " + item.path);
43+
console.log(output);
44+
}
45+
46+
}
47+
}
48+
catch (err) {
49+
console.error('Error: ' + err.stack);
50+
}
51+
52+
}

samples/samples.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[
22
"build",
3-
"task"
3+
"task",
4+
"filecontainer"
45
]

samples/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"files": [
88
"run.ts",
99
"build.ts",
10-
"task.ts"
10+
"task.ts",
11+
"filecontainer.ts"
1112
]
1213
}

0 commit comments

Comments
 (0)