add simple fetch to another service
This commit is contained in:
parent
e0929aff56
commit
802dfc97a2
@ -33,8 +33,16 @@ func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err := params.Validate(); err != nil {
|
if err := params.Validate(); err != nil {
|
||||||
slog.Error("error validating struct", "error", err)
|
slog.Error("error validating struct", "error", err)
|
||||||
h.ToJSON(w, http.StatusBadRequest, app.H{"error": err.Error()})
|
h.ToJSON(w, http.StatusBadRequest, app.H{"error": err.Error()})
|
||||||
}
|
|
||||||
|
|
||||||
slog.Info("loc value", "value", params)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data, err := h.s.GetWeatherByCity(r.Context(), params)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("error", "err", err)
|
||||||
|
h.ToJSON(w, http.StatusInternalServerError, app.H{"error": err})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Info("data retrieved", "location", params.Location)
|
||||||
|
h.ToJSON(w, http.StatusOK, data)
|
||||||
|
}
|
||||||
|
|||||||
@ -2,12 +2,17 @@ package meteo
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type inMemory struct {
|
type inMemory struct {
|
||||||
data map[string]MeteoData
|
data any
|
||||||
mu *sync.RWMutex
|
mu *sync.RWMutex
|
||||||
|
expiry time.Time
|
||||||
}
|
}
|
||||||
type Service struct {
|
type Service struct {
|
||||||
inMemory
|
inMemory
|
||||||
@ -18,5 +23,25 @@ func NewService() *Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) GetWeatherByCity(ctx context.Context, params GetMeteoData) ([]MeteoData, error) {
|
func (s *Service) GetWeatherByCity(ctx context.Context, params GetMeteoData) ([]MeteoData, error) {
|
||||||
|
fromDate, err := time.Parse("2006-01-02", params.Date)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
toDate := fromDate.AddDate(0, 0, params.Days-1)
|
||||||
|
|
||||||
|
url := fmt.Sprintf("http://localhost:8080/data?city=%s&from=%s&to=%s",
|
||||||
|
params.Location, params.Date, toDate.Format("2006-01-02"))
|
||||||
|
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// TODO add backoff
|
||||||
|
// TODO add ristretto
|
||||||
|
|
||||||
|
slog.Info("fetched data", "data", resp)
|
||||||
|
|
||||||
return []MeteoData{}, nil
|
return []MeteoData{}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user