Skip to content

Commit

Permalink
netquery: make parseQueryRequestPayload generic
Browse files Browse the repository at this point in the history
  • Loading branch information
ppacher committed Sep 14, 2023
1 parent ba72c20 commit 8b4a733
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions netquery/query_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ type (
IsDevMode func() bool
Database *Database
}

BatchQueryHandler struct {
IsDevMode func() bool
Database *Database
}
)

func (qh *QueryHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
start := time.Now()
requestPayload, err := parseQueryRequestPayload(req)
requestPayload, err := parseQueryRequestPayload[QueryRequestPayload](req)
if err != nil {
http.Error(resp, err.Error(), http.StatusBadRequest)

Expand Down Expand Up @@ -105,8 +110,11 @@ func (qh *QueryHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
}
}

func parseQueryRequestPayload(req *http.Request) (*QueryRequestPayload, error) { //nolint:dupl
var body io.Reader
func parseQueryRequestPayload[T any](req *http.Request) (*T, error) { //nolint:dupl
var (
body io.Reader
requestPayload T
)

switch req.Method {
case http.MethodPost, http.MethodPut:
Expand All @@ -117,7 +125,6 @@ func parseQueryRequestPayload(req *http.Request) (*QueryRequestPayload, error) {
return nil, fmt.Errorf("invalid HTTP method")
}

var requestPayload QueryRequestPayload
blob, err := io.ReadAll(body)
if err != nil {
return nil, fmt.Errorf("failed to read body" + err.Error())
Expand Down

0 comments on commit 8b4a733

Please sign in to comment.