forked from kurrik/twittergo
/
models.go
232 lines (197 loc) · 4.88 KB
/
models.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright 2011 Arne Roomann-Kurrik
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package twittergo
import (
"fmt"
"github.com/kurrik/json"
"io"
"io/ioutil"
"net/http"
"strconv"
"time"
)
const (
H_LIMIT = "X-Rate-Limit-Limit"
H_LIMIT_REMAIN = "X-Rate-Limit-Remaining"
H_LIMIT_RESET = "X-Rate-Limit-Reset"
H_MEDIA_LIMIT = "X-MediaRateLimit-Limit"
H_MEDIA_LIMIT_REMAIN = "X-MediaRateLimit-Remaining"
H_MEDIA_LIMIT_RESET = "X-MediaRateLimit-Reset"
)
const (
STATUS_INVALID = 400
STATUS_NOTFOUND = 404
STATUS_LIMIT = 429
STATUS_GATEWAY = 502
)
type Error struct {
Code int
Message string
}
func (e Error) Error() string {
msg := "Error %v: %v"
return fmt.Sprintf(msg, e.Code, e.Message)
}
type Errors struct {
Errors []Error
}
func (e Errors) Error() string {
msg := ""
for _, err := range e.Errors {
msg += err.Error() + ". "
}
return msg
}
type RateLimitError struct {
Limit uint32
Remaining uint32
Reset time.Time
}
func (e RateLimitError) Error() string {
msg := "Rate limit: %v, Remaining: %v, Reset: %v"
return fmt.Sprintf(msg, e.Limit, e.Remaining, e.Reset)
}
type APIResponse http.Response
func (r APIResponse) HasRateLimit() bool {
return r.Header.Get(H_LIMIT) != ""
}
func (r APIResponse) RateLimit() uint32 {
h := r.Header.Get(H_LIMIT)
i, _ := strconv.ParseUint(h, 10, 32)
return uint32(i)
}
func (r APIResponse) RateLimitRemaining() uint32 {
h := r.Header.Get(H_LIMIT_REMAIN)
i, _ := strconv.ParseUint(h, 10, 32)
return uint32(i)
}
func (r APIResponse) RateLimitReset() time.Time {
h := r.Header.Get(H_LIMIT_RESET)
i, _ := strconv.ParseUint(h, 10, 32)
t := time.Unix(int64(i), 0)
return t
}
func (r APIResponse) HasMediaRateLimit() bool {
return r.Header.Get(H_MEDIA_LIMIT) != ""
}
func (r APIResponse) MediaRateLimit() uint32 {
h := r.Header.Get(H_MEDIA_LIMIT)
i, _ := strconv.ParseUint(h, 10, 32)
return uint32(i)
}
func (r APIResponse) MediaRateLimitRemaining() uint32 {
h := r.Header.Get(H_MEDIA_LIMIT_REMAIN)
i, _ := strconv.ParseUint(h, 10, 32)
return uint32(i)
}
func (r APIResponse) MediaRateLimitReset() time.Time {
h := r.Header.Get(H_MEDIA_LIMIT_RESET)
i, _ := strconv.ParseUint(h, 10, 32)
t := time.Unix(int64(i), 0)
return t
}
// Parses a JSON encoded HTTP response into the supplied interface.
func (r APIResponse) Parse(out interface{}) (err error) {
var b []byte
switch r.StatusCode {
case STATUS_NOTFOUND:
fallthrough
case STATUS_GATEWAY:
fallthrough
case STATUS_INVALID:
e := &Errors{}
defer r.Body.Close()
if b, err = ioutil.ReadAll(r.Body); err == nil {
json.Unmarshal(b, e)
err = *e
}
return
case STATUS_LIMIT:
err = RateLimitError{
Limit: r.RateLimit(),
Remaining: r.RateLimitRemaining(),
Reset: r.RateLimitReset(),
}
return
}
defer r.Body.Close()
if b, err = ioutil.ReadAll(r.Body); err == nil {
err = json.Unmarshal(b, out)
}
if err == io.EOF {
err = nil
}
return
}
// It's a user!
type User map[string]interface{}
func (u User) Id() uint64 {
id, _ := strconv.ParseUint(u["id_str"].(string), 10, 64)
return id
}
func (u User) IdStr() string {
return u["id_str"].(string)
}
func (u User) Name() string {
return u["name"].(string)
}
func (u User) ScreenName() string {
return u["screen_name"].(string)
}
// It's a Tweet! (Adorably referred to by the API as a "status").
type Tweet map[string]interface{}
func (t Tweet) Id() (id uint64) {
var (
err error
src = t["id_str"].(string)
)
if id, err = strconv.ParseUint(src, 10, 64); err != nil {
panic(fmt.Sprintf("Could not parse ID: %v", err))
}
return
}
func (t Tweet) IdStr() string {
return t["id_str"].(string)
}
func (t Tweet) Text() string {
return t["text"].(string)
}
func (t Tweet) User() User {
return User(t["user"].(map[string]interface{}))
}
func (t Tweet) CreatedAt() (out time.Time) {
var (
err error
src = t["created_at"].(string)
)
if out, err = time.Parse(time.RubyDate, src); err != nil {
panic(fmt.Sprintf("Could not parse time: %v", err))
}
return
}
func (t Tweet) JSON() []byte {
return t["json"].([]byte)
}
// It's a structured list of Tweets!
type SearchResults map[string]interface{}
func (sr SearchResults) Statuses() []Tweet {
var a []interface{} = sr["statuses"].([]interface{})
b := make([]Tweet, len(a))
for i, v := range a {
b[i] = v.(map[string]interface{})
}
return b
}
// It's a less structured list of Tweets!
type Timeline []Tweet