-
Notifications
You must be signed in to change notification settings - Fork 1
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 #52 from RTradeLtd/ex
Examples
- Loading branch information
Showing
30 changed files
with
14,854 additions
and
21 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
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
|
||
pb "github.com/RTradeLtd/TxPB/v3/go" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
var ( | ||
xAPI = "xapi.temporal.cloud:9090" | ||
) | ||
|
||
func main() { | ||
conn, err := grpc.Dial(xAPI, grpc.WithInsecure()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
nc := pb.NewNodeAPIClient(conn) | ||
fc := pb.NewFileAPIClient(conn) | ||
resp, err := nc.Dag(context.Background(), &pb.DagRequest{ | ||
RequestType: pb.DAGREQTYPE_DAG_PUT, | ||
Data: []byte("hello world this is test data"), | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Printf("dag put hash: %s\n", resp.GetHashes()[0]) | ||
hash := uploadFile(fc) | ||
fmt.Printf("upload file hash: %s\n", hash) | ||
resp, err = nc.Dag(context.Background(), &pb.DagRequest{ | ||
RequestType: pb.DAGREQTYPE_DAG_GET_LINKS, | ||
Hash: hash, | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Printf("number of links: %v\n", len(resp.GetLinks())) | ||
} | ||
|
||
func uploadFile(nc pb.FileAPIClient) string { | ||
defer os.Remove("justsometestdata") | ||
if err := ioutil.WriteFile("justsometestdata", []byte("hello world"), os.FileMode(0640)); err != nil { | ||
log.Fatal(err) | ||
} | ||
stream, err := nc.UploadFile(context.Background()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
file, err := os.Open("justsometestdata") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
// declare file options | ||
if err := stream.Send(&pb.UploadRequest{Options: &pb.UploadOptions{MultiHash: "sha2-256", Chunker: "size-1"}}); err != nil { | ||
log.Fatal(err) | ||
} | ||
// upload file - chunked at 5mb each | ||
buf := make([]byte, 4194294) | ||
for { | ||
n, err := file.Read(buf) | ||
if err != nil && err == io.EOF { | ||
// only break if we haven't read any bytes, otherwise exit | ||
if n == 0 { | ||
break | ||
} | ||
} else if err != nil && err != io.EOF { | ||
log.Fatal(err) | ||
} | ||
if err := stream.Send(&pb.UploadRequest{Blob: &pb.Blob{Content: buf[:n]}}); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
resp, err := stream.CloseAndRecv() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return resp.GetHash() | ||
} |
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,10 @@ | ||
# js | ||
|
||
To run these examples you'll need to install the following packages: | ||
|
||
```shell | ||
$> npm install grpc | ||
$> npm install google-protobuf | ||
``` | ||
|
||
To actually run you can do `node index.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,33 @@ | ||
var services = require('../../js/node_grpc_pb'); | ||
var messages = require('../../js/node_pb'); | ||
var grpc = require('grpc'); | ||
|
||
function main() { | ||
var client = new services.NodeAPIClient( | ||
'xapi.temporal.cloud:9090', | ||
grpc.credentials.createInsecure() | ||
); | ||
var request = new messages.DagRequest() | ||
request.RequestType = messages.DAG_PUT; | ||
request.Data = "hello world"; | ||
client.dag(request, function(err, response) { | ||
if (err) { | ||
console.error("failed to put dag", err); | ||
return; | ||
} | ||
response.array.forEach(element => { | ||
if (element === undefined) { | ||
return | ||
} | ||
element.forEach(ele => { | ||
if (ele === undefined) { | ||
return | ||
} | ||
console.log("hash " + ele); | ||
}) | ||
}); | ||
}) | ||
|
||
} | ||
|
||
main(); |
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,10 @@ | ||
# Python Example | ||
|
||
To run this you will want to make sure the appropriate files are in the directory: | ||
|
||
```shell | ||
$> cp ../../py/util_pb2.py . | ||
$> cp ../../py/node_pb2* . | ||
``` | ||
|
||
After which you can run the example with `python3 main.py` (assuming you have the appropriate grpc dependencies installed) |
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,9 @@ | ||
import grpc | ||
import node_pb2_grpc | ||
import node_pb2 | ||
|
||
channel = grpc.insecure_channel('xapi.temporal.cloud:9090') | ||
s = node_pb2_grpc.NodeAPIStub(channel) | ||
resp = s.Dag(node_pb2.DagRequest(requestType=node_pb2.DAG_PUT, data=bytes(0))) | ||
|
||
print(resp) |
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,19 @@ | ||
# ts | ||
|
||
To run this you'll need to install the following dependencies | ||
|
||
```shell | ||
$> npm install grpc | ||
$> npm install google-protobuf | ||
$> npm install @improbable-eng/grpc-web | ||
$> npm install -g typescript | ||
``` | ||
|
||
To compile typescript to javascript run `tsc index.ts` and you can then run the example with `node index.js` | ||
|
||
On some machines the above may cause an error and I'm not exactly sure why. To fix this you can change the `messages` and `services` import to the following: | ||
|
||
``` | ||
var messages = require("../../js/node_pb"); | ||
var services = require("../../js/node_grpc_pb"); | ||
``` |
Oops, something went wrong.