170 lines
3.6 KiB
Go
170 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
neturl "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, body string, response interface{}) error {
|
|
url := c.baseURL + path
|
|
var reader io.Reader
|
|
|
|
reader = bytes.NewBufferString(body)
|
|
|
|
req, err := http.NewRequest(method, url, reader)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Add("Authorization", fmt.Sprintf("%s", c.apiKey))
|
|
if body != "" {
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
}
|
|
client := http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
|
|
fmt.Println("Status code: ", resp.StatusCode)
|
|
fmt.Println(method, ": ", url)
|
|
fmt.Println(body)
|
|
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
|
|
}
|
|
|
|
if response != nil {
|
|
return json.NewDecoder(resp.Body).Decode(response)
|
|
}
|
|
// fmt.Println("reponse: ")
|
|
// _, err = io.Copy(os.Stdout, resp.Body)
|
|
//if err != nil {
|
|
// fmt.Println("Error copyng response body: ", err)
|
|
// handle the error
|
|
//}
|
|
return nil
|
|
}
|
|
|
|
type CommentResponse struct {
|
|
Data []Comment `json:"data"`
|
|
}
|
|
|
|
type PostResponse struct {
|
|
Data []Post `json:"data"`
|
|
}
|
|
|
|
type Comment struct {
|
|
ID int `json:"id"`
|
|
Author Author `json:"author"`
|
|
AuthorName string `json:"author_name"`
|
|
Text string `json:"body"`
|
|
}
|
|
|
|
type Author struct {
|
|
ID int `json:id`
|
|
}
|
|
|
|
type Post struct {
|
|
ID int `json:"id"`
|
|
Author Author `json:"author"`
|
|
AuthorName string `json:"author_name"`
|
|
Text string `json:"body"`
|
|
Title string `json:"title"`
|
|
}
|
|
|
|
type CommentPost struct {
|
|
ParentID string `json:parent_fullname`
|
|
Body string `json:body`
|
|
}
|
|
|
|
type SearchParams struct {
|
|
Hole string
|
|
After time.Time
|
|
Before time.Time
|
|
}
|
|
|
|
func (c *RDramaClient) UpvotePost(id int) error {
|
|
url := fmt.Sprintf("/vote/post/%v/1", id)
|
|
return c.makeRequest("POST", url, "", nil)
|
|
|
|
}
|
|
|
|
func (c *RDramaClient) UpvoteComment(id int) error {
|
|
url := fmt.Sprintf("/vote/comment/%v/1", id)
|
|
return c.makeRequest("POST", url, "", nil)
|
|
|
|
}
|
|
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 := neturl.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
|
|
}
|
|
|
|
func (c *RDramaClient) GetPosts(params SearchParams) ([]Post, 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 := neturl.Values{}
|
|
v.Set("q", s)
|
|
url := "/search/posts?" + v.Encode()
|
|
fmt.Printf("URL: %s\n", url)
|
|
var posts PostResponse
|
|
if err := c.makeRequest("GET", url, "", &posts); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return posts.Data, nil
|
|
}
|
|
|
|
func (c *RDramaClient) PostComment(body string, parent string) error {
|
|
v := neturl.Values{}
|
|
v.Set("parent_fullname", parent)
|
|
v.Set("body", body)
|
|
fmt.Println(v.Encode())
|
|
return c.makeRequest("POST", "/comment", v.Encode(), nil)
|
|
|
|
}
|