Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit 4f3f89b

Browse files
authored
Added support for HTTP POST from Transaction Processor functions (#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
1 parent 55c733e commit 4f3f89b

File tree

36 files changed

+1720
-24
lines changed

36 files changed

+1720
-24
lines changed

packages/composer-common/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,5 @@ module.exports.TypescriptVisitor = require('./lib/codegen/fromcto/typescript/typ
8989
module.exports.Util = require('./lib/util');
9090
module.exports.Wallet = require('./lib/wallet');
9191
module.exports.CodeGen = require('./lib/codegen/codegen.js');
92+
module.exports.Writer = require('./lib/codegen/writer.js');
9293
module.exports.version = require('./package.json');

packages/composer-common/lib/codegen/codegen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
'use strict';
1616

17-
1817
module.exports.GoLangVisitor = require('./fromcto/golang/golangvisitor');
1918
module.exports.JSONSchemaVisitor = require('./fromcto/jsonschema/jsonschemavisitor');
2019
module.exports.PlantUMLVisitor = require('./fromcto/plantuml/plantumlvisitor');
2120
module.exports.TypescriptVisitor = require('./fromcto/typescript/typescriptvisitor');
2221
module.exports.FileWriter = require('./filewriter');
22+

packages/composer-common/lib/codegen/writer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class Writer {
2525

2626
/**
27-
* Create a FileWriter.
27+
* Create a Writer.
2828
*
2929
*/
3030
constructor() {

packages/composer-playground/config/webpack.common.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ module.exports = function (options) {
8585
'path': 'browserfs/dist/shims/path.js',
8686
'processGlobal': 'browserfs/dist/shims/process.js',
8787
'bufferGlobal': 'browserfs/dist/shims/bufferGlobal.js',
88-
'bfsGlobal': require.resolve('browserfs')
88+
'bfsGlobal': require.resolve('browserfs'),
89+
"request$": "xhr" // used for HTTP POST
8990
}
9091

9192
},

packages/composer-playground/src/app/app.routes.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import { ConnectionProfileComponent } from './connection-profile/connection-prof
88
import { IdentityComponent } from './identity';
99

1010
export const ROUTES: Routes = [
11-
{path: 'editor', component: EditorComponent},
12-
{path: 'test', component: TestComponent},
13-
{path: 'identity', component: IdentityComponent},
14-
{path: 'profile', component: ConnectionProfileComponent},
15-
{path: 'github', component: GithubComponent},
16-
{path: '', redirectTo: 'editor', pathMatch: 'full'},
17-
{path: '**', component: NoContentComponent}
11+
{path: 'editor', component: EditorComponent},
12+
{path: 'test', component: TestComponent},
13+
{path: 'identity', component: IdentityComponent},
14+
{path: 'profile', component: ConnectionProfileComponent},
15+
{path: 'github', component: GithubComponent},
16+
{path: '', redirectTo: 'editor', pathMatch: 'full'},
17+
{path: '**', component: NoContentComponent}
1818
];

packages/composer-playground/src/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<meta charset="utf-8">
55
<meta http-equiv="x-ua-compatible" content="ie=edge">
66
<meta name="viewport" content="width=device-width, initial-scale=1">
7-
87
<title><%= htmlWebpackPlugin.options.title %></title>
98

109
<meta name="description" content="<%= htmlWebpackPlugin.options.title %>">

packages/composer-runtime-embedded/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports.EmbeddedContainer = require('./lib/embeddedcontainer');
1818
module.exports.EmbeddedContext = require('./lib/embeddedcontext');
1919
module.exports.EmbeddedDataCollection = require('./lib/embeddeddatacollection');
2020
module.exports.EmbeddedDataService = require('./lib/embeddeddataservice');
21+
module.exports.EmbeddedHTTPService = require('./lib/embeddedhttpservice');
2122
module.exports.EmbeddedEventService = require('./lib/embeddedeventservice');
2223
module.exports.EmbeddedIdentityService = require('./lib/embeddedidentityservice');
2324
module.exports.EmbeddedLoggingService = require('./lib/embeddedloggingservice');

packages/composer-runtime-embedded/lib/embeddedcontext.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
const Context = require('composer-runtime').Context;
1818
const EmbeddedIdentityService = require('./embeddedidentityservice');
1919
const EmbeddedEventService = require('./embeddedeventservice');
20+
const EmbeddedHTTPService = require('./embeddedhttpservice');
2021

2122
/**
2223
* A class representing the current request being handled by the JavaScript engine.
@@ -64,6 +65,17 @@ class EmbeddedContext extends Context {
6465
}
6566
return this.eventService;
6667
}
68+
69+
/**
70+
* Get the event service provided by the chaincode container.
71+
* @return {EventService} The event service provided by the chaincode container.
72+
*/
73+
getHTTPService() {
74+
if (!this.httpService) {
75+
this.httpService = new EmbeddedHTTPService();
76+
}
77+
return this.httpService;
78+
}
6779
}
6880

6981
module.exports = EmbeddedContext;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
'use strict';
16+
17+
const HTTPService = require('composer-runtime').HTTPService;
18+
const Logger = require('composer-common').Logger;
19+
const request = require('request');
20+
const LOG = Logger.getLog('EmbeddedHTTPService');
21+
22+
/**
23+
* Base class representing the http service provided by a {@link Container}.
24+
* @protected
25+
*/
26+
class EmbeddedHTTPService extends HTTPService {
27+
28+
/**
29+
* Constructor.
30+
* @param {EventEmitter} eventSink the event emitter
31+
*/
32+
constructor() {
33+
super();
34+
const method = 'constructor';
35+
LOG.exit(method);
36+
}
37+
38+
/**
39+
* Post data
40+
* @abstract
41+
* @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.
42+
* @throws {Error} throws an error if there is an issue
43+
*/
44+
_post() {
45+
const self = this;
46+
return new Promise(function (resolve, reject) {
47+
48+
const options = {
49+
url : self.url,
50+
method: 'POST',
51+
body: self.data,
52+
json: true
53+
};
54+
55+
request.post(options,
56+
function (err, resp, body) {
57+
LOG.info('error:', err);
58+
LOG.info('statusCode:', resp && resp.statusCode);
59+
LOG.info('body:', body);
60+
61+
if(resp) {
62+
resolve( JSON.stringify({
63+
statusCode: resp.statusCode,
64+
body: (err) ? err : body
65+
}));
66+
}
67+
else {
68+
reject( JSON.stringify({
69+
statusCode: 500,
70+
body: (err) ? err : body
71+
}));
72+
}
73+
});
74+
});
75+
}
76+
}
77+
78+
module.exports = EmbeddedHTTPService;

packages/composer-runtime-embedded/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"debug": "^2.6.2",
5858
"dexie": "^1.5.1",
5959
"fake-indexeddb": "^1.0.8",
60+
"request": "^2.81.0",
6061
"uuid": "^3.0.1"
6162
},
6263
"nyc": {

0 commit comments

Comments
 (0)