-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodjemalec.go
89 lines (75 loc) · 2.29 KB
/
odjemalec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Komunikacija po protokolu gRPC
// odjemalec
package main
import (
"context"
"fmt"
"primer/grpc/protobufStorage"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func Client(url string) {
// vzpostavimo povezavo s strežnikom
fmt.Printf("gRPC client connecting to %v\n", url)
conn, err := grpc.Dial(url, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
panic(err)
}
defer conn.Close()
// vzpostavimo izvajalno okolje
contextCRUD, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
// vzpostavimo vmesnik gRPC
grpcClient := protobufStorage.NewCRUDClient(conn)
// pripravimo strukture, ki jih uporabljamo kot argumente pri klicu oddaljenih metod
lecturesCreate := protobufStorage.Todo{Task: "predavanja", Completed: false}
lecturesUpdate := protobufStorage.Todo{Task: "predavanja", Completed: true}
practicals := protobufStorage.Todo{Task: "vaje", Completed: false}
readAll := protobufStorage.Todo{Task: "", Completed: false}
// ustvarimo zapis
fmt.Print("1. Create: ")
if _, err := grpcClient.Create(contextCRUD, &lecturesCreate); err != nil {
panic(err)
}
fmt.Println("done")
// preberemo en zapis
fmt.Print("2. Read 1: ")
if response, err := grpcClient.Read(contextCRUD, &lecturesCreate); err == nil {
fmt.Println(response.Todos, ": done")
} else {
panic(err)
}
// ustvarimo zapis
fmt.Print("3. Create: ")
if _, err := grpcClient.Create(contextCRUD, &practicals); err != nil {
panic(err)
}
fmt.Println("done")
// preberemo vse zapise
fmt.Print("4. Read *: ")
if response, err := grpcClient.Read(contextCRUD, &readAll); err == nil {
fmt.Println(response.Todos, ": done")
} else {
panic(err)
}
// posodobimo zapis
fmt.Print("5. Update: ")
if _, err := grpcClient.Update(contextCRUD, &lecturesUpdate); err != nil {
panic(err)
}
fmt.Println("done")
// izbrišemo zapis
fmt.Print("6. Delete: ")
if _, err := grpcClient.Delete(contextCRUD, &practicals); err != nil {
panic(err)
}
fmt.Println("done")
// preberemo vse zapise
fmt.Print("7. Read *: ")
if response, err := grpcClient.Read(contextCRUD, &readAll); err == nil {
fmt.Println(response.Todos, ": done")
} else {
panic(err)
}
}