client

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2021 License: GPL-3.0 Imports: 11 Imported by: 1

README

Advent of Code helper

You can also get the [JSON] for this private leaderboard. Please don't make frequent automated requests to this service - avoid sending requests more often than once every 15 minutes (900 seconds). If you do this from a script, you'll have to provide your session cookie in the request; a fresh session cookie lasts for about a month. Timestamps use Unix time.

Source: adventofcode.com

Download input file

targetDir := fmt.Sprintf("input/day%02d", dayNumber)
targetFile := fmt.Sprintf("%s/input", targetDir)

ensurePath(targetDir)

client := aoc.NewClient(os.Getenv("AOC_SESSION"))

err := client.DownloadAndSaveInput(currentYear, dayNumber, targetFile)
if err != nil {
    logrus.Fatal(err.Error())

    return
}

Submit a solution

client := aoc.NewClient(os.Getenv("AOC_SESSION"))

valid, err := client.SubmitSolution(currentYear, dayNumber, partNumber, solution)
if err != nil {
    fmt.Printf("%s\n", err.Error())

    return
}

if valid {
    fmt.Println("Done \\o/")
} else {
    fmt.Println("Something is wrong :(")
}

Documentation

Index

Constants

View Source
const (
	// CorrectAnswer is the goal.
	CorrectAnswer = "That's the right answer!"
	// AlreadySolved means our solution is already there.
	AlreadySolved = "Did you already complete it?"
	// WrongAnswer means the solution was incorrect.
	WrongAnswer = "That's not the right answer;"
	// WaitMore means you have to wait more.
	WaitMore = "You gave an answer too recently;"
	// NoSession means the provided token was not valid.
	NoSession = "To play, please identify yourself"
)
View Source
const RequestTimout = 10

RequestTimout is the timeout of a request in seconds.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	SessionToken string
	HTTPClient   HTTPClient
	// contains filtered or unexported fields
}

Client for Advent of Code.

func NewClient

func NewClient(token string) Client

NewClient creates a new client.

func (*Client) DownloadAndSaveInput

func (c *Client) DownloadAndSaveInput(year, day int, targetFile string) error

DownloadAndSaveInput downloads and saves the requested input file.

func (*Client) DownloadInput

func (c *Client) DownloadInput(year, day int) (io.ReadCloser, error)

DownloadInput downloads the requested input file.

func (*Client) LogLevel

func (c *Client) LogLevel(level logrus.Level)

LogLevel sets logger level.

func (*Client) SubmitSolution

func (c *Client) SubmitSolution(year, day, part int, solution string) (bool, error)

SubmitSolution downloads and saves the requested input file.

type DownloadError

type DownloadError struct {
	StatusCode int
}

DownloadError occurs when the AoC server returns with a status code other than 200.

func (DownloadError) Error

func (e DownloadError) Error() string

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient is a simple interface for http.Client. Reason: mock client in tests.

type IncorrectAnswerError

type IncorrectAnswerError struct {
	Hint string
	Wait string
}

IncorrectAnswerError occurs when the provided answer is not correct.

func (IncorrectAnswerError) Error

func (e IncorrectAnswerError) Error() string

type NetworkError

type NetworkError struct {
	Original error
}

NetworkError occurs when something went wrong with the HTTP request.

func (NetworkError) Error

func (e NetworkError) Error() string

type SubmitError

type SubmitError struct {
	StatusCode int
}

SubmitError occurs when the AoC server returns with a status code other than 200.

func (SubmitError) Error

func (e SubmitError) Error() string

Jump to

Keyboard shortcuts

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