2016-03-15 22:21:50 +00:00
|
|
|
package point
|
|
|
|
|
|
|
|
import (
|
2016-03-20 22:06:34 +00:00
|
|
|
"bitbucket.org/skobkin/point-tools-crawler/simple_http"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2016-03-15 22:21:50 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2016-03-15 22:56:51 +00:00
|
|
|
"strconv"
|
2016-03-15 22:21:50 +00:00
|
|
|
)
|
|
|
|
|
2016-03-20 22:06:34 +00:00
|
|
|
type PointClient struct {
|
|
|
|
client simple_http.Client
|
|
|
|
apiUrl string
|
|
|
|
token Token
|
2016-03-15 22:21:50 +00:00
|
|
|
}
|
|
|
|
|
2016-03-20 22:06:34 +00:00
|
|
|
func NewClient(apiUrl string) PointClient {
|
|
|
|
return PointClient{
|
|
|
|
simple_http.Client{http.Client{}},
|
|
|
|
apiUrl,
|
|
|
|
Token{},
|
2016-03-15 22:21:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 22:06:34 +00:00
|
|
|
func (c *PointClient) Login(login, password string) (Token, error) {
|
2016-03-15 22:21:50 +00:00
|
|
|
var token Token
|
|
|
|
|
|
|
|
data := url.Values{}
|
2016-03-20 22:06:34 +00:00
|
|
|
data.Set("login", login)
|
|
|
|
data.Set("password", password)
|
2016-03-15 22:21:50 +00:00
|
|
|
|
2016-03-20 22:06:34 +00:00
|
|
|
body, req_err := c.client.MakePostRequest(c.apiUrl+"login", data, nil)
|
2016-03-15 22:21:50 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-03-20 22:06:34 +00:00
|
|
|
func (c *PointClient) GetRecentAllPostsPage() (Page, error) {
|
2016-03-15 22:21:50 +00:00
|
|
|
var page Page
|
|
|
|
|
|
|
|
if len(c.token.AuthToken) == 0 {
|
|
|
|
log.Fatal("Can not get recent posts. Login first.")
|
|
|
|
return page, errors.New("Login first")
|
|
|
|
}
|
|
|
|
|
2016-03-20 22:06:34 +00:00
|
|
|
headers := map[string]string{
|
|
|
|
"Authorization": c.token.AuthToken,
|
|
|
|
}
|
|
|
|
|
|
|
|
body, req_err := c.client.MakeGetRequest(c.apiUrl+"all", nil, &headers)
|
2016-03-15 22:21:50 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-03-20 22:06:34 +00:00
|
|
|
func (c *PointClient) GetNextAllPostsPage(page Page) (Page, error) {
|
2016-03-15 22:21:50 +00:00
|
|
|
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{}
|
2016-03-20 22:06:34 +00:00
|
|
|
data.Set("before", strconv.Itoa(page.Posts[len(page.Posts)-1].Uid))
|
2016-03-15 22:21:50 +00:00
|
|
|
|
2016-03-20 22:06:34 +00:00
|
|
|
headers := map[string]string{
|
|
|
|
"Authorization": c.token.AuthToken,
|
|
|
|
}
|
|
|
|
|
|
|
|
body, req_err := c.client.MakeGetRequest(c.apiUrl+"all", &data, &headers)
|
2016-03-15 22:21:50 +00:00
|
|
|
|
|
|
|
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
|
2016-03-20 22:06:34 +00:00
|
|
|
}
|