Skip to content

Instantly share code, notes, and snippets.

@gosharplite
Created October 7, 2016 09:27
Show Gist options
  • Save gosharplite/d87aceb9b59dceff0a21d8256990582c to your computer and use it in GitHub Desktop.
Save gosharplite/d87aceb9b59dceff0a21d8256990582c to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"golang.org/x/net/context"
"google.golang.org/grpc"
pb "grpcapi/protobuf/mongo"
"log"
"os"
)
func main() {
var (
server = flag.String("server", "localhost:50051", "target server")
)
flag.Parse()
conn, err := grpc.Dial(*server, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewMongoClient(conn)
r, err := echo(c)
if err != nil {
log.Fatalf("could not echo: %v", err)
}
log.Printf("echo: %s", string(r.Body))
res, err := documentInsertPost(c)
if err != nil {
log.Fatalf("could not documentInsertPost: %v", err)
}
log.Printf("documentInsertPost: %v", res)
res, err = documentReadGet(c)
if err != nil {
log.Fatalf("could not documentReadGet: %v", err)
}
log.Printf("documentReadGet: %v", res)
res, err = documentUpdatePut(c)
if err != nil {
log.Fatalf("could not documentUpdatePut: %v", err)
}
log.Printf("documentUpdatePut: %v", res)
res, err = documentUpdatePatch(c)
if err != nil {
log.Fatalf("could not documentUpdatePatch: %v", err)
}
log.Printf("documentUpdatePatch: %v", res)
res, err = documentDelete(c)
if err != nil {
log.Fatalf("could not documentDelete: %v", err)
}
log.Printf("documentDelete: %v", res)
}
func echo(c pb.MongoClient) (*pb.Content, error) {
args := os.Args
b := []byte("This is echo.")
if len(args) > 1 {
b = []byte(args[1])
}
r, err := c.Echo(context.Background(), &pb.Content{Body: b})
if err != nil {
return &pb.Content{}, err
}
return r, nil
}
func documentInsertPost(c pb.MongoClient) (*pb.Response, error) {
req := pb.Request{}
req.Method = "POST"
req.Uri = "http://10.128.112.27:30110"
req.Path = "/1/classes/DroiObjectApi"
req.AddHeaders("role", "56cea1d8c9922205b5bdfab7")
req.AddHeaders("X-Droi-AppID", "bf8umbzhigY-zYLSzOhEifAD91gTRXU2lQAAGsIK")
req.AddHeaders("X-Droi-ReqID", "1111111111111T1111111111")
req.AddHeaders("X-Droi-Platform-Key", "weE8UH-S60JxjiXY0wyXL_ok2-tCPsOVL_ok2-tC")
req.Body = []byte(`{"_Id":"56cea1d8c9922205b5bdfab6","MarketId":"oz0000","Sequence":0,"_ACL":{"ur":["56cea1d8c9922205b5bdfab7"],"uw":["56cea1d8c9922205b5bdfab7"],"gr":["56cea1d8c9922205b5bdfab6"],"gw":["56cea1d8c9922205b5bdfab6"]},"testArray":[1,2,3],"_ModifiedTime":"2012-12-01T14:35:00Z","_CreationTime":"2012-12-01T14:35:00Z","testbool":true,"testfloat":0.23,"testTimestamp":1468380409000,"pointer":{"_DataType":"_ReferenceType","_TableName":"cache","_Id":"56a1a5760cf6b0e875f1be5b"}}`)
r, err := c.DocumentCreateAndUpdate(context.Background(), &req)
if err != nil {
return &pb.Response{}, err
}
return r, nil
}
func documentReadGet(c pb.MongoClient) (*pb.Response, error) {
req := pb.Request{}
req.Method = "GET"
req.Uri = "http://10.128.112.27:30110"
req.Path = "/1/classes/DroiObjectApi/56cea1d8c9922205b5bdfab6"
req.AddHeaders("role", "56cea1d8c9922205b5bdfab7")
req.AddHeaders("X-Droi-AppID", "bf8umbzhigY-zYLSzOhEifAD91gTRXU2lQAAGsIK")
req.AddHeaders("X-Droi-ReqID", "1111111111111T1111111111")
req.AddHeaders("X-Droi-Platform-Key", "weE8UH-S60JxjiXY0wyXL_ok2-tCPsOVL_ok2-tC")
r, err := c.DocumentRead(context.Background(), &req)
if err != nil {
return &pb.Response{}, err
}
return r, nil
}
func documentUpdatePut(c pb.MongoClient) (*pb.Response, error) {
req := pb.Request{}
req.Method = "PUT"
req.Uri = "http://10.128.112.27:30110"
req.Path = "/1/classes/DroiObjectApi/56cea1d8c9922205b5bdfab6"
req.AddHeaders("role", "56cea1d8c9922205b5bdfab7")
req.AddHeaders("X-Droi-AppID", "bf8umbzhigY-zYLSzOhEifAD91gTRXU2lQAAGsIK")
req.AddHeaders("X-Droi-ReqID", "1111111111111T1111111111")
req.AddHeaders("X-Droi-Platform-Key", "weE8UH-S60JxjiXY0wyXL_ok2-tCPsOVL_ok2-tC")
req.Body = []byte(`{"MarketId":"oz0000","Sequence":0,"_ACL":{"ur":["56cea1d8c9922205b5bdfab7"],"uw":["56cea1d8c9922205b5bdfab7"],"gr":["56cea1d8c9922205b5bdfab6"],"gw":["56cea1d8c9922205b5bdfab6"]},"testArray":[1,2,3],"_ModifiedTime":"2012-12-01T14:35:00Z","_CreationTime":"2012-12-01T14:35:00Z","testbool":true,"testfloat":0.23,"testTimestamp":1468380409000,"pointer":{"_DataType":"_ReferenceType","_TableName":"cache","_Id":"56a1a5760cf6b0e875f1be5b"}}`)
r, err := c.DocumentCreateAndUpdate(context.Background(), &req)
if err != nil {
return &pb.Response{}, err
}
return r, nil
}
func documentUpdatePatch(c pb.MongoClient) (*pb.Response, error) {
req := pb.Request{}
req.Method = "PATCH"
req.Uri = "http://10.128.112.27:30110"
req.Path = "/1/classes/DroiObjectApi/56cea1d8c9922205b5bdfab6"
req.AddHeaders("role", "56cea1d8c9922205b5bdfab7")
req.AddHeaders("X-Droi-AppID", "bf8umbzhigY-zYLSzOhEifAD91gTRXU2lQAAGsIK")
req.AddHeaders("X-Droi-ReqID", "1111111111111T1111111111")
req.AddHeaders("X-Droi-Platform-Key", "weE8UH-S60JxjiXY0wyXL_ok2-tCPsOVL_ok2-tC")
req.Body = []byte(`{"Sequence":{"__op":"Delete"},"MarketId":"oz0010","test132":{"__op":"Increment","amount":10}}`)
r, err := c.DocumentCreateAndUpdate(context.Background(), &req)
if err != nil {
return &pb.Response{}, err
}
return r, nil
}
func documentDelete(c pb.MongoClient) (*pb.Response, error) {
req := pb.Request{}
req.Method = "DELETE"
req.Uri = "http://10.128.112.27:30110"
req.Path = "/1/classes/DroiObjectApi/56cea1d8c9922205b5bdfab6"
req.AddHeaders("role", "56cea1d8c9922205b5bdfab7")
req.AddHeaders("X-Droi-AppID", "bf8umbzhigY-zYLSzOhEifAD91gTRXU2lQAAGsIK")
req.AddHeaders("X-Droi-ReqID", "1111111111111T1111111111")
req.AddHeaders("X-Droi-Platform-Key", "weE8UH-S60JxjiXY0wyXL_ok2-tCPsOVL_ok2-tC")
r, err := c.DocumentDelete(context.Background(), &req)
if err != nil {
return &pb.Response{}, err
}
return r, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment