-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestions.go
49 lines (43 loc) · 1.11 KB
/
questions.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
package GoStack
import "net/http"
import "io/ioutil"
import "encoding/json"
import "strconv"
//GetQuestions gets all the questions from a given site
func GetQuestions(site string) ([]Question, error) {
resp, err := http.Get("https://api.stackexchange.com/2.2/questions?filter=withbody&site=" + site)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if err = checkError(body); err != nil {
return nil, err
}
var questionResponse QuestionResponse
err = json.Unmarshal(body, &questionResponse)
if err != nil {
return nil, err
}
return questionResponse.Items, nil
}
//GetAnswers returns the relevant answers to a question
func (q *Question) GetAnswers() ([]Answer, error) {
url := "https://api.stackexchange.com/2.2/questions/" + strconv.Itoa(q.QuestionID) + "/answers"
resp, err := http.Get(url)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var aR AnswerResponse
if err = checkError(body); err != nil {
return nil, err
}
json.Unmarshal(body, &aR)
return aR.Items, nil
}