feat: added API gateway
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package grpc_client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
pb "datarush/pkg/api/results"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type ResultsClient struct {
|
||||
client pb.ResultsServiceClient
|
||||
conn *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewResultsClient(ctx context.Context, address string, factory *ClientFactory) (*ResultsClient, error) {
|
||||
conn, err := factory.GetConnection(ctx, address)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create results client: %w", err)
|
||||
}
|
||||
return &ResultsClient{client: pb.NewResultsServiceClient(conn), conn: conn}, nil
|
||||
}
|
||||
|
||||
func (c *ResultsClient) GetCompetitionResults(
|
||||
ctx context.Context,
|
||||
req *pb.GetCompetitionResultsRequest,
|
||||
) (*pb.GetCompetitionResultsResponse, error) {
|
||||
resp, err := c.client.GetCompetitionResults(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get competition results failed: %w", err)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *ResultsClient) GetUserCompetitionResults(
|
||||
ctx context.Context,
|
||||
competitionID, userID string,
|
||||
) (*pb.UserResult, error) {
|
||||
req := &pb.GetUserCompetitionResultsRequest{
|
||||
CompetitionId: competitionID,
|
||||
UserId: userID,
|
||||
}
|
||||
resp, err := c.client.GetUserCompetitionResults(ctx, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get user competition results failed: %w", err)
|
||||
}
|
||||
return resp.Result, nil
|
||||
}
|
||||
|
||||
func (c *ResultsClient) RecalculateResults(ctx context.Context, competitionID string) error {
|
||||
req := &pb.RecalculateResultsRequest{CompetitionId: competitionID}
|
||||
_, err := c.client.RecalculateResults(ctx, req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("recalculate results failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user