forked from hyperledger-archives/composer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for HTTP POST from Transaction Processor functions (hyp…
…erledger-archives#1073) * Fix for #838, isAssignableFrom bug with types from different ns * Added tutorial 2 * Added Tutorial 2 * wip commit, basic fabric and web runtime for HTTP POST * Fixed crash on error * wip * unit and system tests for HTTP POST * Fixed default debug level * Removed console.log * Fixed doc * Fixed initialization * Fixed v0.6 impl and added docs
- Loading branch information
Showing
36 changed files
with
1,720 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ | |
class Writer { | ||
|
||
/** | ||
* Create a FileWriter. | ||
* Create a Writer. | ||
* | ||
*/ | ||
constructor() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
packages/composer-runtime-embedded/lib/embeddedhttpservice.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const HTTPService = require('composer-runtime').HTTPService; | ||
const Logger = require('composer-common').Logger; | ||
const request = require('request'); | ||
const LOG = Logger.getLog('EmbeddedHTTPService'); | ||
|
||
/** | ||
* Base class representing the http service provided by a {@link Container}. | ||
* @protected | ||
*/ | ||
class EmbeddedHTTPService extends HTTPService { | ||
|
||
/** | ||
* Constructor. | ||
* @param {EventEmitter} eventSink the event emitter | ||
*/ | ||
constructor() { | ||
super(); | ||
const method = 'constructor'; | ||
LOG.exit(method); | ||
} | ||
|
||
/** | ||
* Post data | ||
* @abstract | ||
* @return {Promise} A Promise that return the JSON text for the HTTP POST. It captures the status code, header and body of the HTTP POST. The body must also be returned as embedded JSON text. | ||
* @throws {Error} throws an error if there is an issue | ||
*/ | ||
_post() { | ||
const self = this; | ||
return new Promise(function (resolve, reject) { | ||
|
||
const options = { | ||
url : self.url, | ||
method: 'POST', | ||
body: self.data, | ||
json: true | ||
}; | ||
|
||
request.post(options, | ||
function (err, resp, body) { | ||
LOG.info('error:', err); | ||
LOG.info('statusCode:', resp && resp.statusCode); | ||
LOG.info('body:', body); | ||
|
||
if(resp) { | ||
resolve( JSON.stringify({ | ||
statusCode: resp.statusCode, | ||
body: (err) ? err : body | ||
})); | ||
} | ||
else { | ||
reject( JSON.stringify({ | ||
statusCode: 500, | ||
body: (err) ? err : body | ||
})); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = EmbeddedHTTPService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
packages/composer-runtime-embedded/test/embeddedhttpservice.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const EmbeddedHTTPService = require('..').EmbeddedHTTPService; | ||
|
||
const request = require('request'); | ||
const chai = require('chai'); | ||
const sinon = require('sinon'); | ||
require('sinon-as-promised'); | ||
require('chai-as-promised'); | ||
chai.should(); | ||
const expect = chai.expect; | ||
|
||
describe('EmbeddedHTTPService', () => { | ||
|
||
let httpService; | ||
let sandbox; | ||
|
||
beforeEach(() => { | ||
httpService = new EmbeddedHTTPService(); | ||
sandbox = sinon.sandbox.create(); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
describe('#constructor', () => { | ||
it('should create an instance', () => { | ||
httpService = new EmbeddedHTTPService(); | ||
}); | ||
}); | ||
|
||
describe('#post', () => { | ||
it('should return JS Object', () => { | ||
sandbox.stub(request, 'post').yields(null, {statusCode : 200}, {sum: 3}); | ||
const data = {foo : 'bar'}; | ||
httpService.post( 'url', data); | ||
return expect(httpService.post('url', data)).to.eventually.have.property('body'); | ||
}); | ||
|
||
it('should include error if present in JS Object', () => { | ||
sandbox.stub(request, 'post').yields('error', {statusCode : 200}, {sum: 3}); | ||
const data = {foo : 'bar'}; | ||
httpService.post( 'url', data); | ||
return expect(httpService.post('url', data)).to.eventually.deep.equal({statusCode : 200, body : 'error'}); | ||
}); | ||
|
||
it('should reject if there is no response', () => { | ||
sandbox.stub(request, 'post').yields(null, null, {sum: 3}); | ||
const data = {foo : 'bar'}; | ||
httpService.post( 'url', data); | ||
return expect(httpService.post('url', data)).to.eventually.be.rejected; | ||
}); | ||
|
||
it('should reject if there is no response and return error', () => { | ||
sandbox.stub(request, 'post').yields('error', null, {sum: 3}); | ||
const data = {foo : 'bar'}; | ||
httpService.post( 'url', data); | ||
return expect(httpService.post('url', data)).to.eventually.be.rejected; | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.