mirror of
https://github.com/mrusme/neonmodem.git
synced 2025-02-02 15:07:59 -05:00
Refactored response, implemented topics
This commit is contained in:
parent
d203f0a112
commit
d6436021f1
@ -82,6 +82,7 @@ var rootCmd = &cobra.Command{
|
|||||||
posts, err := (*c.Systems[0]).ListPosts()
|
posts, err := (*c.Systems[0]).ListPosts()
|
||||||
fmt.Println("-----------------------")
|
fmt.Println("-----------------------")
|
||||||
fmt.Printf("%v\n", posts)
|
fmt.Printf("%v\n", posts)
|
||||||
|
fmt.Printf("%v\n", err)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
|
||||||
tui := tea.NewProgram(ui.NewModel(&c), tea.WithAltScreen())
|
tui := tea.NewProgram(ui.NewModel(&c), tea.WithAltScreen())
|
||||||
|
@ -13,32 +13,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
Type string `json:"type"`
|
|
||||||
Timestamp int64 `json:"timestamp"`
|
|
||||||
Message string `json:"message"`
|
|
||||||
Validation map[string]interface{} `json:"validation,omitempty"`
|
|
||||||
|
|
||||||
Post PostModel `json:"post,omitempty"`
|
Post PostModel `json:"post,omitempty"`
|
||||||
Posts []PostModel `json:"latest_posts,omitempty"`
|
Posts []PostModel `json:"latest_posts,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RequestError struct {
|
type RequestError struct {
|
||||||
Response Response
|
Err error
|
||||||
Err error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (re *RequestError) Error() string {
|
func (re *RequestError) Error() string {
|
||||||
return re.Err.Error()
|
return re.Err.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (re *RequestError) Type() string {
|
|
||||||
return re.Response.Type
|
|
||||||
}
|
|
||||||
|
|
||||||
func (re *RequestError) Message() string {
|
|
||||||
return re.Response.Message
|
|
||||||
}
|
|
||||||
|
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
Debug(args ...interface{})
|
Debug(args ...interface{})
|
||||||
Debugf(format string, args ...interface{})
|
Debugf(format string, args ...interface{})
|
||||||
@ -82,7 +68,9 @@ type Client struct {
|
|||||||
endpoint *url.URL
|
endpoint *url.URL
|
||||||
credentials map[string]string
|
credentials map[string]string
|
||||||
logger Logger
|
logger Logger
|
||||||
Posts PostsService
|
|
||||||
|
Posts PostsService
|
||||||
|
Topics TopicsService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDefaultClientConfig(
|
func NewDefaultClientConfig(
|
||||||
@ -111,6 +99,7 @@ func NewClient(cc *ClientConfig) *Client {
|
|||||||
c.credentials = cc.Credentials
|
c.credentials = cc.Credentials
|
||||||
|
|
||||||
c.Posts = &PostServiceHandler{client: c}
|
c.Posts = &PostServiceHandler{client: c}
|
||||||
|
c.Topics = &TopicServiceHandler{client: c}
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
@ -156,7 +145,7 @@ func (c *Client) NewRequest(
|
|||||||
func (c *Client) Do(
|
func (c *Client) Do(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
req *http.Request,
|
req *http.Request,
|
||||||
content *Response,
|
content interface{},
|
||||||
) error {
|
) error {
|
||||||
var rreq *retryablehttp.Request
|
var rreq *retryablehttp.Request
|
||||||
var res *http.Response
|
var res *http.Response
|
||||||
@ -186,8 +175,7 @@ func (c *Client) Do(
|
|||||||
if res.StatusCode < http.StatusOK ||
|
if res.StatusCode < http.StatusOK ||
|
||||||
res.StatusCode > http.StatusNoContent {
|
res.StatusCode > http.StatusNoContent {
|
||||||
return &RequestError{
|
return &RequestError{
|
||||||
Err: errors.New("Non-2xx status code"),
|
Err: errors.New("Non-2xx status code"),
|
||||||
Response: *content,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,18 +62,18 @@ func (sys *System) ListPosts() ([]post.Post, error) {
|
|||||||
Logger: sys.logger,
|
Logger: sys.logger,
|
||||||
})
|
})
|
||||||
|
|
||||||
posts, err := c.Posts.List(context.Background())
|
items, err := c.Topics.ListLatest(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []post.Post{}, err
|
return []post.Post{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var mPosts []post.Post
|
var models []post.Post
|
||||||
for _, p := range posts {
|
for _, i := range (*items).TopicList.Topics {
|
||||||
mPosts = append(mPosts, post.Post{
|
models = append(models, post.Post{
|
||||||
ID: string(p.ID),
|
ID: string(i.ID),
|
||||||
Subject: p.TopicTitle,
|
Subject: i.Title,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return mPosts, nil
|
return models, nil
|
||||||
}
|
}
|
||||||
|
121
system/discourse/topics.go
Normal file
121
system/discourse/topics.go
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
package discourse
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
const TopicsBaseURL = "/topics"
|
||||||
|
|
||||||
|
type LatestTopicsResponse struct {
|
||||||
|
Users []struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
AvatarTemplate string `json:"avatar_template"`
|
||||||
|
}
|
||||||
|
|
||||||
|
TopicList struct {
|
||||||
|
CanCreateTopic bool `json:"can_create_topic"`
|
||||||
|
Draft string `json:"draft"`
|
||||||
|
DraftKey string `json:"draft_key"`
|
||||||
|
DraftSequence int `json:"draft_sequence"`
|
||||||
|
PerPage int `json:"per_page"`
|
||||||
|
|
||||||
|
Topics []TopicModel `json:"topics"`
|
||||||
|
} `json:"topic_list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type TopicModel struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
FancyTitle string `json:"fancy_title"`
|
||||||
|
Slug string `json:"slug"`
|
||||||
|
PostsCount int `json:"posts_count"`
|
||||||
|
ReplyCount int `json:"reply_count"`
|
||||||
|
HighestPostNumber int `json:"highest_post_number"`
|
||||||
|
ImageURL string `json:"image_url"`
|
||||||
|
CreatedAt string `json:"created_at"`
|
||||||
|
LastPostedAt string `json:"last_posted_at"`
|
||||||
|
Bumped bool `json:"bumped"`
|
||||||
|
BumpedAt string `json:"bumped_at"`
|
||||||
|
Archetype string `json:"archetype"`
|
||||||
|
Unseen bool `json:"unseen"`
|
||||||
|
LastReadPostNumber int `json:"last_read_post_number"`
|
||||||
|
UnreadPosts int `json:"unread_posts"`
|
||||||
|
Pinned bool `json:"pinned"`
|
||||||
|
Unpinned string `json:"unpinned"`
|
||||||
|
Visible bool `json:"visible"`
|
||||||
|
Closed bool `json:"closed"`
|
||||||
|
Archived bool `json:"archived"`
|
||||||
|
NotificationLevel int `json:"notification_level"`
|
||||||
|
Bookmarked bool `json:"bookmarked"`
|
||||||
|
Liked bool `json:"liked"`
|
||||||
|
Views int `json:"views"`
|
||||||
|
LikeCount int `json:"like_count"`
|
||||||
|
HasSummary bool `json:"has_summary"`
|
||||||
|
LastPosterUsername string `json:"last_poster_username"`
|
||||||
|
CategoryID int `json:"category_id"`
|
||||||
|
OpLikeCount int `json:"op_like_count"`
|
||||||
|
PinnedGlobally bool `json:"pinned_globally"`
|
||||||
|
FeaturedLink string `json:"featured_link"`
|
||||||
|
Posters []struct {
|
||||||
|
Extras string `json:"extras"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
UserID int `json:"user_id"`
|
||||||
|
PrimaryGroupID string `json:"primary_group_id"`
|
||||||
|
} `json:"posters"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type TopicsService interface {
|
||||||
|
Show(
|
||||||
|
ctx context.Context,
|
||||||
|
id string,
|
||||||
|
) (*TopicModel, error)
|
||||||
|
ListLatest(
|
||||||
|
ctx context.Context,
|
||||||
|
) (*LatestTopicsResponse, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type TopicServiceHandler struct {
|
||||||
|
client *Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show
|
||||||
|
func (a *TopicServiceHandler) Show(
|
||||||
|
ctx context.Context,
|
||||||
|
id string,
|
||||||
|
) (*TopicModel, error) {
|
||||||
|
uri := TopicsBaseURL + "/t/" + id + ".json"
|
||||||
|
|
||||||
|
req, err := a.client.NewRequest(ctx, http.MethodGet, uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
response := new(TopicModel)
|
||||||
|
if err = a.client.Do(ctx, req, response); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// List
|
||||||
|
func (a *TopicServiceHandler) ListLatest(
|
||||||
|
ctx context.Context,
|
||||||
|
) (*LatestTopicsResponse, error) {
|
||||||
|
uri := "/latest.json"
|
||||||
|
|
||||||
|
req, err := a.client.NewRequest(ctx, http.MethodGet, uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
response := new(LatestTopicsResponse)
|
||||||
|
if err = a.client.Do(ctx, req, response); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user