-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
154 lines (122 loc) · 2.6 KB
/
api.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package api
import (
"fmt"
"net/http"
"net/url"
"strings"
)
type APIEndpoint interface {
URL() (*url.URL, error)
NewRequest(*url.Values) (*http.Request, error)
SetEndpoint(string) error
}
type APIClient interface {
ExecuteMethod(string, *url.Values) (APIResponse, error)
ExecuteMethodWithCallback(string, *url.Values, APIResponseCallback) error
DefaultArgs() *url.Values
}
type APIResponse interface {
Raw() []byte
String() string
Ok() (bool, APIError)
Pagination() (APIPagination, error)
Places() ([]APIPlacesResult, error)
}
type APIError interface {
String() string
Code() int64
Message() string
}
type APIPagination interface {
String() string
Pages() int
Page() int
PerPage() int
Total() int
Cursor() string
NextQuery() string
}
type APIPlacesResult interface {
WOFId() int64
WOFParentId() int64
WOFName() string
WOFPlacetype() string
WOFCountry() string
WOFRepo() string
Path() string
URI() string
String(...APIResultFlag) string
}
// APIResultFlag is all wet paint... still trying to work it out
type APIResultFlag interface {
Key() string
Value() interface{}
Bool() bool
String() string
Int() int
Int64() int64
Float64() float64
}
type APIResultBooleanFlag struct {
APIResultFlag
flkey string
flvalue bool
}
func (fl APIResultBooleanFlag) Key() string {
return fl.flkey
}
func (fl APIResultBooleanFlag) Value() interface{} {
return fl.flvalue
}
func (fl APIResultBooleanFlag) Bool() bool {
return fl.Value().(bool)
}
func (fl APIResultBooleanFlag) String() string {
return fmt.Sprintf("%t", fl.Bool())
}
func (fl APIResultBooleanFlag) Int() int {
if fl.Bool() {
return 1
}
return 0
}
func (fl APIResultBooleanFlag) Int64() int64 {
return int64(fl.Int())
}
func (fl APIResultBooleanFlag) Float64() float64 {
return float64(fl.Int())
}
func NewAPIResultBooleanFlag(key string, flag bool) APIResultBooleanFlag {
fl := APIResultBooleanFlag{
flkey: key,
flvalue: flag,
}
return fl
}
type APIResultMultiWriter interface { // PLEASE RENAME ME...
Write(APIPlacesResult) (int, error)
Close()
}
type APIResultWriter interface {
Write([]byte) (int, error)
WriteString(string) (int, error)
WriteResult(APIPlacesResult) (int, error)
Close() error
}
type APIResponseCallback func(APIResponse) error
type APIParams []string
func (p *APIParams) String() string {
return strings.Join(*p, "\n")
}
func (p *APIParams) Set(value string) error {
*p = append(*p, value)
return nil
}
func (p *APIParams) ToArgs() *url.Values {
args := url.Values{}
for _, str_pair := range *p {
pair := strings.Split(str_pair, "=")
args.Set(pair[0], pair[1])
}
return &args
}