hackernews

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2023 License: MIT Imports: 6 Imported by: 11

README

Hacker News API Go Client

Go Reference

This is an up-to-date Go client for the Hacker News API.

  • Supports Go modules
  • Supports context.Context
  • Supports all documented endpoints
  • Fully tested

Installation

go get github.com/hermanschaaf/hackernews

Usage

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/hermanschaaf/hackernews"
)

func main() {
    client := hackernews.NewClient()
    ctx := context.Background()

    // Get the top 10 stories
    topStories, err := client.TopStories(ctx)
    if err != nil {
        log.Fatal(err)
    }

    // Print the title of the first story
    story, err := client.GetItem(ctx, topStories[0])
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(story.Title)
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(opts ...Option) *Client

func (*Client) AskStories

func (c *Client) AskStories(ctx context.Context) ([]int, error)

AskStories returns up to 200 of the latest ask stories.

func (*Client) BestStories

func (c *Client) BestStories(ctx context.Context) ([]int, error)

BestStories returns up to 500 of the best stories.

func (*Client) GetItem

func (c *Client) GetItem(ctx context.Context, id int) (Item, error)

func (*Client) GetUser

func (c *Client) GetUser(ctx context.Context, id string) (User, error)
Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/hermanschaaf/hackernews"
)

func main() {
	client := hackernews.NewClient()

	// Get the karma for user "pg" and print true if it's greater than 10000.
	ctx := context.Background()
	user, err := client.GetUser(ctx, "pg")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(user.Karma > 10000)
}
Output:

true

func (*Client) JobStories

func (c *Client) JobStories(ctx context.Context) ([]int, error)

JobStories returns up to 200 of the latest job stories.

func (*Client) MaxItemID

func (c *Client) MaxItemID(ctx context.Context) (int, error)

func (*Client) NewStories

func (c *Client) NewStories(ctx context.Context) ([]int, error)

NewStories returns up to 500 of the newest stories.

func (*Client) ShowStories

func (c *Client) ShowStories(ctx context.Context) ([]int, error)

ShowStories returns up to 200 of the latest show stories.

func (*Client) TopStories

func (c *Client) TopStories(ctx context.Context) ([]int, error)

TopStories returns up to 500 of the latest top stories.

func (*Client) Updates

func (c *Client) Updates(ctx context.Context) (*Updates, error)

Updates returns the latest item IDs. The updates represent the latest items that have been created, updated, or deleted.

type HTTPError added in v1.0.1

type HTTPError struct {
	Code int
}

func (HTTPError) Error added in v1.0.1

func (e HTTPError) Error() string

type Item

type Item struct {
	ID          int    `json:"id"`
	Deleted     bool   `json:"deleted"`
	Type        string `json:"type"`
	By          string `json:"by"`
	Time        int    `json:"time"`
	Text        string `json:"text"`
	Dead        bool   `json:"dead"`
	Parent      int    `json:"parent"`
	Kids        []int  `json:"kids"`
	URL         string `json:"url"`
	Score       int    `json:"score"`
	Title       string `json:"title"`
	Parts       []int  `json:"parts"`
	Descendants int    `json:"descendants"`
}

type Option

type Option func(*Client)

func WithBaseURL

func WithBaseURL(url string) Option

func WithHTTPClient

func WithHTTPClient(client *http.Client) Option

type Updates

type Updates struct {
	Items    []int    `json:"items"`
	Profiles []string `json:"profiles"`
}

type User

type User struct {
	ID        string `json:"id"`
	Created   int    `json:"created"`
	Karma     int    `json:"karma"`
	About     string `json:"about"`
	Submitted []int  `json:"submitted"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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