GetPostWithComments() method implemented.
This commit is contained in:
parent
fc30e4b27f
commit
ae0722ce09
85
client.go
85
client.go
|
@ -11,6 +11,16 @@ import (
|
||||||
"strconv"
|
"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 {
|
type PointClient struct {
|
||||||
client simple_http.Client
|
client simple_http.Client
|
||||||
apiUrl string
|
apiUrl string
|
||||||
|
@ -43,8 +53,8 @@ func (c *PointClient) Login(login, password string) (Token, error) {
|
||||||
jsonErr := json.Unmarshal(body, &token)
|
jsonErr := json.Unmarshal(body, &token)
|
||||||
|
|
||||||
if jsonErr != nil {
|
if jsonErr != nil {
|
||||||
log.Fatal(jsonErr)
|
log.Println(jsonErr)
|
||||||
return token, errors.New("JSON deserialization error")
|
return token, ErrJsonDeserialization
|
||||||
}
|
}
|
||||||
|
|
||||||
if token.Error != "" {
|
if token.Error != "" {
|
||||||
|
@ -56,13 +66,48 @@ func (c *PointClient) Login(login, password string) (Token, error) {
|
||||||
return token, nil
|
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
|
// GetRecentAllPostsPage gets most recent /all posts page
|
||||||
func (c *PointClient) GetRecentAllPostsPage() (Page, error) {
|
func (c *PointClient) GetRecentAllPostsPage() (Page, error) {
|
||||||
var page Page
|
var page Page
|
||||||
|
|
||||||
if len(c.token.AuthToken) == 0 {
|
if 0 != len(c.token.AuthToken) {
|
||||||
log.Fatal("Can not get recent posts. Login first.")
|
log.Println("Can not get recent posts. Login first.")
|
||||||
return page, errors.New("Login first")
|
return page, ErrAuthNeeded
|
||||||
}
|
}
|
||||||
|
|
||||||
headers := map[string]string{
|
headers := map[string]string{
|
||||||
|
@ -72,20 +117,20 @@ func (c *PointClient) GetRecentAllPostsPage() (Page, error) {
|
||||||
body, reqErr := c.client.MakeGetRequest(c.apiUrl+"all", nil, &headers)
|
body, reqErr := c.client.MakeGetRequest(c.apiUrl+"all", nil, &headers)
|
||||||
|
|
||||||
if reqErr != nil {
|
if reqErr != nil {
|
||||||
return page, errors.New("HTTP request error")
|
return page, ErrHttpRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonErr := json.Unmarshal(body, &page)
|
jsonErr := json.Unmarshal(body, &page)
|
||||||
|
|
||||||
if jsonErr != nil {
|
if jsonErr != nil {
|
||||||
log.Fatal(jsonErr)
|
log.Println(jsonErr)
|
||||||
return page, errors.New("JSON deserialization error")
|
return page, ErrJsonDeserialization
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move page.Error to response object
|
// Move page.Error to response object
|
||||||
if page.Error == "NotAuthorized" {
|
if page.Error == "NotAuthorized" {
|
||||||
log.Fatal("Please use correct login and password to fetch recent posts")
|
log.Println("Please use correct login and password to fetch recent posts")
|
||||||
return page, errors.New("Not authorized")
|
return page, ErrNotAuthorized
|
||||||
}
|
}
|
||||||
|
|
||||||
return page, nil
|
return page, nil
|
||||||
|
@ -96,8 +141,8 @@ func (c *PointClient) GetNextAllPostsPageBeforeUid(uid int) (Page, error) {
|
||||||
var nextPage Page
|
var nextPage Page
|
||||||
|
|
||||||
if len(c.token.AuthToken) == 0 {
|
if len(c.token.AuthToken) == 0 {
|
||||||
log.Fatal("Can not get recent posts. Login first.")
|
log.Println("Can not get recent posts. Login first.")
|
||||||
return nextPage, errors.New("Login first")
|
return nextPage, ErrAuthNeeded
|
||||||
}
|
}
|
||||||
|
|
||||||
data := url.Values{}
|
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)
|
body, req_err := c.client.MakeGetRequest(c.apiUrl+"all", &data, &headers)
|
||||||
|
|
||||||
if req_err != nil {
|
if req_err != nil {
|
||||||
return nextPage, errors.New("HTTP request error")
|
return nextPage, ErrHttpRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
json_err := json.Unmarshal(body, &nextPage)
|
json_err := json.Unmarshal(body, &nextPage)
|
||||||
|
|
||||||
if json_err != nil {
|
if json_err != nil {
|
||||||
log.Fatal(json_err)
|
log.Println(json_err)
|
||||||
return nextPage, errors.New("JSON deserialization error")
|
return nextPage, ErrJsonDeserialization
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move page.Error to response object
|
// Move page.Error to response object
|
||||||
if nextPage.Error == "NotAuthorized" {
|
if nextPage.Error == "NotAuthorized" {
|
||||||
log.Fatal("Please use correct login and password to fetch recent posts")
|
log.Println("Please use correct login and password to fetch recent posts")
|
||||||
return nextPage, errors.New("Not authorized")
|
return nextPage, ErrNotAuthorized
|
||||||
}
|
}
|
||||||
|
|
||||||
return nextPage, nil
|
return nextPage, nil
|
||||||
|
@ -134,12 +179,12 @@ func (c *PointClient) GetNextAllPostsPage(page Page) (Page, error) {
|
||||||
var nextPage Page
|
var nextPage Page
|
||||||
|
|
||||||
if len(page.Posts) == 0 {
|
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 {
|
if len(c.token.AuthToken) == 0 {
|
||||||
log.Fatal("Can not get recent posts. Login first.")
|
log.Println("Can not get recent posts. Login first.")
|
||||||
return nextPage, errors.New("Login first")
|
return nextPage, ErrAuthNeeded
|
||||||
}
|
}
|
||||||
|
|
||||||
nextPage, err := c.GetNextAllPostsPageBeforeUid(page.Posts[len(page.Posts)-1].Uid)
|
nextPage, err := c.GetNextAllPostsPageBeforeUid(page.Posts[len(page.Posts)-1].Uid)
|
||||||
|
|
|
@ -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"`
|
||||||
|
}
|
|
@ -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 New Issue