432 lines
13 KiB
Go
432 lines
13 KiB
Go
// Package api provides primitives to interact with the openapi HTTP API.
|
|
//
|
|
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.5.1 DO NOT EDIT.
|
|
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/oapi-codegen/runtime"
|
|
strictnethttp "github.com/oapi-codegen/runtime/strictmiddleware/nethttp"
|
|
)
|
|
|
|
// Defines values for WeatherCondition.
|
|
const (
|
|
CLEAR WeatherCondition = "CLEAR"
|
|
CLOUDY WeatherCondition = "CLOUDY"
|
|
RAIN WeatherCondition = "RAIN"
|
|
RAINSNOW WeatherCondition = "RAIN_SNOW"
|
|
SNOW WeatherCondition = "SNOW"
|
|
)
|
|
|
|
// Celsius 섭씨 온도(℃)
|
|
type Celsius = float32
|
|
|
|
// DailyForecast defines model for DailyForecast.
|
|
type DailyForecast struct {
|
|
// Condition 하루 전체를 대표하는 날씨 상태
|
|
Condition WeatherCondition `json:"condition"`
|
|
|
|
// Date 해당 날짜
|
|
Date time.Time `json:"date"`
|
|
|
|
// High 하루 동안의 예상 최고 기온(℃)
|
|
High Celsius `json:"high"`
|
|
|
|
// Low 하루 동안의 예상 최저 기온(℃)
|
|
Low Celsius `json:"low"`
|
|
}
|
|
|
|
// DailyForecastList defines model for DailyForecastList.
|
|
type DailyForecastList struct {
|
|
Items []DailyForecast `json:"items"`
|
|
}
|
|
|
|
// Error defines model for Error.
|
|
type Error struct {
|
|
// ErrorCode 에러 코드
|
|
ErrorCode int32 `json:"errorCode"`
|
|
|
|
// ErrorMessage 에러 메시지
|
|
ErrorMessage string `json:"errorMessage"`
|
|
}
|
|
|
|
// HourlyForecast defines model for HourlyForecast.
|
|
type HourlyForecast struct {
|
|
// Condition 1시간 동안의 예상 날씨 상태
|
|
Condition WeatherCondition `json:"condition"`
|
|
|
|
// Temperature 1시간 동안의 예상 기온(℃)
|
|
Temperature Celsius `json:"temperature"`
|
|
|
|
// Time 예상 시간
|
|
Time time.Time `json:"time"`
|
|
}
|
|
|
|
// HourlyForecastList defines model for HourlyForecastList.
|
|
type HourlyForecastList struct {
|
|
Items []HourlyForecast `json:"items"`
|
|
}
|
|
|
|
// WeatherCondition defines model for WeatherCondition.
|
|
type WeatherCondition string
|
|
|
|
// ForecastsListDailyParams defines parameters for ForecastsListDaily.
|
|
type ForecastsListDailyParams struct {
|
|
// Timezone 타임존 (기본값: Asia/Seoul)
|
|
Timezone *string `form:"timezone,omitempty" json:"timezone,omitempty"`
|
|
|
|
// Now 현재 시간 (기본값: 현재 시간)
|
|
Now *time.Time `form:"now,omitempty" json:"now,omitempty"`
|
|
}
|
|
|
|
// ServerInterface represents all server handlers.
|
|
type ServerInterface interface {
|
|
|
|
// (GET /forecasts/daily)
|
|
ForecastsListDaily(w http.ResponseWriter, r *http.Request, params ForecastsListDailyParams)
|
|
|
|
// (GET /forecasts/hourly)
|
|
ForecastsListHourly(w http.ResponseWriter, r *http.Request)
|
|
}
|
|
|
|
// Unimplemented server implementation that returns http.StatusNotImplemented for each endpoint.
|
|
|
|
type Unimplemented struct{}
|
|
|
|
// (GET /forecasts/daily)
|
|
func (_ Unimplemented) ForecastsListDaily(w http.ResponseWriter, r *http.Request, params ForecastsListDailyParams) {
|
|
w.WriteHeader(http.StatusNotImplemented)
|
|
}
|
|
|
|
// (GET /forecasts/hourly)
|
|
func (_ Unimplemented) ForecastsListHourly(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotImplemented)
|
|
}
|
|
|
|
// ServerInterfaceWrapper converts contexts to parameters.
|
|
type ServerInterfaceWrapper struct {
|
|
Handler ServerInterface
|
|
HandlerMiddlewares []MiddlewareFunc
|
|
ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
|
|
}
|
|
|
|
type MiddlewareFunc func(http.Handler) http.Handler
|
|
|
|
// ForecastsListDaily operation middleware
|
|
func (siw *ServerInterfaceWrapper) ForecastsListDaily(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var err error
|
|
|
|
// Parameter object where we will unmarshal all parameters from the context
|
|
var params ForecastsListDailyParams
|
|
|
|
// ------------- Optional query parameter "timezone" -------------
|
|
|
|
err = runtime.BindQueryParameter("form", false, false, "timezone", r.URL.Query(), ¶ms.Timezone)
|
|
if err != nil {
|
|
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "timezone", Err: err})
|
|
return
|
|
}
|
|
|
|
// ------------- Optional query parameter "now" -------------
|
|
|
|
err = runtime.BindQueryParameter("form", false, false, "now", r.URL.Query(), ¶ms.Now)
|
|
if err != nil {
|
|
siw.ErrorHandlerFunc(w, r, &InvalidParamFormatError{ParamName: "now", Err: err})
|
|
return
|
|
}
|
|
|
|
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
siw.Handler.ForecastsListDaily(w, r, params)
|
|
}))
|
|
|
|
for _, middleware := range siw.HandlerMiddlewares {
|
|
handler = middleware(handler)
|
|
}
|
|
|
|
handler.ServeHTTP(w, r)
|
|
}
|
|
|
|
// ForecastsListHourly operation middleware
|
|
func (siw *ServerInterfaceWrapper) ForecastsListHourly(w http.ResponseWriter, r *http.Request) {
|
|
|
|
handler := http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
siw.Handler.ForecastsListHourly(w, r)
|
|
}))
|
|
|
|
for _, middleware := range siw.HandlerMiddlewares {
|
|
handler = middleware(handler)
|
|
}
|
|
|
|
handler.ServeHTTP(w, r)
|
|
}
|
|
|
|
type UnescapedCookieParamError struct {
|
|
ParamName string
|
|
Err error
|
|
}
|
|
|
|
func (e *UnescapedCookieParamError) Error() string {
|
|
return fmt.Sprintf("error unescaping cookie parameter '%s'", e.ParamName)
|
|
}
|
|
|
|
func (e *UnescapedCookieParamError) Unwrap() error {
|
|
return e.Err
|
|
}
|
|
|
|
type UnmarshalingParamError struct {
|
|
ParamName string
|
|
Err error
|
|
}
|
|
|
|
func (e *UnmarshalingParamError) Error() string {
|
|
return fmt.Sprintf("Error unmarshaling parameter %s as JSON: %s", e.ParamName, e.Err.Error())
|
|
}
|
|
|
|
func (e *UnmarshalingParamError) Unwrap() error {
|
|
return e.Err
|
|
}
|
|
|
|
type RequiredParamError struct {
|
|
ParamName string
|
|
}
|
|
|
|
func (e *RequiredParamError) Error() string {
|
|
return fmt.Sprintf("Query argument %s is required, but not found", e.ParamName)
|
|
}
|
|
|
|
type RequiredHeaderError struct {
|
|
ParamName string
|
|
Err error
|
|
}
|
|
|
|
func (e *RequiredHeaderError) Error() string {
|
|
return fmt.Sprintf("Header parameter %s is required, but not found", e.ParamName)
|
|
}
|
|
|
|
func (e *RequiredHeaderError) Unwrap() error {
|
|
return e.Err
|
|
}
|
|
|
|
type InvalidParamFormatError struct {
|
|
ParamName string
|
|
Err error
|
|
}
|
|
|
|
func (e *InvalidParamFormatError) Error() string {
|
|
return fmt.Sprintf("Invalid format for parameter %s: %s", e.ParamName, e.Err.Error())
|
|
}
|
|
|
|
func (e *InvalidParamFormatError) Unwrap() error {
|
|
return e.Err
|
|
}
|
|
|
|
type TooManyValuesForParamError struct {
|
|
ParamName string
|
|
Count int
|
|
}
|
|
|
|
func (e *TooManyValuesForParamError) Error() string {
|
|
return fmt.Sprintf("Expected one value for %s, got %d", e.ParamName, e.Count)
|
|
}
|
|
|
|
// Handler creates http.Handler with routing matching OpenAPI spec.
|
|
func Handler(si ServerInterface) http.Handler {
|
|
return HandlerWithOptions(si, ChiServerOptions{})
|
|
}
|
|
|
|
type ChiServerOptions struct {
|
|
BaseURL string
|
|
BaseRouter chi.Router
|
|
Middlewares []MiddlewareFunc
|
|
ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
|
|
}
|
|
|
|
// HandlerFromMux creates http.Handler with routing matching OpenAPI spec based on the provided mux.
|
|
func HandlerFromMux(si ServerInterface, r chi.Router) http.Handler {
|
|
return HandlerWithOptions(si, ChiServerOptions{
|
|
BaseRouter: r,
|
|
})
|
|
}
|
|
|
|
func HandlerFromMuxWithBaseURL(si ServerInterface, r chi.Router, baseURL string) http.Handler {
|
|
return HandlerWithOptions(si, ChiServerOptions{
|
|
BaseURL: baseURL,
|
|
BaseRouter: r,
|
|
})
|
|
}
|
|
|
|
// HandlerWithOptions creates http.Handler with additional options
|
|
func HandlerWithOptions(si ServerInterface, options ChiServerOptions) http.Handler {
|
|
r := options.BaseRouter
|
|
|
|
if r == nil {
|
|
r = chi.NewRouter()
|
|
}
|
|
if options.ErrorHandlerFunc == nil {
|
|
options.ErrorHandlerFunc = func(w http.ResponseWriter, r *http.Request, err error) {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
}
|
|
}
|
|
wrapper := ServerInterfaceWrapper{
|
|
Handler: si,
|
|
HandlerMiddlewares: options.Middlewares,
|
|
ErrorHandlerFunc: options.ErrorHandlerFunc,
|
|
}
|
|
|
|
r.Group(func(r chi.Router) {
|
|
r.Get(options.BaseURL+"/forecasts/daily", wrapper.ForecastsListDaily)
|
|
})
|
|
r.Group(func(r chi.Router) {
|
|
r.Get(options.BaseURL+"/forecasts/hourly", wrapper.ForecastsListHourly)
|
|
})
|
|
|
|
return r
|
|
}
|
|
|
|
type ForecastsListDailyRequestObject struct {
|
|
Params ForecastsListDailyParams
|
|
}
|
|
|
|
type ForecastsListDailyResponseObject interface {
|
|
VisitForecastsListDailyResponse(w http.ResponseWriter) error
|
|
}
|
|
|
|
type ForecastsListDaily200JSONResponse DailyForecastList
|
|
|
|
func (response ForecastsListDaily200JSONResponse) VisitForecastsListDailyResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(200)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type ForecastsListDaily500JSONResponse Error
|
|
|
|
func (response ForecastsListDaily500JSONResponse) VisitForecastsListDailyResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(500)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type ForecastsListHourlyRequestObject struct {
|
|
}
|
|
|
|
type ForecastsListHourlyResponseObject interface {
|
|
VisitForecastsListHourlyResponse(w http.ResponseWriter) error
|
|
}
|
|
|
|
type ForecastsListHourly200JSONResponse HourlyForecastList
|
|
|
|
func (response ForecastsListHourly200JSONResponse) VisitForecastsListHourlyResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(200)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
type ForecastsListHourly500JSONResponse Error
|
|
|
|
func (response ForecastsListHourly500JSONResponse) VisitForecastsListHourlyResponse(w http.ResponseWriter) error {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(500)
|
|
|
|
return json.NewEncoder(w).Encode(response)
|
|
}
|
|
|
|
// StrictServerInterface represents all server handlers.
|
|
type StrictServerInterface interface {
|
|
|
|
// (GET /forecasts/daily)
|
|
ForecastsListDaily(ctx context.Context, request ForecastsListDailyRequestObject) (ForecastsListDailyResponseObject, error)
|
|
|
|
// (GET /forecasts/hourly)
|
|
ForecastsListHourly(ctx context.Context, request ForecastsListHourlyRequestObject) (ForecastsListHourlyResponseObject, error)
|
|
}
|
|
|
|
type StrictHandlerFunc = strictnethttp.StrictHTTPHandlerFunc
|
|
type StrictMiddlewareFunc = strictnethttp.StrictHTTPMiddlewareFunc
|
|
|
|
type StrictHTTPServerOptions struct {
|
|
RequestErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
|
|
ResponseErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
|
|
}
|
|
|
|
func NewStrictHandler(ssi StrictServerInterface, middlewares []StrictMiddlewareFunc) ServerInterface {
|
|
return &strictHandler{ssi: ssi, middlewares: middlewares, options: StrictHTTPServerOptions{
|
|
RequestErrorHandlerFunc: func(w http.ResponseWriter, r *http.Request, err error) {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
},
|
|
ResponseErrorHandlerFunc: func(w http.ResponseWriter, r *http.Request, err error) {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
},
|
|
}}
|
|
}
|
|
|
|
func NewStrictHandlerWithOptions(ssi StrictServerInterface, middlewares []StrictMiddlewareFunc, options StrictHTTPServerOptions) ServerInterface {
|
|
return &strictHandler{ssi: ssi, middlewares: middlewares, options: options}
|
|
}
|
|
|
|
type strictHandler struct {
|
|
ssi StrictServerInterface
|
|
middlewares []StrictMiddlewareFunc
|
|
options StrictHTTPServerOptions
|
|
}
|
|
|
|
// ForecastsListDaily operation middleware
|
|
func (sh *strictHandler) ForecastsListDaily(w http.ResponseWriter, r *http.Request, params ForecastsListDailyParams) {
|
|
var request ForecastsListDailyRequestObject
|
|
|
|
request.Params = params
|
|
|
|
handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) {
|
|
return sh.ssi.ForecastsListDaily(ctx, request.(ForecastsListDailyRequestObject))
|
|
}
|
|
for _, middleware := range sh.middlewares {
|
|
handler = middleware(handler, "ForecastsListDaily")
|
|
}
|
|
|
|
response, err := handler(r.Context(), w, r, request)
|
|
|
|
if err != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, err)
|
|
} else if validResponse, ok := response.(ForecastsListDailyResponseObject); ok {
|
|
if err := validResponse.VisitForecastsListDailyResponse(w); err != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, err)
|
|
}
|
|
} else if response != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response))
|
|
}
|
|
}
|
|
|
|
// ForecastsListHourly operation middleware
|
|
func (sh *strictHandler) ForecastsListHourly(w http.ResponseWriter, r *http.Request) {
|
|
var request ForecastsListHourlyRequestObject
|
|
|
|
handler := func(ctx context.Context, w http.ResponseWriter, r *http.Request, request interface{}) (interface{}, error) {
|
|
return sh.ssi.ForecastsListHourly(ctx, request.(ForecastsListHourlyRequestObject))
|
|
}
|
|
for _, middleware := range sh.middlewares {
|
|
handler = middleware(handler, "ForecastsListHourly")
|
|
}
|
|
|
|
response, err := handler(r.Context(), w, r, request)
|
|
|
|
if err != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, err)
|
|
} else if validResponse, ok := response.(ForecastsListHourlyResponseObject); ok {
|
|
if err := validResponse.VisitForecastsListHourlyResponse(w); err != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, err)
|
|
}
|
|
} else if response != nil {
|
|
sh.options.ResponseErrorHandlerFunc(w, r, fmt.Errorf("unexpected response type: %T", response))
|
|
}
|
|
}
|