-
Notifications
You must be signed in to change notification settings - Fork 1
/
goRant.go
197 lines (182 loc) · 5.05 KB
/
goRant.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
//Package goRant implements a API-wrapper
//for the public API of http://www.devrant.io
package goRant
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
)
//All Constants are written in uppercase, and have no need to be exported
const (
API = "https://www.devrant.io/api"
VERSION = 3
RANTS = "%s/devrant/rants?sort=%s&limit=%d&skip=%d&app=%d"
RANT = "%s/devrant/rants/%d?app=%d"
USERID = "%s/get-user-id?username=%s&app=%d"
USER = "%s/users/%d?app=%d"
SEARCH = "%s/devrant/search?term=%s&app=%d"
SURPRISE = "%s/devrant/rants/surprise?app=%d"
WEEKLY = "%s/devrant/weekly-rants?app=%d"
COLLABS = "%s/devrant/collabs?app=%d"
STORIES = "%s/devrant/story-rants?app=%d"
)
//Client in an empty interface
// It is has all functionalitios of goRant Wrapper
type Client struct {
}
//Rants return an array of Rants from devRant.io
//
//It has 3 parameters: Sort, Limit, Skip
// Sort can take 3 values, "algo", "recent", "top"
// Limit is an int value, used to specify number of rants to get
// Skip is an int value, used to specify number of rants to skip
// Example:
// If you eant the recent 20 rants: Rants("recent",20,0)
// If you want the next 20 rants: Rants("recent",20,20)
func (c *Client) Rants(sort string, limit int, skip int) ([]Rant, error) {
if sort == "" {
sort = "algo"
} else if (sort != "algo") && (sort != "recent") && (sort != "top") {
return nil, errors.New("Invalid sort method")
}
if limit <= 0 {
limit = 50
}
url := fmt.Sprintf(RANTS, API, sort, limit, skip, VERSION)
res, err := http.Get(url)
if err != nil {
log.Fatalln("error", err)
}
var data Rants
json.NewDecoder(res.Body).Decode(&data)
if !data.Success && data.Error != "" {
return nil, errors.New(data.Error)
}
return data.Rants, nil
}
//GetRant returns a singel Rant
// Just pass a rant-id as parameter
func (c *Client) GetRant(id int) (Rant, []Comment, error) {
url := fmt.Sprintf(RANT, API, id, VERSION)
res, err := http.Get(url)
if err != nil {
return Rant{}, nil, err
}
var data RantRes
json.NewDecoder(res.Body).Decode(&data)
if !data.Success && data.Error != "" {
return Rant{}, nil, errors.New(data.Error)
}
return data.Rant, data.Comments, nil
}
//Profile returns a User profile info
// It takes a username of type string as input
func (c *Client) Profile(username string) (User, error) {
id, err := getUserID(username)
if err != nil {
return User{}, err
}
//fmt.Println("id: ", id)
url := fmt.Sprintf(USER, API, id, VERSION)
res, err := http.Get(url)
if err != nil {
return User{}, err
}
var data UserRes
json.NewDecoder(res.Body).Decode(&data)
if !data.Success && data.Error != "" {
return User{}, errors.New(data.Error)
}
return data.Profile, nil
}
//Used to get userID from a given name
// Only used internally for Profile func
func getUserID(username string) (int, error) {
url := fmt.Sprintf(USERID, API, username, VERSION)
res, err := http.Get(url)
if err != nil {
return 0, err
}
var userID UserIDRes
json.NewDecoder(res.Body).Decode(&userID)
if !userID.Success && userID.Error != "" {
return 0, errors.New(userID.Error)
}
return userID.UserID, nil
}
//Search returns array of Rants as the search result
func (c *Client) Search(query string) ([]Rant, error) {
url := fmt.Sprintf(SEARCH, API, query, VERSION)
res, err := http.Get(url)
if err != nil {
return nil, err
}
var data SearchRes
json.NewDecoder(res.Body).Decode(&data)
if !data.Success && data.Error != "" {
return nil, errors.New(data.Error)
}
return data.Rants, nil
}
//Surprise returns a random Rant
func (c *Client) Surprise() (Rant, []Comment, error) {
url := fmt.Sprintf(SURPRISE, API, VERSION)
res, err := http.Get(url)
if err != nil {
return Rant{}, nil, nil
}
var data RantRes
json.NewDecoder(res.Body).Decode(&data)
if !data.Success && data.Error != "" {
return Rant{}, nil, errors.New(data.Error)
}
return data.Rant, data.Comments, nil
}
//WeeklyRant return an array of Rants from weekly rants tab of devRant
func (c *Client) WeeklyRant() ([]Rant, error) {
url := fmt.Sprintf(WEEKLY, API, VERSION)
res, err := http.Get(url)
if err != nil {
return nil, err
}
var data Rants
json.NewDecoder(res.Body).Decode(&data)
if !data.Success && data.Error != "" {
return nil, errors.New(data.Error)
}
return data.Rants, nil
}
//Collabs return an array of Rants from the collabs tob of devRant
func (c *Client) Collabs() ([]Rant, error) {
url := fmt.Sprintf(COLLABS, API, VERSION)
res, err := http.Get(url)
if err != nil {
return nil, err
}
var data Rants
json.NewDecoder(res.Body).Decode(&data)
if !data.Success && data.Error != "" {
return nil, errors.New(data.Error)
}
return data.Rants, nil
}
//Stories return an array of Rants from stories tab of devRant
func (c *Client) Stories() ([]Rant, error) {
url := fmt.Sprintf(STORIES, API, VERSION)
res, err := http.Get(url)
if err != nil {
return nil, err
}
var data Rants
json.NewDecoder(res.Body).Decode(&data)
if !data.Success && data.Error != "" {
return nil, errors.New(data.Error)
}
return data.Rants, nil
}
//New returns a new Client
func New() *Client {
return new(Client)
}