-
Notifications
You must be signed in to change notification settings - Fork 0
/
devrant.go
145 lines (133 loc) · 3.69 KB
/
devrant.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
package devgorant
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
const (
API = "https://www.devrant.io/api"
APP_VERSION = 3
RANTS_PATH = "%s/devrant/rants?sort=%s&limit=%d&skip=%d&app=%d"
RANT_PATH = "%s/devrant/rants/%d?app=%d"
USER_PATH = "%s/users/%d?app=%d"
USER_ID_PATH = "%s/get-user-id?username=%s&app=%d"
SEARCH_PATH = "%s/devrant/search?term=%s&app=%d"
SURPRISE_PATH = "%s/devrant/rants/surprise?app=%d"
WEEKLY_PATH = "%s/devrant/weekly-rants?app=%d"
)
type Client struct {
}
// Fetches rants
func (c *Client) Rants(sort string, limit int, skip int) ([]RantModel, error) {
if sort == "" {
sort = "algo"
} else if (sort != "algo") && (sort != "recent") && (sort != "top") {
return nil, errors.New("Invalid string in sort method")
}
if limit <= 0 {
limit = 50
}
url := fmt.Sprintf(RANTS_PATH, API, sort, limit, skip, APP_VERSION)
res, err := http.Get(url)
if err != nil {
return nil, err
}
var data RantsResponse
json.NewDecoder(res.Body).Decode(&data)
if !data.Success && data.Error != "" {
return nil, errors.New(data.Error)
}
return data.Rants, nil
}
// Fetches a rant and its comments given a valid rant id
func (c *Client) Rant(rantId int) (RantModel, []CommentModel, error) {
url := fmt.Sprintf(RANT_PATH, API, rantId, APP_VERSION)
res, err := http.Get(url)
if err != nil {
return RantModel{}, nil, err
}
var data RantResponse
json.NewDecoder(res.Body).Decode(&data)
if !data.Success && data.Error != "" {
return RantModel{}, nil, errors.New(data.Error)
}
return data.Rant, data.Comments, nil
}
// Fetches ranter's profile data
func (c *Client) Profile(username string) (UserModel, ContentModel, error) {
userId, err := getUserId(username)
if err != nil {
return UserModel{}, ContentModel{}, err
}
url := fmt.Sprintf(USER_PATH, API, userId, APP_VERSION)
res, err := http.Get(url)
if err != nil {
return UserModel{}, ContentModel{}, err
}
var data UserResponse
json.NewDecoder(res.Body).Decode(&data)
if !data.Success && data.Error != "" {
return UserModel{}, ContentModel{}, errors.New(data.Error)
}
return data.Profile, data.Profile.Content.Content, nil
}
// Search for rants matching the search term
func (c *Client) Search(term string) ([]RantModel, error) {
url := fmt.Sprintf(SEARCH_PATH, API, term, APP_VERSION)
res, err := http.Get(url)
if err != nil {
return nil, err
}
var data SearchResponse
json.NewDecoder(res.Body).Decode(&data)
if !data.Success && data.Error != "" {
return nil, errors.New(data.Error)
}
return data.Rants, nil
}
// Returns a random rant
func (c *Client) Surprise() (RantModel, error) {
url := fmt.Sprintf(SURPRISE_PATH, API, APP_VERSION)
res, err := http.Get(url)
if err != nil {
return RantModel{}, err
}
var data RantResponse
json.NewDecoder(res.Body).Decode(&data)
if !data.Success && data.Error != "" {
return RantModel{}, errors.New(data.Error)
}
return data.Rant, nil
}
// Returns the rants tagged for 'weekly'
func (c *Client) WeeklyRants() ([]RantModel, error) {
url := fmt.Sprintf(WEEKLY_PATH, API, APP_VERSION)
res, err := http.Get(url)
if err != nil {
return nil, err
}
var data RantsResponse
json.NewDecoder(res.Body).Decode(&data)
if !data.Success && data.Error != "" {
return nil, errors.New(data.Error)
}
return data.Rants, nil
}
// Fetches the userId given a valid username
func getUserId(username string) (int, error) {
url := fmt.Sprintf(USER_ID_PATH, API, username, APP_VERSION)
res, err := http.Get(url)
if err != nil {
return 0, err
}
var data GetUserIdResponse
json.NewDecoder(res.Body).Decode(&data)
if !data.Success && data.Error != "" {
return 0, errors.New(data.Error)
}
return data.UserId, nil
}
func New() *Client {
return new(Client)
}