jokes

package module
v0.0.0-...-1934895 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2021 License: MIT Imports: 7 Imported by: 0

README

jokes

API wrapper for JokeAPI.

Documentation

Index

Constants

View Source
const (
	KeyAmount    string = "amount"
	KeyBlacklist string = "blacklistFlags"
	KeyContains  string = "contains"
	KeyIDRange   string = "idRange"
	KeyLang      string = "lang"
	KeySafe      string = "safe-mode"
	KeyType      string = "type"
)

Variables

View Source
var BaseURL = url.URL{
	Scheme: "https",
	Host:   "v2.jokeapi.dev",
}

JokeAPI base URL.

Functions

This section is empty.

Types

type Categories

type Categories []Category

Joke categories.

func (*Categories) Add

func (s *Categories) Add(c string) error

Validate and add given string as a Category. Returns an error if given string is an invalid category.

func (Categories) String

func (s Categories) String() string

Convert Categories into a space-separated string.

type Category

type Category string

Represents joke category. See https://jokeapi.dev/#categories

const (
	Any         Category = "Any"
	Christmas   Category = "Christmas"
	Dark        Category = "Dark"
	Misc        Category = "Misc"
	Programming Category = "Programming"
	Pun         Category = "Pun"
	Spooky      Category = "Spooky"
)

type ErrorResponse

type ErrorResponse struct {
	Cause    []string `json:"causedBy"`
	Code     int      `json:"code"`
	Info     string   `json:"additionalInfo"`
	Internal bool     `json:"internalError"`
	Message  string   `json:"message"`
	Time     int      `json:"timestamp"`
}

An error response.

func (ErrorResponse) Error

func (e ErrorResponse) Error() string

type Flag

type Flag string

Blacklist flag. See https://jokeapi.dev/#blacklist-flags

const (
	Nsfw      Flag = "nsfw"
	Religious Flag = "religious"
	Political Flag = "political"
	Racist    Flag = "racist"
	Sexist    Flag = "sexist"
	Explicit  Flag = "explicit"
)

type Flags

type Flags []Flag

Blacklist flags.

func (*Flags) Add

func (s *Flags) Add(f string) error

Validate and add given string as a blacklist flag. Returns error if the given string is an invalid flag.

func (Flags) String

func (s Flags) String() string

Convert Flags into a space-separated string.

type IDRange

type IDRange struct {
	Lower int
	Upper int
}

A joke ID range. See https://jokeapi.dev/#idrange-param

func ID

func ID(id int) *IDRange

Returns an IDRange struct for a single joke ID.

func (IDRange) String

func (r IDRange) String() string

Convert ID range into s "number[-number]" string.

type Joke

type Joke struct {
	Category Category      `json:"category"`
	Delivery string        `json:"delivery"`
	Flags    map[Flag]bool `json:"flags"`
	Id       int           `json:"id"`
	Joke     string        `json:"joke"`
	Lang     Lang          `json:"lang"`
	Safe     bool          `json:"safe"`
	Setup    string        `json:"setup"`
	Type     Type          `json:"type"`
}

A single joke response from the JokeAPI "joke" endpoint.

func ParseResponse

func ParseResponse(jsn []byte) (j []Joke, e error)

Parse given response JSON into a slice of Jokes. Returns an error if response is an error response.

func (Joke) String

func (j Joke) String() string

type Jokes

type Jokes struct {
	Jokes []Joke `json:"jokes"`
}

A multi-joke response if request has Amount > 1.

type Lang

type Lang string

Represents language returned by JokeAPI. See https://jokeapi.dev/#lang

const (
	Cs Lang = "cs" // Czech
	De Lang = "de" // German
	En Lang = "en" // English
	Es Lang = "es" // Spanish
	Fr Lang = "fr" // French
	Pt Lang = "pt" // Portugese
)

func (*Lang) Set

func (s *Lang) Set(l string) error

type Request

type Request struct {
	Amount    int
	Blacklist Flags
	Category  Categories
	Contains  string
	ID        *IDRange
	Lang      Lang
	Safe      bool
	Type      Type
}

A joke request.

func NewRequest

func NewRequest() *Request

Create a new Request struct.

func (Request) Get

func (j Request) Get() ([]Joke, error)

Perform the HTTP GET request to fetch the joke by the default http client.

func (Request) GetUsingClient

func (j Request) GetUsingClient(client *http.Client) (r []Joke, e error)

Perform the HTTP GET request to fetch the joke by using the given http.Client

func (Request) Query

func (j Request) Query() url.Values

Convert joke request into a "url.Values" map.

func (Request) URL

func (j Request) URL() string

Get the full API URL for the joke. If Category is not set, will fetch by default Any categories.

type Type

type Type string

Joke type. See https://jokeapi.dev/#joke-type

const (
	Single  Type = "single"
	Twopart Type = "twopart"
)

func (*Type) Set

func (s *Type) Set(t string) error

Set this Type to the given string. Returns error if the given string is an invalid joke type.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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