jokeapi

package module
v0.0.0-...-6b33385 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2023 License: MIT Imports: 8 Imported by: 2

README

jokeapi-go

GoDoc Go Report Card Test Coverage Mentioned in Awesome Go

Official golang wrapper for Sv443's jokeapi.

Install- Add github.com/icelain/jokeapito your go.mod(recommended) or:

EXPORT GO11MODULE=off
go get -u github.com/icelain/jokeapi

Basic Usage Without Parameters-

import "github.com/icelain/jokeapi"

func main(){
  api := jokeapi.New()
  response, err := api.Fetch()
}

Response Struct-

type JokesResp struct{
	Error bool
	Category string
	JokeType string
	Joke []string
	Flags map[string] bool
	Id float64
	Lang string
}

Usage with all parameters-

import "github.com/icelain/jokeapi"

func main(){
  jt := "single"
  blacklist := []string{"nsfw"}
  ctgs := []string{"Programming","Dark"}
  
  api:= jokeapi.New()
  
  api.Set(jokeapi.Params{Blacklist: blacklist, JokeType: jt, Categories: ctgs})
  response, err := api.Fetch()
}

Config struct-

api.Set(jokeapi.Params{})

Config methods-

api.SetBlacklist(blacklist)
api.SetCategories(ctgs)
api.SetJokeType(joketype)
api.SetLang(language)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JokeAPI

type JokeAPI struct {
	ExportedParams Params
}

JokeAPI struct

func New

func New() *JokeAPI

New Generates instance of JokeAPI struct

func (*JokeAPI) Fetch

func (j *JokeAPI) Fetch() (JokesResp, error)

Fetch gets the content with respect to the parameters. Use FetchWithContext to add your custom context.

func (*JokeAPI) FetchWithContext

func (j *JokeAPI) FetchWithContext(ctx context.Context) (JokesResp, error)

FetchWithContext gets the content with respect to the parameters. Accepts a context.Context.

func (*JokeAPI) Set

func (j *JokeAPI) Set(params Params)

Set sets custom Params struct

func (*JokeAPI) SetBlacklist

func (j *JokeAPI) SetBlacklist(b []string)

SetBlacklist sets joke blacklist. Common blacklists are nsfw | religious | political | racist | sexist | explicit

func (*JokeAPI) SetCategories

func (j *JokeAPI) SetCategories(ctgs []string)

SetCategories sets joke categories. Common categories are Programming | Misc | Spooky | Dark | Fun

func (*JokeAPI) SetJokeType

func (j *JokeAPI) SetJokeType(s string)

SetJokeType sets joke type

func (*JokeAPI) SetLang

func (j *JokeAPI) SetLang(lang string)

SetLang sets language. Go to https://v2.jokeapi.dev/languages?format=txt to select your preferable language format. By default its en (English). Note that (as of now) most jokes are available in en and de only and setting other languages will give a corresponding error

func (*JokeAPI) SetParams

func (j *JokeAPI) SetParams(ctgs []string, blacklist []string, joketype string, lang string)

SetParams sets parameters to JokeAPI struct instance. This method only exists because I don't want to make breaking changes to the existing api by removing it. I would recommend using Jokeapi{}.Set() or the singular methods instead

type JokesResp

type JokesResp struct {
	Error    bool
	Category string
	JokeType string
	Joke     []string
	Flags    map[string]bool
	Id       float64
	Lang     string
}

JokesResp is the response to be sent by JokeAPI{}.Fetch()

type Params

type Params struct {
	Categories []string
	Blacklist  []string
	JokeType   string
	Lang       string
}

Params is the config struct be used by JokeAPI{}.Fetch()

Jump to

Keyboard shortcuts

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