@rivascva/dt-idl 1.1.47 → 1.1.49
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
|
@@ -17,21 +17,21 @@ type Logger interface {
|
|
|
17
17
|
Fatal(v ...any)
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
type
|
|
20
|
+
type ConsoleLogger 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
|
-
// Creates a new logger.
|
|
28
|
-
func
|
|
29
|
-
return
|
|
27
|
+
// Creates a new console logger.
|
|
28
|
+
func NewConsoleLogger() *ConsoleLogger {
|
|
29
|
+
return NewConsoleLoggerWithPrefix("")
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
// Creates a new logger with a custom prefix.
|
|
33
|
-
func
|
|
34
|
-
return &
|
|
33
|
+
func NewConsoleLoggerWithPrefix(prefix string) *ConsoleLogger {
|
|
34
|
+
return &ConsoleLogger{
|
|
35
35
|
prefix: prefix,
|
|
36
36
|
info: log.New(os.Stdout, "[INFO] ", log.LstdFlags),
|
|
37
37
|
warn: log.New(os.Stdout, "[WARN] ", log.LstdFlags),
|
|
@@ -39,18 +39,18 @@ func NewLoggerWithPrefix(prefix string) Logger {
|
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
func (l *
|
|
42
|
+
func (l *ConsoleLogger) Info(v ...any) {
|
|
43
43
|
l.info.Println(l.prefix, fmt.Sprint(v...))
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
func (l *
|
|
46
|
+
func (l *ConsoleLogger) Warn(v ...any) {
|
|
47
47
|
l.warn.Println(l.prefix, fmt.Sprint(v...))
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
func (l *
|
|
50
|
+
func (l *ConsoleLogger) Error(v ...any) {
|
|
51
51
|
l.error.Println(l.prefix, fmt.Sprint(v...))
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
func (l *
|
|
54
|
+
func (l *ConsoleLogger) Fatal(v ...any) {
|
|
55
55
|
l.error.Fatal(l.prefix, fmt.Sprint(v...))
|
|
56
56
|
}
|
|
@@ -8,19 +8,19 @@ import (
|
|
|
8
8
|
"github.com/RivasCVA/dt-idl/go/utils/logger"
|
|
9
9
|
)
|
|
10
10
|
|
|
11
|
-
type Responder[T
|
|
11
|
+
type Responder[T ~string] 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
|
|
17
|
+
type StandardResponder[T ~string] struct {
|
|
18
18
|
log logger.Logger
|
|
19
19
|
baseErrorCode T
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
func
|
|
23
|
-
return &
|
|
22
|
+
func NewStandardResponder[T ~string](log logger.Logger, baseErrorCode T) *StandardResponder[T] {
|
|
23
|
+
return &StandardResponder[T]{
|
|
24
24
|
log: log,
|
|
25
25
|
baseErrorCode: baseErrorCode,
|
|
26
26
|
}
|
|
@@ -29,7 +29,7 @@ func NewResponder[T any](log logger.Logger, baseErrorCode T) Responder[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 *StandardResponder[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 *responder[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 *StandardResponder[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 *responder[T]) WriteError(w http.ResponseWriter, status int, message str
|
|
|
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 *StandardResponder[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),
|