1+ import * as vscode from 'vscode' ;
12import * as httpModule from 'http' ;
23import * as httpsModule from 'https' ;
3- import { outputConsole , currentWorkspaceFolder } from '../utils' ;
4+ import { outputConsole , currentWorkspaceFolder , outputChannel } from '../utils' ;
45const Cache = require ( 'vscode-cache' ) ;
5- import { config , extensionContext , workspaceState } from '../extension' ;
6+ import { config , extensionContext , workspaceState , FILESYSTEM_SCHEMA } from '../extension' ;
7+ import * as url from 'url' ;
8+ import * as request from 'request' ;
69
7- const DEFAULT_API_VERSION : number = 3 ;
10+ const DEFAULT_API_VERSION : number = 1 ;
811
912export class AtelierAPI {
1013 private _config : any ;
1114 private _namespace : string ;
1215 private _cache ;
16+ private _workspaceFolder ;
1317
1418 public get ns ( ) : string {
1519 return this . _namespace || this . _config . ns ;
1620 }
1721
1822 private get apiVersion ( ) : number {
19- return workspaceState . get ( currentWorkspaceFolder ( ) + ':apiVersion' , DEFAULT_API_VERSION ) ;
23+ return workspaceState . get ( this . _workspaceFolder + ':apiVersion' , DEFAULT_API_VERSION ) ;
2024 }
2125
22- constructor ( workspaceFolderName ?: string ) {
26+ constructor ( wsOrFile ?: string | vscode . Uri ) {
27+ let workspaceFolderName : string = '' ;
28+ if ( wsOrFile ) {
29+ if ( wsOrFile instanceof vscode . Uri ) {
30+ if ( wsOrFile . scheme === FILESYSTEM_SCHEMA ) {
31+ workspaceFolderName = wsOrFile . authority
32+ let query = url . parse ( decodeURIComponent ( wsOrFile . toString ( ) ) , true ) . query ;
33+ if ( query ) {
34+ if ( query . ns && query . ns !== '' ) {
35+ let namespace = query . ns . toString ( ) ;
36+ this . setNamespace ( namespace ) ;
37+ }
38+ }
39+ }
40+ } else {
41+ workspaceFolderName = wsOrFile
42+ }
43+ }
2344 this . setConnection ( workspaceFolderName || currentWorkspaceFolder ( ) ) ;
2445 const { name, host, port } = this . _config ;
2546 this . _cache = new Cache ( extensionContext , `API:${ name } :${ host } :${ port } ` ) ;
@@ -48,6 +69,7 @@ export class AtelierAPI {
4869 }
4970
5071 setConnection ( workspaceFolderName : string ) {
72+ this . _workspaceFolder = workspaceFolderName ;
5173 let conn = config ( 'conn' , workspaceFolderName ) ;
5274 this . _config = conn ;
5375 }
@@ -97,6 +119,7 @@ export class AtelierAPI {
97119 headers [ 'Cache-Control' ] = 'no-cache' ;
98120
99121 const { host, port, username, password, https } = this . _config ;
122+ const proto = this . _config . https ? 'https' : 'http' ;
100123 const http : any = this . _config . https ? httpsModule : httpModule ;
101124 const agent = new http . Agent ( {
102125 keepAlive : true ,
@@ -109,66 +132,60 @@ export class AtelierAPI {
109132 body = JSON . stringify ( body ) ;
110133 }
111134
135+ // outputChannel.appendLine(`API: ${method} ${host}:${port}${path}`)
112136 return new Promise ( ( resolve , reject ) => {
113- const req : httpModule . ClientRequest = http
114- . request (
115- {
116- method,
117- host,
118- port,
119- path,
120- agent,
121- auth : `${ username } :${ password } ` ,
122- headers,
123- body
124- } ,
125- ( response : httpModule . IncomingMessage ) => {
126- if ( response . statusCode < 200 || response . statusCode > 299 ) {
127- reject ( new Error ( 'Failed to load page "' + path + '", status code: ' + response . statusCode ) ) ;
128- }
129- this . updateCookies ( response . headers [ 'set-cookie' ] ) ;
130- // temporary data holder
131- let body : string = '' ;
132- response . on ( 'data' , chunk => {
133- body += chunk ;
134- } ) ;
135- response . on ( 'end' , ( ) => {
136- if ( response . headers [ 'content-type' ] . includes ( 'json' ) ) {
137- const json = JSON . parse ( body ) ;
138- if ( json . console ) {
139- outputConsole ( json . console ) ;
140- }
141- if ( json . result . status ) {
142- reject ( new Error ( json . result . status ) ) ;
143- return ;
144- }
145- resolve ( json ) ;
146- } else {
147- resolve ( body ) ;
148- }
149- } ) ;
137+ request ( {
138+ url : `${ proto } ://${ host } :${ port } ${ path } ` ,
139+ method,
140+ host,
141+ port,
142+ agent,
143+ auth : { username, password } ,
144+ headers,
145+ body : ( [ 'PUT' , 'POST' ] . includes ( method ) ? body : null )
146+ } , ( error , response , responseBody ) => {
147+ if ( error ) {
148+ reject ( error )
149+ }
150+ if ( response . statusCode === 401 ) {
151+ reject ( {
152+ code : 'Unauthorized' ,
153+ message : 'Not Authorized'
154+ } )
155+ }
156+ if ( response . headers [ 'content-type' ] . includes ( 'json' ) ) {
157+ const json = JSON . parse ( responseBody ) ;
158+ responseBody = '' ;
159+ if ( json . console ) {
160+ outputConsole ( json . console ) ;
150161 }
151- )
152- . on ( 'error' , error => {
153- reject ( error ) ;
154- } ) ;
155- if ( [ 'PUT' , 'POST' ] . includes ( method ) ) {
156- req . write ( body ) ;
157- }
158- req . end ( ) ;
159- } ) . catch ( error => {
160- console . error ( error ) ;
161- throw error ;
162- } ) ;
162+ if ( json . result . status ) {
163+ reject ( new Error ( json . result . status ) ) ;
164+ return ;
165+ }
166+ resolve ( json ) ;
167+ } else {
168+ resolve ( responseBody ) ;
169+ }
170+ } )
171+ } )
163172 }
164173
165174 serverInfo ( ) : Promise < any > {
166- return this . request ( 0 , 'GET' ) . then ( info => {
167- if ( info && info . result && info . result . content && info . result . content . api > 0 ) {
168- let apiVersion = info . result . content . api ;
169- return workspaceState . update ( currentWorkspaceFolder ( ) + ':apiVersion' , apiVersion ) . then ( ( ) => info ) ;
170- }
171- } ) ;
175+ return this . request ( 0 , 'GET' )
176+ . then ( info => {
177+ if ( info && info . result && info . result . content && info . result . content . api > 0 ) {
178+ let data = info . result . content ;
179+ let apiVersion = data . api ;
180+ if ( ! data . namespaces . includes ( this . ns ) ) {
181+ throw {
182+ code : 'WrongNamespace' ,
183+ message : 'This server does not have specified namespace.'
184+ }
185+ }
186+ return workspaceState . update ( currentWorkspaceFolder ( ) + ':apiVersion' , apiVersion ) . then ( ( ) => info ) ;
187+ }
188+ } ) ;
172189 }
173190 // api v1+
174191 getDocNames ( {
@@ -216,6 +233,8 @@ export class AtelierAPI {
216233 }
217234 // v1+
218235 actionQuery ( query : string , parameters : string [ ] ) : Promise < any > {
236+ // outputChannel.appendLine('SQL: ' + query);
237+ // outputChannel.appendLine('SQLPARAMS: ' + JSON.stringify(parameters));
219238 return this . request ( 1 , 'POST' , `${ this . ns } /action/query` , {
220239 query,
221240 parameters
0 commit comments