-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
59 lines (50 loc) · 1.57 KB
/
helper.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
package vrest
import (
"encoding/base64"
"log/slog"
"reflect"
"slices"
"strings"
)
// IsXMLContentType checks whether the content type is XML.
func IsXMLContentType(contentType string) bool {
return strings.Index(contentType, "/xml") > 0
}
// IsJSONContentType checks whether the content type is JSON.
func IsJSONContentType(contentType string) bool {
return strings.Index(contentType, "/json") > 0
}
// IsSuccess reports whether the given request status code is a success.
// This is the default implementation and can be overridden by
// setting c.Overridable.IsSuccess.
// By default a success is a status code between 200 and 299.
// The success status codes can be overridden by setting
// request.SetSuccessStatusCode() for a single requests.
func IsSuccess(req *Request) bool {
if req.Response.Raw == nil {
return false
}
statusCode := req.Response.Raw.StatusCode
if len(req.Response.SuccessStatusCodes) > 0 {
return slices.Contains(req.Response.SuccessStatusCodes, statusCode)
}
return statusCode >= 200 && statusCode < 300
}
func (c *Client) closeRawResponse(req *Request) {
resp := req.Response.Raw
if resp != nil && resp.Body != nil {
err := resp.Body.Close()
if err != nil {
c.logger.LogAttrs(req.Raw.Context(), slog.LevelError,
"error when closing response body",
slog.String("error", err.Error()))
}
}
}
func typeOf(i interface{}) reflect.Type {
return reflect.Indirect(reflect.ValueOf(i)).Type()
}
func encodeBasicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}