Skip to content

Commit

Permalink
fixed and enahnced request extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
kubitre committed Feb 12, 2022
1 parent 5ea6eb4 commit 91e7259
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Infrastructre methods and types for make api simple

## Request

- extract body to data class from all sources request with special tag
- extract body to data class from all sources request with special tag and automatic response build (not best practic copy and paste deserialize code)

## Metrics Prometheus

Expand Down
16 changes: 14 additions & 2 deletions extractors/extract_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"reflect"

"github.com/kubitre/go_api_infra/response"
"github.com/spf13/cast"
)

Expand All @@ -20,10 +21,18 @@ const (

type extractor func(string, *http.Request) interface{}

func RequestToType(request *http.Request, data interface{}, tartgetParamsPlaces ...ParserType) (interface{}, error) {
/**
RequestToType - extract from http.Request payload data from:
1. Body (prefer)
2. PathVariables(optional)
3. QueryParams(optional)
4. Headers(optional)
*/
func RequestToType(request *http.Request, writer http.ResponseWriter, target string, data interface{}, tartgetParamsPlaces ...ParserType) (interface{}, error) {
if err := json.NewDecoder(request.Body).Decode(&data); err != nil {
if len(tartgetParamsPlaces) == 0 {
return nil, errors.New("can not unmarshalled request")
response.NewResponseJSON(writer, target).BadRequest(response.ApiError{Code: "WEB_INPUT_ERROR", Message: "can not deserialize input request", Target: target, Context: "Read spec at: /swagger"})
return nil, err
}
}

Expand All @@ -43,6 +52,9 @@ func RequestToType(request *http.Request, data interface{}, tartgetParamsPlaces
value := reflect.Indirect(reflect.ValueOf(data))

if err := prepareInlineStructFields(request, value, extractorFuncs); err != nil {
response.NewResponseJSON(writer, target).BadRequest(
response.ApiError{Code: "WEB_INPUT_ERROR", Message: "can not deserialize input request", Target: target, Context: "Read spec at: /swagger"},
)
return nil, err
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package middlewares
package metricsprometheus

import (
"net/http"
"strconv"
"time"

metricsprometheus "github.com/kubitre/go_api_infra/metrics_prometheus"
)

func AddGoldenMetrics(recorder metricsprometheus.MetricRecorder, method string, handler http.HandlerFunc) http.Handler {
func AddGoldenMetrics(recorder MetricRecorder, method string, handler http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
wi := &responseWriterInterceptor{
statusCode: http.StatusOK,
Expand All @@ -17,12 +15,12 @@ func AddGoldenMetrics(recorder metricsprometheus.MetricRecorder, method string,

timeStart := time.Now()
defer func() {
recorder.ObserveHTTPRequestDuration(request.Context(), metricsprometheus.HTTPReqProperties{
recorder.ObserveHTTPRequestDuration(request.Context(), HTTPReqProperties{
Method: method,
Code: strconv.Itoa(wi.statusCode),
}, time.Since(timeStart))

recorder.ObserveHTTPResponseSize(request.Context(), metricsprometheus.HTTPReqProperties{
recorder.ObserveHTTPResponseSize(request.Context(), HTTPReqProperties{
Method: method,
Code: strconv.Itoa(wi.statusCode),
}, wi.sizeResponse)
Expand Down

0 comments on commit 91e7259

Please sign in to comment.