|
| 1 | +// client/main.go |
| 2 | +package main |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "log" |
| 8 | + |
| 9 | + "golang.org/x/net/context" |
| 10 | + "google.golang.org/grpc" |
| 11 | + |
| 12 | + pb "./customer" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + address = "localhost:50051" |
| 17 | +) |
| 18 | + |
| 19 | +// createCustomer calls the RPC method CreateCustomer of CustomerServer |
| 20 | +func createCustomer(client pb.CustomerClient, customer *pb.CustomerRequest) { |
| 21 | + resp, err := client.CreateCustomer(context.Background(), customer) |
| 22 | + if err != nil { |
| 23 | + log.Fatalf("Could not create Customer: %v", err) |
| 24 | + } |
| 25 | + if resp.Success { |
| 26 | + log.Printf("A new Customer has been added with id: %d", resp.Id) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// GetCustomers calls the RPC method GetCustomers of CustomerServer |
| 31 | +func getCustomers(client pb.CustomerClient, filter *pb.CustomerFilter) { |
| 32 | + // calling the streaming API |
| 33 | + stream, err := client.GetCustomers(context.Background(), filter) |
| 34 | + if err != nil { |
| 35 | + log.Fatal("Error on get customers: %v", err) |
| 36 | + } |
| 37 | + for { |
| 38 | + customer, err := stream.Recv() |
| 39 | + if err == io.EOF { |
| 40 | + break |
| 41 | + } |
| 42 | + |
| 43 | + if err != nil { |
| 44 | + log.Fatal("%v.GetCustomers(_) = _, %v", client, err) |
| 45 | + } |
| 46 | + log.Printf("Customer: %v", customer) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func main() { |
| 51 | + // Set up a connection to the RPC server |
| 52 | + conn, err := grpc.Dial(address, grpc.WithInsecure()) |
| 53 | + if err != nil { |
| 54 | + log.Fatal("did not connect: %v", err) |
| 55 | + } |
| 56 | + defer conn.Close() |
| 57 | + // creates a new CustomerClient |
| 58 | + client := pb.NewCustomerClient(conn) |
| 59 | + |
| 60 | + customer := &pb.CustomerRequest{ |
| 61 | + Id: 101, |
| 62 | + Name: "Shiju Varghese", |
| 63 | + |
| 64 | + Phone: "732-757-2923", |
| 65 | + Addresses: []*pb.CustomerRequest_Address{ |
| 66 | + &pb.CustomerRequest_Address{ |
| 67 | + Street: "1 Mission Street", |
| 68 | + City: "San Francisco", |
| 69 | + State: "CA", |
| 70 | + Zip: "94105", |
| 71 | + IsShippingAddress: false, |
| 72 | + }, |
| 73 | + &pb.CustomerRequest_Address{ |
| 74 | + Street: "Greenfield", |
| 75 | + City: "Kochi", |
| 76 | + State: "KL", |
| 77 | + Zip: "68356", |
| 78 | + IsShippingAddress: true, |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + // Create a new customer |
| 84 | + createCustomer(client, customer) |
| 85 | + |
| 86 | + customer = &pb.CustomerRequest{ |
| 87 | + Id: 102, |
| 88 | + Name: "Irene Rose", |
| 89 | + |
| 90 | + Phone: "732-757-2924", |
| 91 | + Addresses: []*pb.CustomerRequest_Address{ |
| 92 | + &pb.CustomerRequest_Address{ |
| 93 | + Street: "1 Mission Street", |
| 94 | + City: "San Francisco", |
| 95 | + State: "CA", |
| 96 | + Zip: "94105", |
| 97 | + IsShippingAddress: true, |
| 98 | + }, |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + // Create a new customer |
| 103 | + createCustomer(client, customer) |
| 104 | + //Filter with an empty Keyword |
| 105 | + filter := &pb.CustomerFilter{Keyword: ""} |
| 106 | + |
| 107 | + fmt.Println("=====") |
| 108 | + getCustomers(client, filter) |
| 109 | + |
| 110 | +} |
0 commit comments