SendPostWithComments() method implemented. Some refactoring done.

This commit is contained in:
Alexey Skobkin 2016-03-30 06:00:29 +03:00
parent 9ba6534965
commit bb97206d98
3 changed files with 59 additions and 6 deletions

View file

@ -11,10 +11,16 @@ import (
"net/url"
)
const (
STATUS_SUCCESS string = "success"
STATUS_FAILURE string = "fail"
)
// Package errors
var (
ErrHttpRequest = errors.New("point-api: HTTP request error")
ErrJsonDeserialization = errors.New("point-api: JSON deserialization error")
ErrJsonSerialization = errors.New("point-api: JSON serialization error")
)
type PointToolsClient struct {
@ -37,7 +43,18 @@ func GetPageJSON(page point.Page) (string, error) {
if err != nil {
log.Println("point.Page serialize error:", err)
return "", err
return "", ErrJsonSerialization
}
return string(b[:]), nil
}
func GetPostWithCommentsJSON(post point.PostShowResponse) (string, error) {
b, err := json.Marshal(post)
if err != nil {
log.Println("point.Page serialize error:", err)
return "", ErrJsonSerialization
}
return string(b[:]), nil
@ -71,3 +88,32 @@ func (c *PointToolsClient) SendAllPage(page point.Page) (SendAllPageResponse, er
return response, nil
}
// SendPostWithComments sends post with comments to Point Tools crawler API gate for processing
func (c *PointToolsClient) SendPostWithComments(post point.PostShowResponse) (SendPostWithCommentsResponse, error) {
var response SendPostWithCommentsResponse
jsonStr, err := GetPostWithCommentsJSON(post)
if err != nil {
return response, err
}
data := url.Values{}
data.Set("token", c.token)
data.Add("json", jsonStr)
body, reqErr := c.client.MakePostRequest(c.apiUrl+"all/page", data, nil)
if reqErr != nil {
return response, ErrHttpRequest
}
jsonErr := json.Unmarshal(body, &response)
if jsonErr != nil {
return response, ErrJsonDeserialization
}
return response, nil
}

View file

@ -1,10 +1,5 @@
package point_tools
const (
STATUS_SUCCESS string = "success"
STATUS_FAILURE string = "fail"
)
type SendAllPageResponse struct {
Status string `json:"status"`
Data SendAllPageResponseData `json:"data"`

View file

@ -0,0 +1,12 @@
package point_tools
type SendPostWithCommentsResponse struct {
Status string `json:"status"`
Data SendPostWithCommentsResponseData `json:"data"`
Error ApiError `json:"error"`
}
type SendPostWithCommentsResponseData struct {
Continue bool `json:"continue"`
Next string `json:"next"`
}