-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_test.go
67 lines (55 loc) · 1.61 KB
/
session_test.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
package sdk
import (
"context"
"fmt"
"github.com/cosmos/gogoproto/grpc"
"github.com/pokt-network/poktroll/x/shared/types"
)
func ExampleSessionClient() {
// Initialize the SessionClient.
var grpcConn grpc.ClientConn
// setup the grpc connection
// ...
sc := SessionClient{
// Use the default implementation of the PoktNodeSessionFetcher interface.
PoktNodeSessionFetcher: NewPoktNodeSessionFetcher(grpcConn),
}
// Get the session.
session, err := sc.GetSession(context.Background(), "appId", "serviceId", 1)
if err != nil {
fmt.Printf("Erorr fetching session: %v\n", err)
return
}
// Get all the endpoints for the session.
sessionFilter := SessionFilter{
Session: session,
EndpointFilters: []EndpointFilter{},
}
serviceEndpoints, err := sessionFilter.AllEndpoints()
if err != nil {
fmt.Printf("Error getting service endpoints: %v\n", err)
return
}
for supplierId, endpoints := range serviceEndpoints {
for _, e := range endpoints {
fmt.Printf("Supplier: %s, Endpoint URL: %s\n", supplierId, e.Endpoint().Url)
}
}
// Use a filter to get only the endpoints that satisfy the filter.
filterEndpoints := func(e Endpoint) bool {
return e.Endpoint().RpcType == types.RPCType_JSON_RPC
}
sessionFilter.EndpointFilters = append(sessionFilter.EndpointFilters, filterEndpoints)
filteredEndpoints, err := sessionFilter.FilteredEndpoints()
if err != nil {
fmt.Printf("Error filtering service endpoints: %v\n", err)
return
}
for _, endpoint := range filteredEndpoints {
fmt.Printf(
"Supplier: %s, Endpoint URL: %s\n",
endpoint.Supplier(),
endpoint.Endpoint().Url,
)
}
}