package client import ( "encoding/json" "fmt" "gitea.pena/PenaSide/codeword/internal/models" "github.com/gofiber/fiber/v2" "go.uber.org/zap" ) type AuthClientDeps struct { AuthUrl string Logger *zap.Logger } type AuthClient struct { authUrl string fiberClient *fiber.Client logger *zap.Logger } func NewAuthClient(deps AuthClientDeps) *AuthClient { return &AuthClient{ authUrl: deps.AuthUrl, fiberClient: fiber.AcquireClient(), logger: deps.Logger, } } func (a *AuthClient) RefreshAuthToken(userID, signature string) (*models.RefreshResponse, error) { body := models.AuthRequestBody{ UserID: userID, Signature: signature, } bodyBytes, err := json.Marshal(body) if err != nil { a.logger.Error("Failed to encode request body", zap.Error(err)) return nil, err } agent := a.fiberClient.Post(fmt.Sprintf("%s/auth/exchange", a.authUrl)) agent.Set("Content-Type", "application/json").Body(bodyBytes) statusCode, resBody, errs := agent.Bytes() if len(errs) > 0 { for _, err := range errs { a.logger.Error("Error in exchange auth token request", zap.Error(err)) } return nil, fmt.Errorf("request failed: %v", errs) } if statusCode != fiber.StatusOK { errorMessage := fmt.Sprintf("received an incorrect response from the authentication service: %d", statusCode) a.logger.Error(errorMessage, zap.Int("status", statusCode), zap.String("respBody", string(resBody)), zap.String("UserID", userID), zap.String("sign", signature)) return nil, fmt.Errorf(errorMessage) } var tokens models.RefreshResponse if err := json.Unmarshal(resBody, &tokens); err != nil { a.logger.Error("failed to unmarshal auth service response", zap.Error(err)) return nil, err } return &tokens, nil }