Add timezone support to /forecast/hourly endpoint
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
This commit is contained in:
@@ -5,6 +5,7 @@ package httpapi
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedapi/bind"
|
||||
"gitea.maximumdirect.net/ejr/weatherapi/internal/adapters/inbound/httpapi/presenter"
|
||||
@@ -17,6 +18,7 @@ type queryRequest struct {
|
||||
type precisionQueryRequest struct {
|
||||
Units presenter.Units
|
||||
Precision int
|
||||
Timezone *time.Location
|
||||
}
|
||||
|
||||
func bindQuery(r *http.Request) (queryRequest, error) {
|
||||
@@ -41,16 +43,29 @@ func bindQuery(r *http.Request) (queryRequest, error) {
|
||||
}
|
||||
|
||||
func bindPrecisionQuery(r *http.Request) (precisionQueryRequest, error) {
|
||||
return bindPrecisionQueryInternal(r, false)
|
||||
}
|
||||
|
||||
func bindForecastPrecisionQuery(r *http.Request) (precisionQueryRequest, error) {
|
||||
return bindPrecisionQueryInternal(r, true)
|
||||
}
|
||||
|
||||
func bindPrecisionQueryInternal(r *http.Request, allowTimezone bool) (precisionQueryRequest, error) {
|
||||
normalizeCommonQueryValue(r, "units")
|
||||
normalizeCommonQueryValue(r, "format")
|
||||
normalizeCommonQueryValue(r, "precision")
|
||||
|
||||
allowedExtra := []string{"precision"}
|
||||
if allowTimezone {
|
||||
allowedExtra = append(allowedExtra, "tz", "TZ")
|
||||
}
|
||||
|
||||
common, err := bind.CommonQueryParams(r, bind.QueryPolicy{
|
||||
AllowUnits: true,
|
||||
AllowFormat: true,
|
||||
DefaultUnits: string(presenter.UnitsMetric),
|
||||
RejectUnknown: true,
|
||||
}, "precision")
|
||||
}, allowedExtra...)
|
||||
if err != nil {
|
||||
return precisionQueryRequest{}, err
|
||||
}
|
||||
@@ -70,5 +85,18 @@ func bindPrecisionQuery(r *http.Request) (precisionQueryRequest, error) {
|
||||
if units == "" {
|
||||
units = presenter.UnitsMetric
|
||||
}
|
||||
return precisionQueryRequest{Units: units, Precision: precision}, nil
|
||||
|
||||
var tz *time.Location
|
||||
if allowTimezone {
|
||||
tz, err = parseTimezoneQuery(r)
|
||||
if err != nil {
|
||||
return precisionQueryRequest{}, err
|
||||
}
|
||||
}
|
||||
|
||||
return precisionQueryRequest{
|
||||
Units: units,
|
||||
Precision: precision,
|
||||
Timezone: tz,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user