-
Notifications
You must be signed in to change notification settings - Fork 1
/
smartapi.go
63 lines (52 loc) · 1.22 KB
/
smartapi.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
package smartapi
import (
"context"
"log"
)
// Logger logs the outcome of unsuccessful http requests
type Logger interface {
LogApiError(ctx context.Context, err ApiError)
LogError(ctx context.Context, err error)
}
// API interface represents an API
type API interface {
Start(string) error
Init()
}
// Method represents an http method
type Method int
const (
MethodPost Method = iota
MethodGet
MethodPatch
MethodDelete
MethodPut
MethodOptions
MethodConnect
MethodHead
MethodTrace
)
// String converts a method into a string with name of the method in capital letters
func (m Method) String() string {
return methodNames[m]
}
var methodNames = []string{
MethodPost: "POST",
MethodGet: "GET",
MethodPatch: "PATCH",
MethodDelete: "DELETE",
MethodPut: "PUT",
MethodOptions: "OPTIONS",
MethodConnect: "CONNECT",
MethodHead: "HEAD",
MethodTrace: "TRACE",
}
type defaultLogger struct{}
func (defaultLogger) LogApiError(ctx context.Context, err ApiError) {
log.Printf("[%d] %s", err.Status(), err.Error())
}
func (defaultLogger) LogError(ctx context.Context, err error) {
log.Print(err)
}
// DefaultLogger is simple implementation of the Logger interface
var DefaultLogger Logger = defaultLogger{}