point-tools-crawler/point/client.go
2016-03-16 01:56:51 +03:00

193 lines
3.7 KiB
Go

package point
import (
"log"
"net/http"
"net/url"
"io/ioutil"
"encoding/json"
"errors"
"strings"
"strconv"
)
const point_api_url string = "https://point.im/api/"
type Client struct {
token Token
}
func (c *Client) getResponseBody(resp *http.Response) ([]byte, error) {
body, read_err := ioutil.ReadAll(resp.Body)
if read_err != nil {
log.Fatal(read_err)
return nil, read_err
}
return body, nil
}
func (c *Client) MakeGetRequest(url string, params *url.Values) ([]byte, error) {
var body []byte
if params != nil {
url += "?" + params.Encode()
}
req, req_err := http.NewRequest("GET", url, nil)
if req_err != nil {
log.Fatal(req_err)
return body, req_err
}
if len(c.token.AuthToken) > 0 {
req.Header.Set("Authorization", c.token.AuthToken)
}
hc := &http.Client{}
resp, resp_err := hc.Do(req)
if resp_err != nil {
log.Fatal(resp_err)
return nil, resp_err
}
defer resp.Body.Close()
body, read_err := c.getResponseBody(resp)
if read_err != nil {
return nil, read_err
}
return body, nil
}
func (c *Client) MakePostRequest(url string, params url.Values) ([]byte, error) {
req, req_err := http.NewRequest("POST", url, strings.NewReader(params.Encode()))
if req_err != nil {
log.Fatal(req_err)
return nil, req_err
}
hc := &http.Client{}
if len(c.token.AuthToken) > 0 {
req.Header.Set("Authorization", c.token.AuthToken)
}
resp, resp_err := hc.Do(req)
if resp_err != nil {
log.Fatal(resp_err)
return nil, resp_err
}
body, read_err := c.getResponseBody(resp)
if read_err != nil {
return nil, read_err
}
return body, nil
}
func (c *Client) Login(login, password string) (Token, error) {
var token Token
data := url.Values{}
data.Add("login", login)
data.Add("password", password)
body, req_err := c.MakePostRequest(point_api_url + "login", data)
if req_err != nil {
return token, req_err
}
json_err := json.Unmarshal(body, &token)
if json_err != nil {
log.Fatal(json_err)
return token, errors.New("JSON deserialization error")
}
if token.Error != "" {
return token, errors.New(token.Error)
}
c.token = token
return token, nil
}
func (c *Client) GetRecentAllPostsPage() (Page, error) {
var page Page
if len(c.token.AuthToken) == 0 {
log.Fatal("Can not get recent posts. Login first.")
return page, errors.New("Login first")
}
body, req_err := c.MakeGetRequest(point_api_url + "all", nil)
if req_err != nil {
return page, errors.New("HTTP request error")
}
json_err := json.Unmarshal(body, &page)
if json_err != nil {
log.Fatal(json_err)
return page, errors.New("JSON deserialization error")
}
// Move page.Error to response object
if page.Error == "NotAuthorized" {
log.Fatal("Please use correct login and password to fetch recent posts")
return page, errors.New("Not authorized")
}
return page, nil
}
func (c *Client) GetNextAllPostsPage(page Page) (Page, error) {
var nextPage Page
if false == page.HasNext || len(page.Posts) == 0 {
return nextPage, errors.New("Page must have has_next=true and some posts")
}
if len(c.token.AuthToken) == 0 {
log.Fatal("Can not get recent posts. Login first.")
return nextPage, errors.New("Login first")
}
data := url.Values{}
data.Add("before", strconv.Itoa(page.Posts[len(page.Posts)-1].Uid))
body, req_err := c.MakeGetRequest(point_api_url + "all", &data)
if req_err != nil {
return nextPage, errors.New("HTTP request error")
}
json_err := json.Unmarshal(body, &nextPage)
if json_err != nil {
log.Fatal(json_err)
return nextPage, errors.New("JSON deserialization error")
}
// Move page.Error to response object
if nextPage.Error == "NotAuthorized" {
log.Fatal("Please use correct login and password to fetch recent posts")
return nextPage, errors.New("Not authorized")
}
return nextPage, nil
}