Initial commit. Code separated from point-tools-crawler.

This commit is contained in:
Alexey Skobkin 2016-03-29 10:28:54 +03:00
commit fac428f790
4 changed files with 86 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/.idea/

6
api_error.go Normal file
View file

@ -0,0 +1,6 @@
package point_tools
type ApiError struct {
Code int `json:"code"`
Message string `json:"message"`
}

63
client.go Normal file
View file

@ -0,0 +1,63 @@
package point_tools
import (
"bitbucket.org/skobkin/point-api-go"
"bitbucket.org/skobkin/dumb-http-go"
"encoding/json"
"log"
"net/http"
"net/url"
)
type PointToolsClient struct {
client simple_http.Client
apiUrl string
token string
}
func New(apiUrl, token string) PointToolsClient {
return PointToolsClient{
simple_http.Client{http.Client{}},
apiUrl,
token,
}
}
func GetPageJSON(page point.Page) (string, error) {
b, err := json.Marshal(page)
if err != nil {
log.Println("point.Page serialize error:", err)
return "", err
}
return string(b[:]), nil
}
func (c *PointToolsClient) SendPage(page point.Page) (ReceiveAllPageResponse, error) {
var response ReceiveAllPageResponse
jsonStr, err := GetPageJSON(page)
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, reqErr
}
jsonErr := json.Unmarshal(body, &response)
if jsonErr != nil {
return response, jsonErr
}
return response, nil
}

View file

@ -0,0 +1,16 @@
package point_tools
const (
STATUS_SUCCESS string = "success"
STATUS_FAILURE string = "fail"
)
type ReceiveAllPageResponse struct {
Status string `json:"status"`
Data ReceiveAllPageData `json:"data"`
Error ApiError `json:"error"`
}
type ReceiveAllPageData struct {
Continue bool `json:"continue"`
}