2023-06-22 09:36:43 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/url"
|
|
|
|
|
2024-11-18 07:23:41 +00:00
|
|
|
"gitea.pena/PenaSide/customer/internal/errors"
|
|
|
|
"gitea.pena/PenaSide/customer/internal/models"
|
|
|
|
"gitea.pena/PenaSide/customer/internal/utils"
|
|
|
|
"gitea.pena/PenaSide/customer/pkg/client"
|
2024-11-26 15:28:42 +00:00
|
|
|
"go.uber.org/zap"
|
2023-06-22 09:36:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HubadminClientDeps struct {
|
|
|
|
Logger *zap.Logger
|
2024-11-26 15:28:42 +00:00
|
|
|
URL string
|
2023-06-22 09:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type HubadminClient struct {
|
|
|
|
logger *zap.Logger
|
2024-11-26 15:28:42 +00:00
|
|
|
url string
|
2023-06-22 09:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHubadminClient(deps HubadminClientDeps) *HubadminClient {
|
|
|
|
if deps.Logger == nil {
|
|
|
|
log.Panicln("logger is nil on <NewHubadminClient>")
|
|
|
|
}
|
|
|
|
|
2024-11-26 15:28:42 +00:00
|
|
|
if deps.URL == "" {
|
2023-06-22 09:36:43 +00:00
|
|
|
log.Panicln("urls is nil on <NewHubadminClient>")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &HubadminClient{
|
|
|
|
logger: deps.Logger,
|
2024-11-26 15:28:42 +00:00
|
|
|
url: deps.URL,
|
2023-06-22 09:36:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 14:50:48 +00:00
|
|
|
func (receiver *HubadminClient) GetTariff(ctx context.Context, accessToken string, tariffID string) (*models.Tariff, errors.Error) {
|
2024-11-26 15:28:42 +00:00
|
|
|
tariffURL, err := url.JoinPath(fmt.Sprintf("%s/tariff", receiver.url), tariffID)
|
2023-06-22 09:36:43 +00:00
|
|
|
if err != nil {
|
2023-06-29 14:50:48 +00:00
|
|
|
return nil, errors.New(fmt.Errorf("failed to join path on <GetTariff> of <HubadminClient>: %w", err), errors.ErrInternalError)
|
2023-06-22 09:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
response, err := client.Get[models.Tariff, models.FastifyError](ctx, &client.RequestSettings{
|
2023-06-29 14:50:48 +00:00
|
|
|
URL: tariffURL,
|
|
|
|
Headers: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"Authorization": fmt.Sprintf("Bearer %s", accessToken),
|
|
|
|
},
|
2023-06-22 09:36:43 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
receiver.logger.Error("failed to request get tariff on <GetTariff> of <HubadminClient>",
|
|
|
|
zap.Error(err),
|
|
|
|
zap.String("tariffID", tariffID),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil, errors.New(
|
|
|
|
fmt.Errorf("failed to request get tariff with <%s> on <GetTariff> of <HubadminClient>: %w", tariffID, err),
|
|
|
|
errors.ErrInternalError,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if response.Error != nil {
|
|
|
|
receiver.logger.Error("failed request on <GetTariff> of <HubadminClient>",
|
|
|
|
zap.String("error", response.Error.Message),
|
|
|
|
zap.String("tariffID", tariffID),
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil, errors.New(
|
|
|
|
fmt.Errorf("failed request with <%s> on <GetTariff> of <HubadminClient>: %s", tariffID, response.Error.Message),
|
|
|
|
utils.DetermineClientErrorResponse(response.StatusCode),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.Body, nil
|
|
|
|
}
|
|
|
|
|
2023-06-29 14:50:48 +00:00
|
|
|
func (receiver *HubadminClient) GetTariffs(ctx context.Context, accessToken string, tarriffIDs []string) ([]models.Tariff, errors.Error) {
|
2023-06-22 09:36:43 +00:00
|
|
|
tariffs := make([]models.Tariff, len(tarriffIDs))
|
|
|
|
|
|
|
|
for index, tariffID := range tarriffIDs {
|
2023-06-29 14:50:48 +00:00
|
|
|
tariff, err := receiver.GetTariff(ctx, accessToken, tariffID)
|
2023-06-22 09:36:43 +00:00
|
|
|
if err != nil {
|
|
|
|
receiver.logger.Error("failed to get tariff on <GetTariffs> of <HubadminClient>", zap.Error(err), zap.String("tariffID", tariffID))
|
|
|
|
return []models.Tariff{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tariffs[index] = *tariff
|
|
|
|
}
|
|
|
|
|
|
|
|
return tariffs, nil
|
|
|
|
}
|