mirror of
https://github.com/lorsanstand/HomeOps-Hub.git
synced 2026-06-19 16:45:15 +03:00
0c2fdfee2a
Create connection manager for stream
41 lines
1.0 KiB
Go
41 lines
1.0 KiB
Go
package rpc
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
pb "github.com/lorsanstand/HomeOps-Hub/api/gen/homeops"
|
|
"github.com/lorsanstand/HomeOps-Hub/shared/domain"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
type Connection struct {
|
|
hub pb.HubClient
|
|
conn *grpc.ClientConn
|
|
}
|
|
|
|
func NewConnectAgent(conn *grpc.ClientConn) *Connection {
|
|
client := pb.NewHubClient(conn)
|
|
return &Connection{hub: client, conn: conn}
|
|
}
|
|
|
|
func (c *Connection) Close() error {
|
|
return c.conn.Close()
|
|
}
|
|
|
|
func (c *Connection) Hub() pb.HubClient {
|
|
return c.hub
|
|
}
|
|
|
|
func (c *Connection) RegisterAgent(ctx context.Context, RegisterData domain.RegisterAgentRequest) (domain.RegisterAgentResponse, error) {
|
|
ResponseData, err := c.Hub().RegisterAgent(ctx, new(domain.ToGRPCAgentRequest(RegisterData)))
|
|
if err != nil {
|
|
return domain.RegisterAgentResponse{}, fmt.Errorf("send register agent: %w", err)
|
|
}
|
|
response, err := domain.ToDomainAgentResponse(ResponseData)
|
|
if err != nil {
|
|
return domain.RegisterAgentResponse{}, fmt.Errorf("casting response: %w", err)
|
|
}
|
|
return response, nil
|
|
}
|