package hnapi

import "github.com/hlandau/ircproto/egobot/hnapi"

Package hnapi provides basic functions for retrieving data from the Hacker News API.

Index

Variables

var _, Log = xlog.New("hnapi")

Functions

func UnmarshalMaxEvent

func UnmarshalMaxEvent(ev *Event) (int, error)

Unmarshals a maximum item number change event. Returns the new maximum item number.

func UnmarshalTopEvent

func UnmarshalTopEvent(ev *Event) ([]int, error)

Unmarshals a top stories set change event. Returns the list of story IDs which are on the top list.

Types

type Client

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

A Hacker News API client.

func New

func New(cfg *Config) (*Client, error)

Create a new Hacker News API client.

func (*Client) GetItem

func (c *Client) GetItem(itemNo int) (*Item, error)

Fetch an item with the given ID from the API. May return FetchError or other errors.

func (*Client) MaxItemEventChan

func (c *Client) MaxItemEventChan() (<-chan *Event, error)

Calling this sets up a max item event listener, which yields events whenever the maximum item number changes. Use UnmarshalMaxEvent on the result to access the maximum item number.

func (*Client) RelativeURL

func (c *Client) RelativeURL(relURL string) (string, error)

Get a Hacker News API URL by resolving the given relative URL relative to the configured base URL.

func (*Client) TopStoryEventChan

func (c *Client) TopStoryEventChan() (<-chan *Event, error)

Calling this sets up a top story event listener, which yields events whenever the top stories change. Use UnmarshalTopEvent to access the data.

type Config

type Config struct {
	HTTPClient    *http.Client // HTTP client to use to make requests, or nil for default.
	HTTPUserAgent string       // If HTTPClient is nil, HTTP user agent string to use.
	URL           string       // Base URL of the Hacker News API. Must end in "/".
}

type Event

type Event struct {
	Type string
	Data string
	ID   string
}

type FetchError

type FetchError struct {
	ItemNo     int    // The item that was requested.
	StatusCode int    // HTTP status code.
	Status     string // HTTP status string.
}

Information about an item retrieval error.

func (*FetchError) Error

func (e *FetchError) Error() string

func (*FetchError) Temporary

func (e *FetchError) Temporary() bool

Whether this appears to be a temporary error.

type Item

type Item struct {
	Type string `json:"type"`
	ID   int    `json:"id"`
	Time int    `json:"time"`

	// Not all fields are present for all types of item.
	Title       string `json:"title"`
	By          string `json:"by"`
	Score       int    `json:"score"`
	URL         string `json:"url"`
	Descendants int    `json:"descendants"`
	Text        string `json:"text"`
	Parent      int    `json:"parent"`
	Kids        []int  `json:"kids"`
}

A Hacker News item.

type SSEListener

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

An HTTP Server Sent Events client.

func NewSSEListener

func NewSSEListener(url string, client *http.Client, notifyChan chan<- *Event) (*SSEListener, error)

Create a new SSE listener which connects to the given URL, using the given HTTP client (or a default client if nil is specified) and sending notifications on notifyChan when events occur. Reconnects automatically if the connection is lost.

func (*SSEListener) Close

func (l *SSEListener) Close() error

Tears down the listener.

Calling this multiple times is inconsequential.

Source Files

hnapi.go sse.go

Version
v0.0.0-20240529044229-f1af42e426cd (latest)
Published
May 29, 2024
Platform
linux/amd64
Imports
10 packages
Last checked
1 month ago

Tools for package owners.