Skip to content

Commit

Permalink
Merge pull request #120 from angrbrd/angrbrd-doc-edits
Browse files Browse the repository at this point in the history
Angrbrd doc edits
  • Loading branch information
Mr. Angry committed Feb 20, 2016
2 parents df6762b + d15d6c8 commit c0fae27
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 77 deletions.
21 changes: 13 additions & 8 deletions api/Openchain API.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,52 +473,57 @@ For additional information on the Open Blockchain REST endpoints and more detail

## Node.js Application

You can interface to the obc-peer process from a Node.js application in one of two ways. Both approaches rely on the Swagger API description document, [rest_api.json](https://github.com/openblockchain/obc-peer/blob/master/openchain/rest/rest_api.json). Use the approach that you find the most convenient.
You can interface with the obc-peer process from a Node.js application in one of two ways. Both approaches rely on the Swagger API description document, [rest_api.json](https://github.com/openblockchain/obc-peer/blob/master/openchain/rest/rest_api.json). Use the approach that you find the most convenient.

### [OpenchainSample_1](https://github.com/openblockchain/obc-docs/blob/master/api/Openchain%20Samples/openchain_1.js)

* Demonstrates interfacing with a peer node from a Node.js app.
* Demonstrates interfacing with a peer node from a Node.js application.
* Utilizes the Node.js swagger-js plugin: https://github.com/swagger-api/swagger-js

**To run:**

1. Build and install [obc-peer](https://github.com/openblockchain/obc-peer/blob/master/README.md).

2. Run local peer node only (not complete network) with:
```
cd $GOPATH/src/github.com/openblockchain/obc-peer
go build
```

2. Run a local peer node only (not a complete Open Blockchain network) with:

`./obc-peer peer`

3. Set up a test blockchain data structure (with 5 blocks only) by running a test from within Vagrant as follows. Subsequently restart the peer process.
3. Set up a test blockchain data structure (with 5 blocks only) by running a test from within Vagrant as follows. Subsequently restart the peer process.

```
cd /opt/gopath/src/github.com/openblockchain/obc-peer
go test -v -run TestServerOpenchain_API_GetBlockCount github.com/openblockchain/obc-peer/openchain
```

4. Set up HTTP server to serve up the Open Blockchain API Swagger doc:
4. Set up a Node.js HTTP server to serve up the Open Blockchain API Swagger document:

```
npm install http-server -g
cd /opt/gopath/src/github.com/openblockchain/obc-peer/openchain/rest
http-server -a 0.0.0.0 -p 5554 --cors
```

5. Download [OpenchainSample_1.zip](https://github.com/openblockchain/obc-docs/blob/master/api/Openchain%20Samples/OpenchainSample_1.zip)
5. Download and unzip [OpenchainSample_1.zip](https://github.com/openblockchain/obc-docs/blob/master/api/Openchain%20Samples/OpenchainSample_1.zip)

```
unzip OpenchainSample_1.zip -d OpenchainSample_1
cd OpenchainSample_1
```

6. Update the api_url within [openchain.js](https://github.com/openblockchain/obc-docs/blob/master/api/Openchain%20Samples/openchain_1.js) to the appropriate URL if it is not already the default
6. Update the api_url variable within [openchain.js](https://github.com/openblockchain/obc-docs/blob/master/api/Openchain%20Samples/openchain_1.js) to the appropriate URL if it is not already the default

`var api_url = 'http://localhost:5554/rest_api.json';`

7. Run the Node.js app

`node ./openchain.js`

You will observe several responses on the console and the program will appear to hang for a few moments at the end. This is normal, as is it waiting for a build request for a Docker container to complete.
You will observe several responses on the console and the program will appear to hang for a few moments at the end. This is expected, as is it waiting for the invocation transaction to complete in order to then execute a query. You can take a look at the sample output of the program inside the 'openchain_test' file located inside OpenchainSample_1 directory.

### [OpenchainSample_2](https://github.com/openblockchain/obc-docs/blob/master/api/Openchain%20Samples/openchain_2.js)

Expand Down
Binary file modified api/Openchain Samples/OpenchainSample_1.zip
Binary file not shown.
169 changes: 100 additions & 69 deletions api/Openchain Samples/openchain_1.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,131 @@
// Initialize the Swagger JS plugin
var client = require('swagger-client');

// Point plugin to the location of the Swagger API descrition
// Point plug-in to the location of the Swagger API description
// Update this variable accordingly, depending on where you're hosing the API description
var api_url = 'http://localhost:5554/rest_api.json';

// Operations currently exposed through IBM Blockchain Swagger description:
//
// getChain
// getBlock
// getTransaction
// chaincodeDeploy
// chaincodeInvoke
// chaincodeQuery
// registerUser
// getUserRegistration
// deleteUserRegistration
// getUserEnrollmentCertificate
// getPeers
//

// Initialize the Swagger-based client, passing in the API URL
var swagger = new client({
url: api_url,
success: function() { // If connection to API established, sucess and proceed
success: function() {
// If connection to the API description is established, report success and
// proceed
console.log("Connected to REST API server!\n");

// GET /chain
// Run through some of the Swagger APIs
runSwaggerAPITest();
},
error: function() {
// If connection to the API description failed, then report error and exit
console.log("Failed to connect to REST API server.\n");
console.log("Exiting.\n");
}
});

// Sample script to trigger APIs exposed in Swagger through Node.js
function runSwaggerAPITest() {
console.log("Running Swagger API test...\n");

// GET /chain -- retrieve blockchain information
swagger.Blockchain.getChain({},{responseContentType: 'application/json'}, function(Blockchain){
console.log("----- Blockchain Retrieved: -----\n");
console.log(Blockchain);
console.log("----------\n\n");
});

// GET /chain/blocks/{Block} -- success
// GET /chain/blocks/0 -- retrieve block information for block 0
swagger.Block.getBlock({'Block': '0'},{responseContentType: 'application/json'}, function(Block){
console.log("----- Block Retrieved: -----\n");
console.log(Block);
console.log("----------\n\n");
});

// GET /chain/blocks/{Block} -- failure, block not in test blockchain
// GET /chain/blocks/5 -- retrieve block information for block 5
swagger.Block.getBlock({'Block': '5'},{responseContentType: 'application/json'}, function(Block){
console.log("----- Block Retrieved: -----\n");
console.log(Block);
console.log("----------\n\n");
});

// /state/{chaincodeID}/{key} -- success/match, chaincode with this key exists
swagger.State.getChaincodeState({'chaincodeID': 'MyContract', 'key': 'x'},{responseContentType: 'application/json'}, function(State){
console.log("----- State Retrieved: -----\n");
console.log(State);
console.log("----------\n\n");
});

// /state/{chaincodeID}/{key} -- success/no match, chaincode with this key not found
swagger.State.getChaincodeState({'chaincodeID': 'MyOtherContract', 'key': 'NONE'},{responseContentType: 'application/json'}, function(State){
console.log("----- State Retrieved: -----\n");
console.log(State);
console.log("----------\n\n");
});

// POST /devops/build -- success
var chaincodeSpec = {
type: "GOLANG",
chaincodeID: {
url: "github.com/openblockchain/obc-peer/openchain/example/chaincode/chaincode_example01",
version: "0.1.0"
}
};

// Print confirmation message only, as codePackage is too large to be printed
swagger.Devops.chaincodeBuild({'ChaincodeSpec': chaincodeSpec},{responseContentType: 'application/json'},function(Devops){
console.log("----- Devops Build Triggered: -----\n");
console.log(Devops);
console.log("----------\n\n");
});

// POST /devops/build -- failure, only GOLANG support for chainCode

// Compose the payload for chaincode deploy transaction
var chaincodeSpec = {
type: "NODE",
chaincodeID: {
url: "github.com/openblockchain/obc-peer/openchain/example/chaincode/chaincode_simple",
version: "0.1.0"
"type": "GOLANG",
"chaincodeID":{
"path":"github.com/openblockchain/obc-peer/openchain/example/chaincode/chaincode_example02"
},
"ctorMsg": {
"function":"init",
"args":["a", "100", "b", "200"]
}
};

// Print confirmation message only, as codePackage is too large to be printed
swagger.Devops.chaincodeBuild({'ChaincodeSpec': chaincodeSpec},{responseContentType: 'application/json'},function(Devops){
console.log("----- Devops Build Triggered: -----\n");

// POST /devops/deploy -- deploy the sample chaincode
// name (hash) returned is:
// bb540edfc1ee2ac0f5e2ec6000677f4cd1c6728046d5e32dede7fea11a42f86a6943b76a8f9154f4792032551ed320871ff7b7076047e4184292e01e3421889c
swagger.Devops.chaincodeDeploy({'ChaincodeSpec': chaincodeSpec},{responseContentType: 'application/json'}, function(Devops){
console.log("----- Chaincode Deployed: -----\n");
console.log(Devops);
console.log("----------\n\n");

// Compose a payload for chaincode invocation transaction
var chaincodeInvocationSpec = {
"chaincodeSpec": {
"type": "GOLANG",
"chaincodeID":{
"name":"bb540edfc1ee2ac0f5e2ec6000677f4cd1c6728046d5e32dede7fea11a42f86a6943b76a8f9154f4792032551ed320871ff7b7076047e4184292e01e3421889c"
},
"ctorMsg": {
"function":"invoke",
"args":["a", "b", "10"]
}
}
};

// POST /devops/invoke -- invoke the sample chaincode
swagger.Devops.chaincodeInvoke({'ChaincodeInvocationSpec': chaincodeInvocationSpec},{responseContentType: 'application/json'}, function(Devops){
console.log("----- Devops Invoke Triggered: -----\n");
console.log(Devops);
console.log("----------\n\n");

// Compose a payload for chaincode query transaction
chaincodeInvocationSpec = {
"chaincodeSpec": {
"type": "GOLANG",
"chaincodeID":{
"name":"bb540edfc1ee2ac0f5e2ec6000677f4cd1c6728046d5e32dede7fea11a42f86a6943b76a8f9154f4792032551ed320871ff7b7076047e4184292e01e3421889c"
},
"ctorMsg": {
"function":"query",
"args":["a"]
}
}
};

// POST /devops/query -- query the sample chaincode for variable "a"
// Must introduce a wait here, to insure that the invocation transaction had completed, as it returns immediately at runtime
setTimeout(function() {
swagger.Devops.chaincodeQuery({'ChaincodeInvocationSpec': chaincodeInvocationSpec},{responseContentType: 'application/json'}, function(Devops){
console.log("----- Devops Query Triggered: -----\n");
console.log(Devops);
console.log("----------\n\n");
})
}, 20000);
});
});

// POST /devops/build -- failure, chainCode path does not exist
var chaincodeSpec = {
type: "GOLANG",
chaincodeID: {
url: "/test/test/test",
version: "0.1.0"
}
};

// Print confirmation message only, as codePackage is too large to be printed
swagger.Devops.chaincodeBuild({'ChaincodeSpec': chaincodeSpec},{responseContentType: 'application/json'},function(Devops){
console.log("----- Devops Build Triggered: -----\n");
console.log(Devops);
console.log("----------\n\n");
});
},
error: function() { // If connection to API failed, error and exit
console.log("Failed to connect to REST API server.\n");
console.log("Exiting.\n");
}
});
}

0 comments on commit c0fae27

Please sign in to comment.