1
1
'use strict' ;
2
2
3
- var http = require ( 'http' ) ;
4
3
var Request = require ( 'request' ) ;
5
4
var Q = require ( 'q' ) ;
6
5
var zlib = require ( 'zlib' ) ;
@@ -15,7 +14,7 @@ function Codeine(server,port,api_key) {
15
14
this . server = server ;
16
15
this . api_key = api_key ;
17
16
this . port = port ;
18
- this . request = Request . defaults ( { 'headers' : { 'api_token' : this . api_key } } ) ;
17
+ this . request = Request . defaults ( { 'headers' : { 'api_token' : this . api_key } , 'followAllRedirects' : true } ) ;
19
18
}
20
19
21
20
function getCodeinePath ( self ) {
@@ -53,7 +52,7 @@ Codeine.prototype.createNewProject = function(projectName, selectedProject) {
53
52
var deferred = Q . defer ( ) ;
54
53
var uri = getCodeinePath ( this ) + 'projects' ;
55
54
log ( 'createNewProject() - uri is ' + uri ) ;
56
- this . request ( createNewProjectCommand ( uri , this . api_key , projectName , selectedProject ) , function ( error , response , body ) {
55
+ this . request ( createNewProjectCommand ( uri , projectName , selectedProject ) , function ( error , response , body ) {
57
56
if ( ! error && response && response . statusCode === 200 ) {
58
57
deferred . resolve ( body ) ;
59
58
}
@@ -73,31 +72,27 @@ Codeine.prototype.createNewProject = function(projectName, selectedProject) {
73
72
74
73
Codeine . prototype . runCommand = function ( projectName , commandObject ) {
75
74
var deferred = Q . defer ( ) ;
76
- var runCommandOptions = createCodeineRunCommand ( this . server , projectName , this . api_key , this . port ) ;
77
- log ( 'runCommand() - command object ' , runCommandOptions ) ;
78
75
var post_data = JSON . stringify ( commandObject ) ;
79
76
log ( 'runCommand() - post data is ' + post_data ) ;
80
- var commandReq = http . request ( runCommandOptions , function ( res ) {
81
- res . setEncoding ( 'utf8' ) ;
77
+ var uri = getCodeinePath ( this ) + 'command-nodes?project=' + projectName ;
78
+ var runCommandOptions = createCodeineRunCommand ( uri , post_data ) ;
79
+ log ( 'runCommand() - command object ' , runCommandOptions ) ;
82
80
83
- if ( res . statusCode === 200 ) {
84
- log ( 'runCommand() - Command ran successfully' ) ;
81
+ this . request ( runCommandOptions , function ( error , response , body ) {
82
+ if ( ! error && response && response . statusCode === 200 ) {
83
+ deferred . resolve ( body ) ;
85
84
}
86
85
else {
87
- console . error ( 'codeine:: runCommand() - Status Code = ' + res . statusCode ) ;
88
- deferred . reject ( ) ;
86
+ log ( 'runCommand() - Error is' , error ) ;
87
+ if ( response && response . statusCode ) {
88
+ console . error ( 'codeine:: runCommand() - Status Code = ' + response . statusCode ) ;
89
+ deferred . reject ( response . statusCode ) ;
90
+ }
91
+ else {
92
+ deferred . reject ( error ) ;
93
+ }
89
94
}
90
- res . on ( 'data' , function ( chunk ) {
91
- log ( 'Response: ' , chunk ) ;
92
- deferred . resolve ( chunk ) ;
93
- } ) ;
94
95
} ) ;
95
- commandReq . write ( post_data ) ;
96
- commandReq . on ( 'error' , function ( e ) {
97
- console . error ( 'codeine:: runCommand() - Error from server' , e ) ;
98
- deferred . reject ( e ) ;
99
- } ) ;
100
- commandReq . end ( ) ;
101
96
return deferred . promise ;
102
97
} ;
103
98
@@ -106,6 +101,7 @@ Codeine.prototype.runCommandSync = function (projectName, commandObject, sleep,
106
101
log ( 'runCommandSync() - Running command' , commandObject . command_info . name ) ;
107
102
return this . runCommand ( projectName , commandObject )
108
103
. then ( function ( commandId ) {
104
+ commandId = commandId . toString ( ) ;
109
105
log ( 'runCommandSync() - Waiting for command' , commandId , 'to finish' ) ;
110
106
function check ( iteration ) {
111
107
log ( 'runCommandSync() - waiting for command' , commandId , 'to finish.' +
@@ -245,29 +241,28 @@ Codeine.prototype.getProjectStatus = function(projectName) {
245
241
246
242
Codeine . prototype . saveProjectConfiguration = function ( projectName , conf ) {
247
243
var deferred = Q . defer ( ) ;
248
- var saveConfCommandOptions = createSaveConfCommand ( this . server , projectName , this . api_key , this . port ) ;
244
+ var uri = getCodeinePath ( this ) + 'project-configuration?project=' + projectName ;
249
245
var post_data = JSON . stringify ( conf ) ;
246
+ var saveConfCommandOptions = createSaveConfCommand ( uri , post_data ) ;
250
247
log ( 'saveProjectConfiguration() - post data is ' , post_data ) ;
251
- var commandReq = http . request ( saveConfCommandOptions , function ( res ) {
252
- res . setEncoding ( 'utf8' ) ;
253
- if ( res . statusCode === 200 ) {
248
+
249
+ this . request ( saveConfCommandOptions , function ( error , response , body ) {
250
+ if ( ! error && response && response . statusCode === 200 ) {
254
251
log ( 'saveProjectConfiguration() - Configuration was saved successfully' ) ;
252
+ var config = JSON . parse ( body ) ;
253
+ deferred . resolve ( config ) ;
255
254
}
256
255
else {
257
- console . error ( 'coeine:: saveProjectConfiguration() - Status Code = ' + res . statusCode ) ;
258
- deferred . reject ( ) ;
256
+ log ( 'saveProjectConfiguration() - Error is' , error ) ;
257
+ if ( response && response . statusCode ) {
258
+ console . error ( 'codeine:: saveProjectConfiguration() - Status Code = ' + response . statusCode ) ;
259
+ deferred . reject ( response . statusCode ) ;
260
+ }
261
+ else {
262
+ deferred . reject ( error ) ;
263
+ }
259
264
}
260
- res . on ( 'data' , function ( chunk ) {
261
- log ( 'Response: ' , chunk ) ;
262
- deferred . resolve ( chunk ) ;
263
- } ) ;
264
- } ) ;
265
- commandReq . write ( post_data ) ;
266
- commandReq . on ( 'error' , function ( e ) {
267
- console . error ( 'codeine:: saveProjectConfiguration() - Error from server' , e ) ;
268
- deferred . reject ( e ) ;
269
265
} ) ;
270
- commandReq . end ( ) ;
271
266
return deferred . promise ;
272
267
} ;
273
268
@@ -302,7 +297,7 @@ Codeine.prototype.getCommandOutput = function(projectName, commandId) {
302
297
return this . request ( { url : uri , encoding : null } ) ;
303
298
} ;
304
299
305
- function createNewProjectCommand ( url , api_token , projectName , selectedProject ) {
300
+ function createNewProjectCommand ( url , projectName , selectedProject ) {
306
301
var requestData = {
307
302
'project_name' : projectName ,
308
303
'type' : 'New'
@@ -314,31 +309,26 @@ function createNewProjectCommand(url,api_token,projectName,selectedProject) {
314
309
return {
315
310
url : url ,
316
311
encoding : null ,
317
- headers : {
318
- 'api_token' : api_token
319
- } ,
320
312
method : 'POST' ,
321
313
body : JSON . stringify ( requestData )
322
314
} ;
323
315
}
324
316
325
- function createSaveConfCommand ( server , project , api_token , port ) {
317
+ function createSaveConfCommand ( uri , requestData ) {
326
318
return {
327
- hostname : server ,
328
- headers : { 'api_token' : api_token } ,
329
- port : port ,
330
- path : '/api-with-token/project-configuration?project=' + project ,
331
- method : 'PUT'
319
+ url : uri ,
320
+ encoding : null ,
321
+ method : 'PUT' ,
322
+ body : requestData
332
323
} ;
333
324
}
334
325
335
- function createCodeineRunCommand ( server , project , api_token , port ) {
326
+ function createCodeineRunCommand ( uri , requestData ) {
336
327
return {
337
- hostname : server ,
338
- headers : { 'api_token' : api_token } ,
339
- port : port ,
340
- path : '/api-with-token/command-nodes?project=' + project ,
341
- method : 'POST'
328
+ url : uri ,
329
+ encoding : null ,
330
+ method : 'POST' ,
331
+ body : requestData
342
332
} ;
343
333
}
344
334
0 commit comments