mirror of
https://bitbucket.org/skobkin/point-api-go.git
synced 2025-01-15 14:06:27 +00:00
GetPostWithComments() method implemented.
This commit is contained in:
parent
fc30e4b27f
commit
ae0722ce09
85
client.go
85
client.go
|
@ -11,6 +11,16 @@ import (
|
|||
"strconv"
|
||||
)
|
||||
|
||||
// Package errors
|
||||
var (
|
||||
ErrHttpRequest = errors.New("point-api: HTTP request error")
|
||||
ErrJsonDeserialization = errors.New("point-api: JSON deserialization error")
|
||||
ErrNotAuthorized = errors.New("point-api: Not authorized")
|
||||
ErrAuthNeeded = errors.New("point-api: Authentication needed")
|
||||
ErrNoNextPage = errors.New("point-api: Page has next_page: false")
|
||||
ErrAuthorPrivacy = errors.New("point-api: Post author privacy error")
|
||||
)
|
||||
|
||||
type PointClient struct {
|
||||
client simple_http.Client
|
||||
apiUrl string
|
||||
|
@ -43,8 +53,8 @@ func (c *PointClient) Login(login, password string) (Token, error) {
|
|||
jsonErr := json.Unmarshal(body, &token)
|
||||
|
||||
if jsonErr != nil {
|
||||
log.Fatal(jsonErr)
|
||||
return token, errors.New("JSON deserialization error")
|
||||
log.Println(jsonErr)
|
||||
return token, ErrJsonDeserialization
|
||||
}
|
||||
|
||||
if token.Error != "" {
|
||||
|
@ -56,13 +66,48 @@ func (c *PointClient) Login(login, password string) (Token, error) {
|
|||
return token, nil
|
||||
}
|
||||
|
||||
// GetPostWithComments gets post with provided id and comments in it
|
||||
func (c *PointClient) GetPostWithComments(id string) (PostShowResponse, error) {
|
||||
var response PostShowResponse
|
||||
|
||||
headers := map[string]string{}
|
||||
|
||||
if 0 != len(c.token.AuthToken) {
|
||||
headers["Authorization"] = c.token.AuthToken
|
||||
}
|
||||
|
||||
body, reqErr := c.client.MakeGetRequest(c.apiUrl+"post/"+id, nil, &headers)
|
||||
|
||||
if reqErr != nil {
|
||||
return response, ErrHttpRequest
|
||||
}
|
||||
|
||||
jsonErr := json.Unmarshal(body, &response)
|
||||
|
||||
if jsonErr != nil {
|
||||
log.Println(jsonErr)
|
||||
return response, ErrJsonDeserialization
|
||||
}
|
||||
|
||||
switch response.Error {
|
||||
case "NotAuthorized":
|
||||
log.Println("Please use correct login and password to fetch this post")
|
||||
return response, ErrNotAuthorized
|
||||
case "PostAuthorError":
|
||||
log.Println("Post author profile is private")
|
||||
return response, ErrAuthorPrivacy
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// GetRecentAllPostsPage gets most recent /all posts page
|
||||
func (c *PointClient) 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")
|
||||
if 0 != len(c.token.AuthToken) {
|
||||
log.Println("Can not get recent posts. Login first.")
|
||||
return page, ErrAuthNeeded
|
||||
}
|
||||
|
||||
headers := map[string]string{
|
||||
|
@ -72,20 +117,20 @@ func (c *PointClient) GetRecentAllPostsPage() (Page, error) {
|
|||
body, reqErr := c.client.MakeGetRequest(c.apiUrl+"all", nil, &headers)
|
||||
|
||||
if reqErr != nil {
|
||||
return page, errors.New("HTTP request error")
|
||||
return page, ErrHttpRequest
|
||||
}
|
||||
|
||||
jsonErr := json.Unmarshal(body, &page)
|
||||
|
||||
if jsonErr != nil {
|
||||
log.Fatal(jsonErr)
|
||||
return page, errors.New("JSON deserialization error")
|
||||
log.Println(jsonErr)
|
||||
return page, ErrJsonDeserialization
|
||||
}
|
||||
|
||||
// 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")
|
||||
log.Println("Please use correct login and password to fetch recent posts")
|
||||
return page, ErrNotAuthorized
|
||||
}
|
||||
|
||||
return page, nil
|
||||
|
@ -96,8 +141,8 @@ func (c *PointClient) GetNextAllPostsPageBeforeUid(uid int) (Page, error) {
|
|||
var nextPage Page
|
||||
|
||||
if len(c.token.AuthToken) == 0 {
|
||||
log.Fatal("Can not get recent posts. Login first.")
|
||||
return nextPage, errors.New("Login first")
|
||||
log.Println("Can not get recent posts. Login first.")
|
||||
return nextPage, ErrAuthNeeded
|
||||
}
|
||||
|
||||
data := url.Values{}
|
||||
|
@ -110,20 +155,20 @@ func (c *PointClient) GetNextAllPostsPageBeforeUid(uid int) (Page, error) {
|
|||
body, req_err := c.client.MakeGetRequest(c.apiUrl+"all", &data, &headers)
|
||||
|
||||
if req_err != nil {
|
||||
return nextPage, errors.New("HTTP request error")
|
||||
return nextPage, ErrHttpRequest
|
||||
}
|
||||
|
||||
json_err := json.Unmarshal(body, &nextPage)
|
||||
|
||||
if json_err != nil {
|
||||
log.Fatal(json_err)
|
||||
return nextPage, errors.New("JSON deserialization error")
|
||||
log.Println(json_err)
|
||||
return nextPage, ErrJsonDeserialization
|
||||
}
|
||||
|
||||
// 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")
|
||||
log.Println("Please use correct login and password to fetch recent posts")
|
||||
return nextPage, ErrNotAuthorized
|
||||
}
|
||||
|
||||
return nextPage, nil
|
||||
|
@ -134,12 +179,12 @@ func (c *PointClient) GetNextAllPostsPage(page Page) (Page, error) {
|
|||
var nextPage Page
|
||||
|
||||
if len(page.Posts) == 0 {
|
||||
return nextPage, errors.New("Page must have has_next=true and some posts")
|
||||
return nextPage, ErrNoNextPage
|
||||
}
|
||||
|
||||
if len(c.token.AuthToken) == 0 {
|
||||
log.Fatal("Can not get recent posts. Login first.")
|
||||
return nextPage, errors.New("Login first")
|
||||
log.Println("Can not get recent posts. Login first.")
|
||||
return nextPage, ErrAuthNeeded
|
||||
}
|
||||
|
||||
nextPage, err := c.GetNextAllPostsPageBeforeUid(page.Posts[len(page.Posts)-1].Uid)
|
||||
|
|
13
comment.go
Normal file
13
comment.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package point
|
||||
|
||||
// Post represents the post comment data
|
||||
type Comment struct {
|
||||
PostId string `json:"post_id"`
|
||||
Number int `json:"id"`
|
||||
ToNumber int `json:"to_comment_id"`
|
||||
IsRec bool `json:"is_rec"`
|
||||
Author User `json:"author"`
|
||||
Text string `json:"text"`
|
||||
Files []string `json:"files"`
|
||||
Created string `json:"created"`
|
||||
}
|
8
post_show_response.go
Normal file
8
post_show_response.go
Normal file
|
@ -0,0 +1,8 @@
|
|||
package point
|
||||
|
||||
// PostShowResponse represents /api/post response including post data and comments
|
||||
type PostShowResponse struct {
|
||||
Post Post `json:"post"`
|
||||
Comments []Comment `json:"comments"`
|
||||
Error string `json:"error"`
|
||||
}
|
Loading…
Reference in a new issue