hnapi

package
v0.0.0-...-1276c7d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2022 License: MIT Imports: 5 Imported by: 3

Documentation

Index

Constants

View Source
const (
	CommentItem = "comment"
	PollItem    = "poll"
	PollOptItem = "pollopt"
	JobItem     = "job"
	AskItem     = "ask"
	StoryItem   = "story"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Comment

type Comment struct {
	By        string
	ID        int
	Kids      []int
	Parent    int
	Text      string
	Time      int64
	Timestamp time.Time
	Type      string
}

Comment represents a HackerNews comment on a story.

func ItemToComment

func ItemToComment(item *Item) (*Comment, error)

ItemToComment converts an Item into a Comment.

type HNClient

type HNClient struct {
	// contains filtered or unexported fields
}

HNClient wraps an http client with methods to query the Hacker News firebase API.

func NewHNClient

func NewHNClient() *HNClient

NewHNClient returns an HNClient with default settings.

func NewHNClientWithURL

func NewHNClientWithURL(url string) *HNClient

NewHNClientWithURL returns an HNClient that will send all client request to the specified URL. This client is dependent on the API schema defined here: https://github.com/HackerNews/API/blob/9a57f04559388cc657d8b47b67fe0a687519ba4f/README.md

func (*HNClient) GetComment

func (h *HNClient) GetComment(id string) (*Comment, error)

GetComment is a helper function that gets an Item and converts it to a Comment or errors.

func (*HNClient) GetItem

func (h *HNClient) GetItem(id string) (*Item, error)

GetItem returns an *Item from the API.

func (*HNClient) GetPoll

func (h *HNClient) GetPoll(id string) (*Poll, error)

GetPoll is a helper function that gets an Item and converts it to a Poll or errors.

func (*HNClient) GetPollOpt

func (h *HNClient) GetPollOpt(id string) (*PollOpt, error)

GetPollOpt is a helper function that gets an Item and converts it to a Pollopt or errors.

func (*HNClient) GetStory

func (h *HNClient) GetStory(id string) (*Story, error)

GetStory is a helper function that gets an Item and converts it to a Story or errors.

func (*HNClient) Item deprecated

func (h *HNClient) Item(id string) (interface{}, error)

Deprecated: Item is deprecated. Use GetItem or a more specialized method like GetComment instead. This was deprecated because it pushed type assertion into client code. There are now helpers like GetStory and ItemToComment to avoid returning an interface{} for clients to deal with. Item issues a GET request on the /item/<id> path and creates a struct from the response body. It returns an empty interface to be asserted into one of the hnapi types Story, Comment, Poll, PollOpt or an error.

func (*HNClient) MaxItemID

func (h *HNClient) MaxItemID() (int, error)

MaxItemID issues a GET request to the /maxitem path and returns the current largest item id. This can be used to request information for all items by walking backwards.

func (*HNClient) TopStoryIDs

func (h *HNClient) TopStoryIDs(t TopType) ([]int, error)

TopStoryIDs issues a GET request to the /<TopType> path where TopType is one of the defined enum values. The API will return up to ~500 results for the Top and New categories.

func (*HNClient) Updates

func (h *HNClient) Updates() (*Update, error)

Updates issues a GET request to the /updates path and returns the latest item and profile changes. Items are updates to posts and comments and profiles are the IDs of the profiles that have recently changed.

func (*HNClient) User

func (h *HNClient) User(id string) (*HNUser, error)

User issues a GET request on the /user/<id> path and returns a HNUser struct containing the details from the response.

type HNUser

type HNUser struct {
	About     string
	Created   int64
	Delay     int
	ID        string
	Karma     int
	Submitted []int
}

HNUser represents a HackerNews user profile.

type Item

type Item struct {
	ID          int
	Deleted     bool
	Type        string
	By          string
	Time        int64
	Timestamp   time.Time // Not directly available in the API.
	Text        string
	Dead        bool
	Parent      int
	Poll        int
	Kids        []int
	URL         string
	Score       int
	Title       string
	Parts       []int
	Descendants int
}

Item represents a HackerNews API item. Its properties are a superset of Story, Comment, Poll, and Pollopt types.

type Poll

type Poll struct {
	By          string
	Descendants int
	ID          int
	Kids        []int
	Parts       []int
	Score       int
	Text        string
	Time        int64
	Timestamp   time.Time
	Title       string
	Type        string
}

Poll represents a HackerNews poll. It contains a Parts field that describes the related poll options.

func ItemToPoll

func ItemToPoll(item *Item) (*Poll, error)

ItemToPoll converts an Item into a Poll.

type PollOpt

type PollOpt struct {
	By        string
	ID        int
	Poll      int
	Score     int
	Text      string
	Time      int64
	Timestamp time.Time
	Type      string
}

PollOpt represents the poll options from a given poll.

func ItemToPollOpt

func ItemToPollOpt(item *Item) (*PollOpt, error)

ItemToPollOpt converts an Item into a PollOpt.

type Story

type Story struct {
	By          string
	Descendants int
	ID          int
	Kids        []int
	Score       int
	Time        int64
	Timestamp   time.Time
	Title       string
	Type        string
	URL         string
}

Story represents a HackerNews submitted story.

func ItemToStory

func ItemToStory(item *Item) (*Story, error)

ItemToStory converts an Item into a Story.

type TopType

type TopType string

TopType is a type alias used to represent an enum.

const (
	// Top is for the top ~500 stories.
	Top TopType = "topstories"
	// New is for the new stories.
	New TopType = "newstories"
	// Best is for the highest ranking stories.
	Best TopType = "beststories"
	// Show is for stories categorized as 'Show'.
	Show TopType = "showstories"
	// Job is for stories categorized as 'Jobs'.
	Job TopType = "jobstories"
)

type Update

type Update struct {
	Items    []int
	Profiles []string
}

Update represents the response from the 'updates' path.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL