@rivascva/dt-idl 1.1.45 → 1.1.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/go/utils/fetch/fetch.go
CHANGED
|
@@ -6,14 +6,14 @@ import (
|
|
|
6
6
|
"net/http"
|
|
7
7
|
)
|
|
8
8
|
|
|
9
|
-
type
|
|
9
|
+
type Response struct {
|
|
10
10
|
StatusCode int
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
// Makes a GET request to the given url.
|
|
14
14
|
//
|
|
15
15
|
// Parses the JSON-encoded response and stores the result into v.
|
|
16
|
-
func Get(url string, v any) (*
|
|
16
|
+
func Get(url string, v any) (*Response, error) {
|
|
17
17
|
// make the GET request
|
|
18
18
|
resp, err := http.Get(url)
|
|
19
19
|
if err != nil {
|
|
@@ -33,7 +33,7 @@ func Get(url string, v any) (*response, error) {
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
// build the custom fetch response
|
|
36
|
-
response :=
|
|
36
|
+
response := Response{
|
|
37
37
|
StatusCode: resp.StatusCode,
|
|
38
38
|
}
|
|
39
39
|
|
|
@@ -6,7 +6,7 @@ import (
|
|
|
6
6
|
"os"
|
|
7
7
|
)
|
|
8
8
|
|
|
9
|
-
type
|
|
9
|
+
type Logger interface {
|
|
10
10
|
// Prints an info log message.
|
|
11
11
|
Info(v ...any)
|
|
12
12
|
// Prints a warning log message.
|
|
@@ -17,24 +17,21 @@ type LoggerInterface interface {
|
|
|
17
17
|
Fatal(v ...any)
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
type
|
|
20
|
+
type logger struct {
|
|
21
21
|
prefix string
|
|
22
22
|
info *log.Logger
|
|
23
23
|
warn *log.Logger
|
|
24
24
|
error *log.Logger
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
// A shared singleton log.
|
|
28
|
-
var Log = New()
|
|
29
|
-
|
|
30
27
|
// Creates a new logger.
|
|
31
|
-
func
|
|
32
|
-
return
|
|
28
|
+
func NewLogger() Logger {
|
|
29
|
+
return NewLoggerWithPrefix("")
|
|
33
30
|
}
|
|
34
31
|
|
|
35
32
|
// Creates a new logger with a custom prefix.
|
|
36
|
-
func
|
|
37
|
-
return &
|
|
33
|
+
func NewLoggerWithPrefix(prefix string) Logger {
|
|
34
|
+
return &logger{
|
|
38
35
|
prefix: prefix,
|
|
39
36
|
info: log.New(os.Stdout, "[INFO] ", log.LstdFlags),
|
|
40
37
|
warn: log.New(os.Stdout, "[WARN] ", log.LstdFlags),
|
|
@@ -42,18 +39,18 @@ func NewWithPrefix(prefix string) LoggerInterface {
|
|
|
42
39
|
}
|
|
43
40
|
}
|
|
44
41
|
|
|
45
|
-
func (l *
|
|
42
|
+
func (l *logger) Info(v ...any) {
|
|
46
43
|
l.info.Println(l.prefix, fmt.Sprint(v...))
|
|
47
44
|
}
|
|
48
45
|
|
|
49
|
-
func (l *
|
|
46
|
+
func (l *logger) Warn(v ...any) {
|
|
50
47
|
l.warn.Println(l.prefix, fmt.Sprint(v...))
|
|
51
48
|
}
|
|
52
49
|
|
|
53
|
-
func (l *
|
|
50
|
+
func (l *logger) Error(v ...any) {
|
|
54
51
|
l.error.Println(l.prefix, fmt.Sprint(v...))
|
|
55
52
|
}
|
|
56
53
|
|
|
57
|
-
func (l *
|
|
54
|
+
func (l *logger) Fatal(v ...any) {
|
|
58
55
|
l.error.Fatal(l.prefix, fmt.Sprint(v...))
|
|
59
56
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
package
|
|
1
|
+
package responder
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
4
|
"encoding/json"
|
|
@@ -8,19 +8,19 @@ import (
|
|
|
8
8
|
"github.com/RivasCVA/dt-idl/go/utils/logger"
|
|
9
9
|
)
|
|
10
10
|
|
|
11
|
-
type
|
|
11
|
+
type Responder[T any] interface {
|
|
12
12
|
Write(w http.ResponseWriter, status int, data any)
|
|
13
13
|
WriteError(w http.ResponseWriter, status int, message string)
|
|
14
14
|
WriteErrorWithCode(w http.ResponseWriter, status int, code T, message string)
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
type
|
|
18
|
-
log logger.
|
|
17
|
+
type responder[T any] struct {
|
|
18
|
+
log logger.Logger
|
|
19
19
|
baseErrorCode T
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
func
|
|
23
|
-
return &
|
|
22
|
+
func NewResponder[T any](log logger.Logger, baseErrorCode T) Responder[T] {
|
|
23
|
+
return &responder[T]{
|
|
24
24
|
log: log,
|
|
25
25
|
baseErrorCode: baseErrorCode,
|
|
26
26
|
}
|
|
@@ -29,7 +29,7 @@ func New[T any](log logger.LoggerInterface, baseErrorCode T) ResponseInterface[T
|
|
|
29
29
|
// Writes a JSON response to the given response writer.
|
|
30
30
|
//
|
|
31
31
|
// The given data is converted to JSON. An error response is written if the marshal fails.
|
|
32
|
-
func (r *
|
|
32
|
+
func (r *responder[T]) Write(w http.ResponseWriter, status int, data any) {
|
|
33
33
|
// encode the given data object
|
|
34
34
|
out, err := json.Marshal(data)
|
|
35
35
|
if err != nil {
|
|
@@ -49,7 +49,7 @@ func (r *Response[T]) Write(w http.ResponseWriter, status int, data any) {
|
|
|
49
49
|
// Writes a JSON error response to the given response writer.
|
|
50
50
|
//
|
|
51
51
|
// The response is based on the generated "api.Error" schema.
|
|
52
|
-
func (r *
|
|
52
|
+
func (r *responder[T]) WriteError(w http.ResponseWriter, status int, message string) {
|
|
53
53
|
r.WriteErrorWithCode(w, status, r.baseErrorCode, message)
|
|
54
54
|
}
|
|
55
55
|
|
|
@@ -58,7 +58,7 @@ func (r *Response[T]) WriteError(w http.ResponseWriter, status int, message stri
|
|
|
58
58
|
// The code is based on the generated "api.ErrorCode" enum.
|
|
59
59
|
//
|
|
60
60
|
// The response is based on the generated "api.Error" schema.
|
|
61
|
-
func (r *
|
|
61
|
+
func (r *responder[T]) WriteErrorWithCode(w http.ResponseWriter, status int, code T, message string) {
|
|
62
62
|
// encode an error object with the given message
|
|
63
63
|
out, err := json.Marshal(Error[T]{
|
|
64
64
|
Status: int64(status),
|