package main import ( "encoding/json" "fmt" "net/http" "net/url" "time" ) type RDramaClient struct { baseURL string apiKey string } func NewRDramaClient(baseURL, apiKey string) *RDramaClient { return &RDramaClient{ baseURL: baseURL, apiKey: apiKey, } } func (c *RDramaClient) makeRequest(method, path string, v interface{}) error { url := c.baseURL + path req, err := http.NewRequest(method, url, nil) if err != nil { return err } req.Header.Add("Authorization", fmt.Sprintf("%s", c.apiKey)) client := http.Client{} resp, err := client.Do(req) if err != nil { return err } defer resp.Body.Close() if !(resp.StatusCode >= 200 && resp.StatusCode < 300) { return fmt.Errorf("unexpected status code: %d", resp.StatusCode) } return json.NewDecoder(resp.Body).Decode(v) } type CommentResponse struct { Data []Comment `json:"data"` } type Comment struct { ID int `json:"id"` Author string `json:"author_name"` Text string `json:"body"` } type SearchParams struct { Hole string After time.Time Before time.Time } func (c *RDramaClient) GetComments(params SearchParams) ([]Comment, error) { var s string if params.Hole != "" { s += fmt.Sprintf("hole:%s", params.Hole) } nullTime := time.Time{} if params.After != nullTime { s += fmt.Sprintf(" after:%d", params.After.Unix()) } if params.Before != nullTime { s += fmt.Sprintf(" before:%d", params.Before.Unix()) } v := url.Values{} v.Set("q", s) url := "/search/comments?" + v.Encode() fmt.Printf("URL: %s\n", url) var comments CommentResponse if err := c.makeRequest("GET", url, &comments); err != nil { return nil, err } return comments.Data, nil }