pkg/http: Replace interface{}
with any
Signed-off-by: Thomas Hipp <thomashipp@gmail.com>
This commit is contained in:
parent
81d15a8436
commit
4e6033896d
3 changed files with 8 additions and 8 deletions
|
@ -17,11 +17,11 @@ var DefaultHTTPClient = &http.Client{
|
||||||
}
|
}
|
||||||
|
|
||||||
type Decoder interface {
|
type Decoder interface {
|
||||||
Decode(dst interface{}, src map[string][]string) error
|
Decode(dst any, src map[string][]string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Encoder interface {
|
type Encoder interface {
|
||||||
Encode(src interface{}, dst map[string][]string) error
|
Encode(src any, dst map[string][]string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type FormAuthorization func(url.Values)
|
type FormAuthorization func(url.Values)
|
||||||
|
@ -33,7 +33,7 @@ func AuthorizeBasic(user, password string) RequestAuthorization {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func FormRequest(endpoint string, request interface{}, encoder Encoder, authFn interface{}) (*http.Request, error) {
|
func FormRequest(endpoint string, request any, encoder Encoder, authFn any) (*http.Request, error) {
|
||||||
form := url.Values{}
|
form := url.Values{}
|
||||||
if err := encoder.Encode(request, form); err != nil {
|
if err := encoder.Encode(request, form); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -53,7 +53,7 @@ func FormRequest(endpoint string, request interface{}, encoder Encoder, authFn i
|
||||||
return req, nil
|
return req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func HttpRequest(client *http.Client, req *http.Request, response interface{}) error {
|
func HttpRequest(client *http.Client, req *http.Request, response any) error {
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -76,7 +76,7 @@ func HttpRequest(client *http.Client, req *http.Request, response interface{}) e
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func URLEncodeParams(resp interface{}, encoder Encoder) (url.Values, error) {
|
func URLEncodeParams(resp any, encoder Encoder) (url.Values, error) {
|
||||||
values := make(map[string][]string)
|
values := make(map[string][]string)
|
||||||
err := encoder.Encode(resp, values)
|
err := encoder.Encode(resp, values)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -8,11 +8,11 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
func MarshalJSON(w http.ResponseWriter, i interface{}) {
|
func MarshalJSON(w http.ResponseWriter, i any) {
|
||||||
MarshalJSONWithStatus(w, i, http.StatusOK)
|
MarshalJSONWithStatus(w, i, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
func MarshalJSONWithStatus(w http.ResponseWriter, i interface{}, status int) {
|
func MarshalJSONWithStatus(w http.ResponseWriter, i any, status int) {
|
||||||
w.Header().Set("content-type", "application/json")
|
w.Header().Set("content-type", "application/json")
|
||||||
w.WriteHeader(status)
|
w.WriteHeader(status)
|
||||||
if i == nil || (reflect.ValueOf(i).Kind() == reflect.Ptr && reflect.ValueOf(i).IsNil()) {
|
if i == nil || (reflect.ValueOf(i).Kind() == reflect.Ptr && reflect.ValueOf(i).IsNil()) {
|
||||||
|
|
|
@ -94,7 +94,7 @@ func TestConcatenateJSON(t *testing.T) {
|
||||||
|
|
||||||
func TestMarshalJSONWithStatus(t *testing.T) {
|
func TestMarshalJSONWithStatus(t *testing.T) {
|
||||||
type args struct {
|
type args struct {
|
||||||
i interface{}
|
i any
|
||||||
status int
|
status int
|
||||||
}
|
}
|
||||||
type res struct {
|
type res struct {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue