-
Notifications
You must be signed in to change notification settings - Fork 666
/
transport.go
123 lines (105 loc) · 2.74 KB
/
transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package apiutil
import (
"context"
"encoding/json"
"log/slog"
"net/http"
"strconv"
"github.com/absmach/magistrala/pkg/errors"
kithttp "github.com/go-kit/kit/transport/http"
)
// LoggingErrorEncoder is a go-kit error encoder logging decorator.
func LoggingErrorEncoder(logger *slog.Logger, enc kithttp.ErrorEncoder) kithttp.ErrorEncoder {
return func(ctx context.Context, err error, w http.ResponseWriter) {
if errors.Contains(err, ErrValidation) {
logger.Error(err.Error())
}
enc(ctx, err, w)
}
}
// ReadStringQuery reads the value of string http query parameters for a given key.
func ReadStringQuery(r *http.Request, key, def string) (string, error) {
vals := r.URL.Query()[key]
if len(vals) > 1 {
return "", ErrInvalidQueryParams
}
if len(vals) == 0 {
return def, nil
}
return vals[0], nil
}
// ReadMetadataQuery reads the value of json http query parameters for a given key.
func ReadMetadataQuery(r *http.Request, key string, def map[string]interface{}) (map[string]interface{}, error) {
vals := r.URL.Query()[key]
if len(vals) > 1 {
return nil, ErrInvalidQueryParams
}
if len(vals) == 0 {
return def, nil
}
m := make(map[string]interface{})
err := json.Unmarshal([]byte(vals[0]), &m)
if err != nil {
return nil, errors.Wrap(ErrInvalidQueryParams, err)
}
return m, nil
}
// ReadBoolQuery reads boolean query parameters in a given http request.
func ReadBoolQuery(r *http.Request, key string, def bool) (bool, error) {
vals := r.URL.Query()[key]
if len(vals) > 1 {
return false, ErrInvalidQueryParams
}
if len(vals) == 0 {
return def, nil
}
b, err := strconv.ParseBool(vals[0])
if err != nil {
return false, errors.Wrap(ErrInvalidQueryParams, err)
}
return b, nil
}
type number interface {
int64 | float64 | uint16 | uint64
}
// ReadNumQuery returns a numeric value.
func ReadNumQuery[N number](r *http.Request, key string, def N) (N, error) {
vals := r.URL.Query()[key]
if len(vals) > 1 {
return 0, ErrInvalidQueryParams
}
if len(vals) == 0 {
return def, nil
}
val := vals[0]
switch any(def).(type) {
case int64:
v, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return 0, errors.Wrap(ErrInvalidQueryParams, err)
}
return N(v), nil
case uint64:
v, err := strconv.ParseUint(val, 10, 64)
if err != nil {
return 0, errors.Wrap(ErrInvalidQueryParams, err)
}
return N(v), nil
case uint16:
v, err := strconv.ParseUint(val, 10, 16)
if err != nil {
return 0, errors.Wrap(ErrInvalidQueryParams, err)
}
return N(v), nil
case float64:
v, err := strconv.ParseFloat(val, 64)
if err != nil {
return 0, errors.Wrap(ErrInvalidQueryParams, err)
}
return N(v), nil
default:
return def, nil
}
}