forked from jyellick/obc-docs
-
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.
Merge pull request #120 from angrbrd/angrbrd-doc-edits
Angrbrd doc edits
- Loading branch information
Showing
3 changed files
with
113 additions
and
77 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
Binary file not shown.
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 |
---|---|---|
@@ -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"); | ||
} | ||
}); | ||
} |