mirror of
https://github.com/netscrawler/changeAPI.git
synced 2026-09-05 08:29:36 +00:00
v0.3
This commit is contained in:
@@ -6,8 +6,17 @@ import (
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"log/slog"
|
||||
"os"
|
||||
)
|
||||
|
||||
type response struct {
|
||||
ConvertedAmount uint32
|
||||
Rate float32
|
||||
ConvertedAmo uint32
|
||||
ConvertedCurrency string
|
||||
}
|
||||
|
||||
type Converter interface {
|
||||
Convert(ctx context.Context,
|
||||
amount uint32,
|
||||
@@ -28,20 +37,31 @@ func (s *serverAPI) Convert(
|
||||
ctx context.Context,
|
||||
req *cnvrtv1.ConvertRequest) (
|
||||
*cnvrtv1.ConvertResponse, error) {
|
||||
|
||||
log := setupLogger()
|
||||
log.Info("convertationRequest", slog.Any("req", req))
|
||||
|
||||
if !isValidCurrency(req.GetTargetCurrency()) {
|
||||
return nil, status.Error(codes.InvalidArgument, "Invalid target currency")
|
||||
}
|
||||
|
||||
convertedAmount, rate, err := s.convert.Convert(ctx, req.GetAmount(), req.GetTargetCurrency())
|
||||
baseAmount := req.GetAmount()
|
||||
convertedAmo := convertedAmount
|
||||
convertedCurrency := req.GetTargetCurrency()
|
||||
Rate := rate
|
||||
r := response{baseAmount, rate, convertedAmo, convertedCurrency}
|
||||
log.Info("ConvertationResponse", slog.Any("res", r))
|
||||
|
||||
if err != nil {
|
||||
//todo error handler
|
||||
return nil, status.Error(codes.Internal, "Internal error")
|
||||
}
|
||||
return &cnvrtv1.ConvertResponse{
|
||||
BaseAmount: req.GetAmount(),
|
||||
ConvertedAmount: convertedAmount,
|
||||
ConvertedCurrency: req.GetTargetCurrency(),
|
||||
Rate: rate,
|
||||
BaseAmount: baseAmount,
|
||||
ConvertedAmount: convertedAmo,
|
||||
ConvertedCurrency: convertedCurrency,
|
||||
Rate: Rate,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -57,3 +77,11 @@ func isValidCurrency(currency string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func setupLogger() *slog.Logger {
|
||||
var log *slog.Logger
|
||||
log = slog.New(
|
||||
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}),
|
||||
)
|
||||
return log
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ func (c *Converter) Convert(
|
||||
)
|
||||
log.Info("convertation")
|
||||
var convertedAmount uint32
|
||||
//TODO extract rate
|
||||
|
||||
rate, err := rateExtract.GetExchangeRate(currency)
|
||||
if err != nil {
|
||||
log.Error("Error extracting rate", sl.Err(err))
|
||||
|
||||
Reference in New Issue
Block a user