Skip to content

Commit c34ccb1

Browse files
committed
better way to deal with api version
1 parent 6195b3d commit c34ccb1

File tree

5 files changed

+50
-38
lines changed

5 files changed

+50
-38
lines changed

Diff for: api/index.ts

+34-33
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ export class AtelierAPI {
99
private cookies: string[] = [];
1010
private _config: any;
1111
private _namespace: string;
12-
private static _apiVersion: number;
1312

1413
private get ns(): string {
1514
return this._namespace || this._config.ns;
1615
}
1716

1817
private get apiVersion(): number {
19-
return AtelierAPI._apiVersion || DEFAULT_API_VERSION;
18+
return this._config.version || DEFAULT_API_VERSION;
2019
}
2120

2221
constructor() {
@@ -27,10 +26,6 @@ export class AtelierAPI {
2726
this._namespace = namespace;
2827
}
2928

30-
setApiVersion(apiVersion: number) {
31-
AtelierAPI._apiVersion = apiVersion;
32-
}
33-
3429
updateCookies(cookies: string[]) {
3530
cookies.forEach(cookie => {
3631
let [cookieName] = cookie.split('=');
@@ -48,7 +43,20 @@ export class AtelierAPI {
4843
this._config = conn;
4944
}
5045

51-
async request(method: string, path?: string, body?: any, params?: any, headers?: any): Promise<any> {
46+
async request(
47+
minVersion: number,
48+
method: string,
49+
path?: string,
50+
body?: any,
51+
params?: any,
52+
headers?: any
53+
): Promise<any> {
54+
if (minVersion > this.apiVersion) {
55+
return Promise.reject(`${path} not supported by API version ${this.apiVersion}`);
56+
}
57+
if (minVersion && minVersion > 0) {
58+
path = `v${this.apiVersion}/${path}`;
59+
}
5260
if (!this._config.active) {
5361
return Promise.reject();
5462
}
@@ -83,7 +91,6 @@ export class AtelierAPI {
8391
const http: any = this._config.https ? httpsModule : httpModule;
8492
const agent = new http.Agent({ keepAlive: true, maxSockets: 10 });
8593
path = encodeURI(`/api/atelier/${path || ''}${buildParams()}`);
86-
console.log(`API request: ${method} ${path}`);
8794

8895
if (headers['Content-Type'] && headers['Content-Type'].includes('json')) {
8996
body = JSON.stringify(body);
@@ -143,7 +150,7 @@ export class AtelierAPI {
143150
}
144151

145152
serverInfo(): Promise<any> {
146-
return this.request('GET');
153+
return this.request(0, 'GET');
147154
}
148155
// api v1+
149156
getDocNames({
@@ -157,7 +164,7 @@ export class AtelierAPI {
157164
type?: string;
158165
filter?: string;
159166
}): Promise<any> {
160-
return this.request('GET', `v${this.apiVersion}/${this.ns}/docnames/${category}/${type}`, null, {
167+
return this.request(1, 'GET', `${this.ns}/docnames/${category}/${type}`, null, {
161168
filter,
162169
generated
163170
});
@@ -170,56 +177,50 @@ export class AtelierAPI {
170177
format
171178
};
172179
}
173-
return this.request('GET', `v${this.apiVersion}/${this.ns}/doc/${name}`, params);
180+
return this.request(1, 'GET', `${this.ns}/doc/${name}`, params);
174181
}
175182
// v1+
176183
putDoc(name: string, data: { enc: boolean; content: string[] }, ignoreConflict?: boolean): Promise<any> {
177184
let params = { ignoreConflict };
178-
return this.request('PUT', `v${this.apiVersion}/${this.ns}/doc/${name}`, data, params);
185+
return this.request(1, 'PUT', `${this.ns}/doc/${name}`, data, params);
179186
}
180187
// v1+
181188
actionIndex(docs: string[]): Promise<any> {
182-
return this.request('POST', `v${this.apiVersion}/${this.ns}/action/index`, docs);
189+
return this.request(1, 'POST', `${this.ns}/action/index`, docs);
183190
}
184191
// v2+
185192
actionSearch(params: { query: string; files?: string; sys?: boolean; gen?: boolean; max?: number }): Promise<any> {
186-
return this.apiVersion >= 2 ?
187-
this.request('GET', `v${this.apiVersion}/${this.ns}/action/search`, null, params) :
188-
Promise.reject(`Method 'search' not supported by API version ${this.apiVersion}`);
193+
return this.request(2, 'GET', `${this.ns}/action/search`, null, params);
189194
}
190195
// v1+
191196
actionQuery(query: string, parameters: string[]): Promise<any> {
192-
return this.request('POST', `v${this.apiVersion}/${this.ns}/action/query`, {
197+
return this.request(1, 'POST', `${this.ns}/action/query`, {
193198
query,
194199
parameters
195200
});
196201
}
197202
// v1+
198203
actionCompile(docs: string[], flags?: string, source = false): Promise<any> {
199-
return this.request('POST', `v${this.apiVersion}/${this.ns}/action/compile`, docs, { flags, source });
204+
return this.request(1, 'POST', `${this.ns}/action/compile`, docs, { flags, source });
200205
}
201206

202207
cvtXmlUdl(source: string): Promise<any> {
203-
return this.request('POST', `v${this.apiVersion}/${this.ns}/cvt/xml/doc`, source, {}, { 'Content-Type': 'application/xml' });
208+
return this.request(1, 'POST', `${this.ns}/`, source, {}, { 'Content-Type': 'application/xml' });
204209
}
205210
// v2+
206211
getmacrodefinition(docname: string, macroname: string, includes: string[]) {
207-
return this.apiVersion >= 2 ?
208-
this.request('POST', `v${this.apiVersion}/${this.ns}/action/getmacrodefinition`, {
209-
docname,
210-
macroname,
211-
includes
212-
}) :
213-
Promise.reject(`Method 'getmacrodefinition' not supported by API version ${this.apiVersion}`);
212+
return this.request(2, 'POST', `${this.ns}/action/getmacrodefinition`, {
213+
docname,
214+
macroname,
215+
includes
216+
});
214217
}
215218
// v2+
216219
getmacrolocation(docname: string, macroname: string, includes: string[]) {
217-
return this.apiVersion >= 2 ?
218-
this.request('POST', `v${this.apiVersion}/${this.ns}/action/getmacrolocation`, {
219-
docname,
220-
macroname,
221-
includes
222-
}) :
223-
Promise.reject(`Method 'getmacrolocation' not supported by API version ${this.apiVersion}`);
220+
return this.request(2, 'POST', `${this.ns}/action/getmacrolocation`, {
221+
docname,
222+
macroname,
223+
includes
224+
});
224225
}
225226
}

Diff for: extension.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,16 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
8888
const api = new AtelierAPI();
8989
api
9090
.serverInfo()
91-
.then(info => {
91+
.then(async info => {
9292
panel.text = `${conn.label}:${conn.ns} - Connected`;
9393
if (info && info.result && info.result.content && info.result.content.api > 0) {
94-
api.setApiVersion(info.result.content.api);
94+
let apiVersion = info.result.content.api;
95+
await vscode.workspace.getConfiguration().update('objectscript.conn.version', apiVersion);
9596
}
9697
explorerProvider.refresh();
9798
})
98-
.catch(error => {
99+
.catch((error: Error) => {
100+
outputChannel.appendLine(error.message);
99101
panel.text = `${conn.label}:${conn.ns} - ERROR`;
100102
});
101103
};

Diff for: package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+7
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,13 @@
364364
"type": "string",
365365
"default": "USER"
366366
},
367+
"objectscript.conn.version": {
368+
"description": "API Version number",
369+
"type": "number",
370+
"default": 3,
371+
"readOnly": true,
372+
"deprecationMessage": "This value loads from server"
373+
},
367374
"objectscript.conn.https": {
368375
"description": "Use SSL to access to API",
369376
"type": "boolean",

Diff for: utils/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ export function currentWorkspaceFolder(): string {
7676
if (vscode.window.activeTextEditor && vscode.window.activeTextEditor.document) {
7777
const uri = vscode.window.activeTextEditor.document.uri;
7878
if (uri.scheme === 'file') {
79-
workspaceFolder = vscode.workspace.getWorkspaceFolder(uri).name;
79+
if (vscode.workspace.getWorkspaceFolder(uri)) {
80+
workspaceFolder = vscode.workspace.getWorkspaceFolder(uri).name;
81+
}
8082
} else if (uri.scheme.startsWith('objectscript')) {
8183
workspaceFolder = uri.authority;
8284
}

0 commit comments

Comments
 (0)