-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrpc_client.go
36 lines (32 loc) · 1.05 KB
/
grpc_client.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
package main
import (
"context"
"errors"
"os"
"time"
pb "dangerous.tech/streamdl/protos"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
)
func getStream(site string, user string, quality string) (string, error) {
conn, err := grpc.Dial(os.Getenv("STREAMDL_GRPC_ADDR")+":"+os.Getenv("STREAMDL_GRPC_PORT"), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
if err != nil {
log.Fatalf("gRPC Failed to Connect: %v", err)
}
defer conn.Close()
c := pb.NewStreamClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(time.Second)*time.Millisecond)
defer cancel()
msg, err := c.GetStream(ctx, &pb.StreamInfo{Site: site, User: user, Quality: quality})
if err != nil {
if e, ok := status.FromError(err); ok {
log.Debugf("Failed to Get Stream for %v: %v", user, e.Code())
return "", errors.New("failed to get stream")
}
} else {
log.Tracef("Stream for %v Fetched: %v", user, msg.Url)
}
return msg.Url, nil
}