Documentation
¶
Index ¶
- Constants
- Variables
- func Fetch[T any](ctx context.Context, client *http.Client, method, url string) (T, error)
- func SetMaxWorkers(n int)
- func Sort[S Sortable](items []S, sort func(a, b S) int)
- func SortID[S Sortable](items []S, order Order)
- func SortScore[S Sortable](items []S, order Order)
- func SortTime[S Sortable](items []S, order Order)
- func SortType[S Sortable](items []S, order Order)
- func To[C Convertible](item Item) (c C, err error)
- func ToList[C Convertible](items []Item) []C
- type Ask
- type Client
- type Comment
- type Convertible
- type Item
- type ItemService
- type Job
- type LiveService
- func (s *LiveService) Ask(ctx context.Context) ([]uint, error)
- func (s *LiveService) AskList(ctx context.Context, filter func(Item) bool) ([]Ask, error)
- func (s *LiveService) Best(ctx context.Context) ([]uint, error)
- func (s *LiveService) BestList(ctx context.Context, filter func(Item) bool) ([]Item, error)
- func (s *LiveService) Job(ctx context.Context) ([]uint, error)
- func (s *LiveService) JobList(ctx context.Context, filter func(Item) bool) ([]Job, error)
- func (s *LiveService) MaxID(ctx context.Context) (uint, error)
- func (s *LiveService) New(ctx context.Context) ([]uint, error)
- func (s *LiveService) NewList(ctx context.Context, filter func(Item) bool) ([]Item, error)
- func (s *LiveService) Recent(ctx context.Context, offset uint) ([]Item, error)
- func (s *LiveService) Show(ctx context.Context) ([]uint, error)
- func (s *LiveService) ShowList(ctx context.Context, filter func(Item) bool) ([]Story, error)
- func (s *LiveService) Top(ctx context.Context) ([]uint, error)
- func (s *LiveService) TopList(ctx context.Context, filter func(Item) bool) ([]Item, error)
- func (s *LiveService) Update(ctx context.Context) (Update, error)
- func (s *LiveService) UpdateList(ctx context.Context, filter func(Item) bool) ([]Item, error)
- type Order
- type Poll
- type PollOption
- type Sortable
- type Story
- type Timestamp
- type Update
- type User
- type UserService
- func (s *UserService) Asks(ctx context.Context, username string) ([]Ask, error)
- func (s *UserService) Comments(ctx context.Context, username string) ([]Comment, error)
- func (s *UserService) Get(ctx context.Context, username string) (User, error)
- func (s *UserService) Items(ctx context.Context, username string, filter func(Item) bool) ([]Item, error)
- func (s *UserService) Jobs(ctx context.Context, username string) ([]Job, error)
- func (s *UserService) PollOptions(ctx context.Context, username string) ([]PollOption, error)
- func (s *UserService) Polls(ctx context.Context, username string) ([]Poll, error)
- func (s *UserService) Stories(ctx context.Context, username string) ([]Story, error)
Constants ¶
const ( StoryType = "story" CommentType = "comment" AskType = "ask" JobType = "job" PollType = "poll" PollOptionType = "pollopt" )
Variables ¶
var (
ErrNotFound = errors.New("item is not found")
)
Functions ¶
func Fetch ¶
Fetch sends an HTTP request to the Hacker News API and returns a value of the specified type.
func SetMaxWorkers ¶
func SetMaxWorkers(n int)
SetMaxWorkers sets the maximum number of workers for multiple item fetch operations. The default value of maxWorkers is -1, meaning there is no limit to the number of workers.
func To ¶
func To[C Convertible](item Item) (c C, err error)
To is a helper function to convert any Item struct to a struct of a specific type that implements the Convertible interface: Comment, Story, Ask, Job, Poll or PollOption. Another alternative for this function is a specific converter (ToComment, ToStory, etc.).
func ToList ¶
func ToList[C Convertible](items []Item) []C
ToList converts a slice of items to a list of structs of a specific type.
If the type of any item doesn't match the output type, the item is excluded from the converted list.
Types ¶
type Ask ¶
type Client ¶
type Client struct { Items *ItemService Users *UserService Live *LiveService }
Client represents a client for the Hacker News API.
type Comment ¶
type Comment struct { Kids []uint `json:"kids,omitempty"` Parent uint `json:"parent,omitempty"` Text string `json:"text,omitempty"` // contains filtered or unexported fields }
type Convertible ¶
type Convertible interface {
Type() string
}
Convertible describes types that can be converted from an Item.
type Item ¶
type Item struct { Descendants int `json:"descendants,omitempty"` Parts []uint `json:"parts,omitempty"` Parent uint `json:"parent,omitempty"` Kids []uint `json:"kids,omitempty"` Text string `json:"text,omitempty"` Title string `json:"title,omitempty"` Poll uint `json:"poll,omitempty"` URL string `json:"url,omitempty"` // contains filtered or unexported fields }
Item is a common type for all other types: story, comment, poll, etc. It contains all fields, so some of them may be empty if the value of the specific type doesn't have that field.
type ItemService ¶
type ItemService struct {
// contains filtered or unexported fields
}
ItemService provides methods to retrieve data about Hacker News items.
type Job ¶
type LiveService ¶
type LiveService struct {
// contains filtered or unexported fields
}
LiveService provides methods to retrieve data about recent updates.
func (*LiveService) Ask ¶
func (s *LiveService) Ask(ctx context.Context) ([]uint, error)
Ask returns a list of IDs for the asks.
func (*LiveService) Best ¶
func (s *LiveService) Best(ctx context.Context) ([]uint, error)
Best returns a list of IDs for the best stories.
func (*LiveService) BestList ¶
BestList returns a list of items for the best stories, filtered if necessary.
func (*LiveService) Job ¶
func (s *LiveService) Job(ctx context.Context) ([]uint, error)
Job returns a list of IDs for the jobs.
func (*LiveService) MaxID ¶
func (s *LiveService) MaxID(ctx context.Context) (uint, error)
MaxID returns the ID of the most recently published item.
func (*LiveService) New ¶
func (s *LiveService) New(ctx context.Context) ([]uint, error)
New returns a list of IDs for the new stories.
func (*LiveService) NewList ¶
NewList returns a list of items for the new stories, filtered if necessary.
func (*LiveService) Show ¶
func (s *LiveService) Show(ctx context.Context) ([]uint, error)
Show returns a list of IDs for the shows.
func (*LiveService) ShowList ¶
ShowList returns a list of items for the shows, filtered if necessary.
func (*LiveService) Top ¶
func (s *LiveService) Top(ctx context.Context) ([]uint, error)
Top returns a list of IDs for the top stories.
func (*LiveService) TopList ¶
TopList returns a list of items for the top stories, filtered if necessary.
func (*LiveService) Update ¶
func (s *LiveService) Update(ctx context.Context) (Update, error)
Update returns an Update containing IDs of updated items and profiles.
func (*LiveService) UpdateList ¶
UpdateList returns a list of updated items, filtered if necessary.
type Poll ¶
type PollOption ¶
type PollOption struct { Poll uint `json:"poll,omitempty"` Text string `json:"text,omitempty"` // contains filtered or unexported fields }
func ToPollOption ¶
func ToPollOption(item Item) PollOption
ToPollOption converts an Item struct to a PollOption struct.
func (PollOption) Type ¶
func (o PollOption) Type() string
type Sortable ¶
type Sortable interface {
// contains filtered or unexported methods
}
Sortable defines methods for comparing baseItem and other structs (e.g., Comment, Story, etc.).
type Story ¶
type Timestamp ¶
func (*Timestamp) UnmarshalJSON ¶
type UserService ¶
type UserService struct {
// contains filtered or unexported fields
}
UserService provides methods to retrieve data about Hacker News users.
func (*UserService) Comments ¶
Comments returns the comments submitted by the user with the given name.
func (*UserService) Items ¶
func (s *UserService) Items(ctx context.Context, username string, filter func(Item) bool) ([]Item, error)
Items returns the items submitted by the user with the given name, filtered if necessary.
func (*UserService) PollOptions ¶
func (s *UserService) PollOptions(ctx context.Context, username string) ([]PollOption, error)
PollOptions returns the poll options submitted by the user with the given name.