mirror of
https://bitbucket.org/skobkin/point-tools-go.git
synced 2024-11-21 20:36:04 +00:00
Initial commit. Code separated from point-tools-crawler.
This commit is contained in:
commit
fac428f790
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/.idea/
|
6
api_error.go
Normal file
6
api_error.go
Normal file
|
@ -0,0 +1,6 @@
|
|||
package point_tools
|
||||
|
||||
type ApiError struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
63
client.go
Normal file
63
client.go
Normal 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
|
||||
}
|
16
receive_all_page_response.go
Normal file
16
receive_all_page_response.go
Normal 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"`
|
||||
}
|
Loading…
Reference in a new issue