forked from darylhjd/mangodex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
86 lines (71 loc) · 2.02 KB
/
helpers.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package mangodex
import (
"context"
"errors"
"io"
"net/url"
"os"
)
type Demographic string
type Status string
type ReadStatus string
type ContentRating string
type ListVisibility string
const (
ShonenDemographic Demographic = "shonen"
ShoujoDemographic Demographic = "shoujo"
JoseiDemographic Demographic = "josei"
SeinenDemograpic Demographic = "seinen"
OngoingStatus Status = "ongoing"
CompletedStatus Status = "completed"
HiatusStatus Status = "hiatus"
CancelledStatus Status = "cancelled"
ReadingReadStatus ReadStatus = "reading"
OnHoldReadStatus ReadStatus = "on_hold"
PlanToReadReadStatus ReadStatus = "plan_to_read"
DroppedReadStatus ReadStatus = "dropped"
ReReadingReadStatus ReadStatus = "re_reading"
CompletedReadStatus ReadStatus = "completed"
SafeRating ContentRating = "safe"
SuggestiveRating ContentRating = "suggestive"
EroticaRating ContentRating = "erotica"
PornRating ContentRating = "pornographic"
PublicList ListVisibility = "public"
PrivateList ListVisibility = "private"
)
var (
testClient = NewDexClient()
user, pwd = os.Getenv("USER"), os.Getenv("PASSWORD")
)
type ResponseType interface {
GetResult() string
}
type Response struct {
Result string `json:"result"`
}
func (r *Response) GetResult() string {
return r.Result
}
// checkErrorAndResult : Helper function to check success of request by error and status code.
func checkErrorAndResult(err error, r ResponseType) error {
switch {
case err != nil:
return err
case r.GetResult() != "ok":
return errors.New(r.GetResult())
default:
return nil
}
}
// responseOp : Convenience function for simple operations that return a ResponseType.
func (dc *DexClient) responseOp(ctx context.Context, method, path string, body io.Reader, r ResponseType) error {
u, _ := url.Parse(BaseAPI)
u.Path = path
// Default ResponseType will be a Response struct
if r == nil {
res := Response{}
r = &res
}
_, err := dc.RequestAndDecode(ctx, method, u.String(), body, &r)
return checkErrorAndResult(err, r)
}