Skip to content

Commit

Permalink
Added support for HTTP POST from Transaction Processor functions (hyp…
Browse files Browse the repository at this point in the history
…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
dselman authored May 24, 2017
1 parent 55c733e commit 4f3f89b
Show file tree
Hide file tree
Showing 36 changed files with 1,720 additions and 24 deletions.
1 change: 1 addition & 0 deletions packages/composer-common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@ module.exports.TypescriptVisitor = require('./lib/codegen/fromcto/typescript/typ
module.exports.Util = require('./lib/util');
module.exports.Wallet = require('./lib/wallet');
module.exports.CodeGen = require('./lib/codegen/codegen.js');
module.exports.Writer = require('./lib/codegen/writer.js');
module.exports.version = require('./package.json');
2 changes: 1 addition & 1 deletion packages/composer-common/lib/codegen/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

'use strict';


module.exports.GoLangVisitor = require('./fromcto/golang/golangvisitor');
module.exports.JSONSchemaVisitor = require('./fromcto/jsonschema/jsonschemavisitor');
module.exports.PlantUMLVisitor = require('./fromcto/plantuml/plantumlvisitor');
module.exports.TypescriptVisitor = require('./fromcto/typescript/typescriptvisitor');
module.exports.FileWriter = require('./filewriter');

2 changes: 1 addition & 1 deletion packages/composer-common/lib/codegen/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class Writer {

/**
* Create a FileWriter.
* Create a Writer.
*
*/
constructor() {
Expand Down
3 changes: 2 additions & 1 deletion packages/composer-playground/config/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ module.exports = function (options) {
'path': 'browserfs/dist/shims/path.js',
'processGlobal': 'browserfs/dist/shims/process.js',
'bufferGlobal': 'browserfs/dist/shims/bufferGlobal.js',
'bfsGlobal': require.resolve('browserfs')
'bfsGlobal': require.resolve('browserfs'),
"request$": "xhr" // used for HTTP POST
}

},
Expand Down
14 changes: 7 additions & 7 deletions packages/composer-playground/src/app/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { ConnectionProfileComponent } from './connection-profile/connection-prof
import { IdentityComponent } from './identity';

export const ROUTES: Routes = [
{path: 'editor', component: EditorComponent},
{path: 'test', component: TestComponent},
{path: 'identity', component: IdentityComponent},
{path: 'profile', component: ConnectionProfileComponent},
{path: 'github', component: GithubComponent},
{path: '', redirectTo: 'editor', pathMatch: 'full'},
{path: '**', component: NoContentComponent}
{path: 'editor', component: EditorComponent},
{path: 'test', component: TestComponent},
{path: 'identity', component: IdentityComponent},
{path: 'profile', component: ConnectionProfileComponent},
{path: 'github', component: GithubComponent},
{path: '', redirectTo: 'editor', pathMatch: 'full'},
{path: '**', component: NoContentComponent}
];
1 change: 0 additions & 1 deletion packages/composer-playground/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title><%= htmlWebpackPlugin.options.title %></title>

<meta name="description" content="<%= htmlWebpackPlugin.options.title %>">
Expand Down
1 change: 1 addition & 0 deletions packages/composer-runtime-embedded/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports.EmbeddedContainer = require('./lib/embeddedcontainer');
module.exports.EmbeddedContext = require('./lib/embeddedcontext');
module.exports.EmbeddedDataCollection = require('./lib/embeddeddatacollection');
module.exports.EmbeddedDataService = require('./lib/embeddeddataservice');
module.exports.EmbeddedHTTPService = require('./lib/embeddedhttpservice');
module.exports.EmbeddedEventService = require('./lib/embeddedeventservice');
module.exports.EmbeddedIdentityService = require('./lib/embeddedidentityservice');
module.exports.EmbeddedLoggingService = require('./lib/embeddedloggingservice');
12 changes: 12 additions & 0 deletions packages/composer-runtime-embedded/lib/embeddedcontext.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
const Context = require('composer-runtime').Context;
const EmbeddedIdentityService = require('./embeddedidentityservice');
const EmbeddedEventService = require('./embeddedeventservice');
const EmbeddedHTTPService = require('./embeddedhttpservice');

/**
* A class representing the current request being handled by the JavaScript engine.
Expand Down Expand Up @@ -64,6 +65,17 @@ class EmbeddedContext extends Context {
}
return this.eventService;
}

/**
* Get the event service provided by the chaincode container.
* @return {EventService} The event service provided by the chaincode container.
*/
getHTTPService() {
if (!this.httpService) {
this.httpService = new EmbeddedHTTPService();
}
return this.httpService;
}
}

module.exports = EmbeddedContext;
78 changes: 78 additions & 0 deletions packages/composer-runtime-embedded/lib/embeddedhttpservice.js
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;
1 change: 1 addition & 0 deletions packages/composer-runtime-embedded/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"debug": "^2.6.2",
"dexie": "^1.5.1",
"fake-indexeddb": "^1.0.8",
"request": "^2.81.0",
"uuid": "^3.0.1"
},
"nyc": {
Expand Down
15 changes: 15 additions & 0 deletions packages/composer-runtime-embedded/test/embeddedcontext.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const EmbeddedContainer = require('..').EmbeddedContainer;
const EmbeddedContext = require('..').EmbeddedContext;
const IdentityService = require('composer-runtime').IdentityService;
const EventService = require('composer-runtime').EventService;
const HTTPService = require('composer-runtime').HTTPService;

require('chai').should();
const sinon = require('sinon');
Expand Down Expand Up @@ -85,4 +86,18 @@ describe('EmbeddedContext', () => {
});
});

describe('#getHTTPService', () => {

it('should return the container http service', () => {
let context = new EmbeddedContext(mockEngine, 'bob1');
context.getHTTPService().should.be.an.instanceOf(HTTPService);
});

it('should return this.httpService if it is set', () => {
let context = new EmbeddedContext(mockEngine, 'bob1');
context.httpService = {};
context.getHTTPService().should.deep.equal({});
});
});

});
76 changes: 76 additions & 0 deletions packages/composer-runtime-embedded/test/embeddedhttpservice.js
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;
});
});
});
12 changes: 12 additions & 0 deletions packages/composer-runtime-hlf/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Context struct {
DataService *DataService
IdentityService *IdentityService
EventService *EventService
HTTPService *HTTPService
}

// NewContext creates a Go wrapper around a new instance of the Context JavaScript class.
Expand Down Expand Up @@ -54,11 +55,14 @@ func NewContext(vm *otto.Otto, engine *Engine, stub shim.ChaincodeStubInterface)
result.DataService = NewDataService(vm, result, stub)
result.IdentityService = NewIdentityService(vm, result, stub)
result.EventService = NewEventService(vm, result, stub)
result.HTTPService = NewHTTPService(vm, result, stub)

// Bind the methods into the JavaScript object.
result.This.Set("getDataService", result.getDataService)
result.This.Set("getIdentityService", result.getIdentityService)
result.This.Set("getEventService", result.getEventService)
result.This.Set("getHTTPService", result.getHTTPService)

return result

}
Expand Down Expand Up @@ -86,3 +90,11 @@ func (context *Context) getEventService(call otto.FunctionCall) (result otto.Val

return context.EventService.This.Value()
}

// getHTTPService ...
func (context *Context) getHTTPService(call otto.FunctionCall) (result otto.Value) {
logger.Debug("Entering Context.getHTTPService", call)
defer func() { logger.Debug("Exiting Context.getHTTPService", result) }()

return context.HTTPService.This.Value()
}
Loading

0 comments on commit 4f3f89b

Please sign in to comment.