feat: added API gateway
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package grpc_client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
pb "datarush/pkg/api/user"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type UserClient struct {
|
||||
client pb.UserServiceClient
|
||||
conn *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewUserClient(ctx context.Context, address string, factory *ClientFactory) (*UserClient, error) {
|
||||
conn, err := factory.GetConnection(ctx, address)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create user client: %w", err)
|
||||
}
|
||||
|
||||
return &UserClient{
|
||||
client: pb.NewUserServiceClient(conn),
|
||||
conn: conn,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *UserClient) GetProfile(ctx context.Context, userID string) (*pb.User, error) {
|
||||
req := &pb.GetProfileRequest{
|
||||
UserId: userID,
|
||||
}
|
||||
|
||||
resp, err := c.client.GetProfile(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get profile failed: %w", err)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *UserClient) RegisterForCompetition(ctx context.Context, competitionID string) error {
|
||||
req := &pb.RegisterForCompetitionRequest{
|
||||
CompetitionId: competitionID,
|
||||
}
|
||||
|
||||
_, err := c.client.RegisterForCompetition(ctx, req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("register for competition failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *UserClient) UnregisterFromCompetition(ctx context.Context, competitionID string) error {
|
||||
req := &pb.UnregisterFromCompetitionRequest{
|
||||
CompetitionId: competitionID,
|
||||
}
|
||||
|
||||
_, err := c.client.UnregisterFromCompetition(ctx, req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unregister from competition failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *UserClient) ListUserCompetitions(ctx context.Context, userID string) ([]string, error) {
|
||||
req := &pb.ListUserCompetitionsRequest{
|
||||
UserId: userID,
|
||||
}
|
||||
|
||||
resp, err := c.client.ListUserCompetitions(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list user competitions failed: %w", err)
|
||||
}
|
||||
|
||||
return resp.CompetitionIds, nil
|
||||
}
|
||||
Reference in New Issue
Block a user