Skip to content

Commit 1320d75

Browse files
author
Kenneth Shepherd
committed
We now set the api version number based on the value returned from getServer. We also have some checks on the calls to later api methods to warn the user that the calls won't work.
1 parent d2d1b68 commit 1320d75

File tree

3 files changed

+44
-46
lines changed

3 files changed

+44
-46
lines changed

Diff for: api/index.ts

+41-33
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@ import * as httpsModule from 'https';
33
import { outputConsole, currentWorkspaceFolder } from '../utils';
44
import { config } from '../extension';
55

6+
const DEFAULT_API_VERSION: number = 3;
7+
68
export class AtelierAPI {
79
private cookies: string[] = [];
810
private _config: any;
911
private _namespace: string;
10-
private _apiVersion: string;
12+
private static _apiVersion: number;
1113

1214
private get ns(): string {
1315
return this._namespace || this._config.ns;
1416
}
1517

16-
private get apiVersion(): string {
17-
return this._apiVersion || this._config.apiVersion;
18+
private get apiVersion(): number {
19+
return AtelierAPI._apiVersion || DEFAULT_API_VERSION;
1820
}
1921

2022
constructor() {
@@ -25,8 +27,8 @@ export class AtelierAPI {
2527
this._namespace = namespace;
2628
}
2729

28-
setApiVersion(apiVersion: string) {
29-
this._apiVersion = apiVersion;
30+
setApiVersion(apiVersion: number) {
31+
AtelierAPI._apiVersion = apiVersion;
3032
}
3133

3234
updateCookies(cookies: string[]) {
@@ -143,7 +145,7 @@ export class AtelierAPI {
143145
serverInfo(): Promise<any> {
144146
return this.request('GET');
145147
}
146-
148+
// api v1+
147149
getDocNames({
148150
generated = false,
149151
category = '*',
@@ -155,63 +157,69 @@ export class AtelierAPI {
155157
type?: string;
156158
filter?: string;
157159
}): Promise<any> {
158-
return this.request('GET', `${this.apiVersion}/${this.ns}/docnames/${category}/${type}`, null, {
160+
return this.request('GET', `v${this.apiVersion}/${this.ns}/docnames/${category}/${type}`, null, {
159161
filter,
160162
generated
161163
});
162164
}
163-
165+
// api v1+
164166
getDoc(name: string, format?: string): Promise<any> {
165167
let params = {};
166168
if (format) {
167169
params = {
168170
format
169171
};
170172
}
171-
return this.request('GET', `${this.apiVersion}/${this.ns}/doc/${name}`, params);
173+
return this.request('GET', `v${this.apiVersion}/${this.ns}/doc/${name}`, params);
172174
}
173-
175+
// v1+
174176
putDoc(name: string, data: { enc: boolean; content: string[] }, ignoreConflict?: boolean): Promise<any> {
175177
let params = { ignoreConflict };
176-
return this.request('PUT', `${this.apiVersion}/${this.ns}/doc/${name}`, data, params);
178+
return this.request('PUT', `v${this.apiVersion}/${this.ns}/doc/${name}`, data, params);
177179
}
178-
180+
// v1+
179181
actionIndex(docs: string[]): Promise<any> {
180-
return this.request('POST', `${this.apiVersion}/${this.ns}/action/index`, docs);
182+
return this.request('POST', `v${this.apiVersion}/${this.ns}/action/index`, docs);
181183
}
182-
184+
// v2+
183185
actionSearch(params: { query: string; files?: string; sys?: boolean; gen?: boolean; max?: number }): Promise<any> {
184-
return this.request('GET', `${this.apiVersion}/${this.ns}/action/search`, null, params);
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}`);
185189
}
186-
190+
// v1+
187191
actionQuery(query: string, parameters: string[]): Promise<any> {
188-
return this.request('POST', `${this.apiVersion}/${this.ns}/action/query`, {
192+
return this.request('POST', `v${this.apiVersion}/${this.ns}/action/query`, {
189193
query,
190194
parameters
191195
});
192196
}
193-
197+
// v1+
194198
actionCompile(docs: string[], flags?: string, source = false): Promise<any> {
195-
return this.request('POST', `${this.apiVersion}/${this.ns}/action/compile`, docs, { flags, source });
199+
return this.request('POST', `v${this.apiVersion}/${this.ns}/action/compile`, docs, { flags, source });
196200
}
197201

198202
cvtXmlUdl(source: string): Promise<any> {
199-
return this.request('POST', `${this.apiVersion}/${this.ns}/cvt/xml/doc`, source, {}, { 'Content-Type': 'application/xml' });
203+
return this.request('POST', `v${this.apiVersion}/${this.ns}/cvt/xml/doc`, source, {}, { 'Content-Type': 'application/xml' });
200204
}
201-
205+
// v2+
202206
getmacrodefinition(docname: string, macroname: string, includes: string[]) {
203-
return this.request('POST', `${this.apiVersion}/${this.ns}/action/getmacrodefinition`, {
204-
docname,
205-
macroname,
206-
includes
207-
});
208-
}
209-
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}`);
214+
}
215+
// v2+
210216
getmacrolocation(docname: string, macroname: string, includes: string[]) {
211-
return this.request('POST', `${this.apiVersion}/${this.ns}/action/getmacrolocation`, {
212-
docname,
213-
macroname,
214-
includes
215-
});
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}`);
216224
}
217225
}

Diff for: extension.ts

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
9090
.serverInfo()
9191
.then(info => {
9292
panel.text = `${conn.label}:${conn.ns} - Connected`;
93+
if (info && info.result && info.result.content && info.result.content.api > 0) {
94+
api.setApiVersion(info.result.content.api);
95+
}
9396
explorerProvider.refresh();
9497
})
9598
.catch(error => {

Diff for: package.json

-13
Original file line numberDiff line numberDiff line change
@@ -369,19 +369,6 @@
369369
"type": "boolean",
370370
"default": false
371371
},
372-
"objectscript.conn.apiVersion": {
373-
"description": "Version of the API to use.",
374-
"type": "string",
375-
"enum": [
376-
"v2",
377-
"v3"
378-
],
379-
"enumDescriptions": [
380-
"Use v2 api",
381-
"Use v3 api"
382-
],
383-
"default": "v3"
384-
},
385372
"objectscript.export": {
386373
"type": "object",
387374
"description": "Export only the necessary stuff "

0 commit comments

Comments
 (0)