opentrivia

package module
v0.0.0-...-83245ad Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2017 License: MIT Imports: 6 Imported by: 0

README

opentrivia

This repository contains an API client for Open Trivia DB.

opentrivia was developed under golang version 1.8 and not tested on previous versions of the language.

Usage

opentrivia (godoc)
go get github.com/pinheirolucas/opentrivia

Construct a new Open Trivia API client, then use the services on the client to access different parts of the API. For example:

client := opentrivia.DefaultClient

// list 10 random questions
questions, err := client.Question.List(nil)

Some API methods have optional parameters that can be passed. For example:

client := opentrivia.DefaultClient

// list 20 questions with difficulty easy
options := &opentrivia.QuestionListOptions{
	Difficulty: opentrivia.QuestionDifficultyEasy,
	Limit: 20,
}
questions, err := client.Question.List(options)

License

This library is distributed under the MIT license found in the LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidParameter is returned when the Open Trivia API
	// identifies an invalid parameter.
	ErrInvalidParameter = errors.New("opentrivia: the provided options are not valid")

	// ErrNoResults is returned when the Open Trivia API has no
	// results.
	ErrNoResults = errors.New("opentrivia: no results were found for the provided options")
)
View Source
var (
	// DefaultQuestionListOptions is the default options of Question List
	// method.
	DefaultQuestionListOptions = &QuestionListOptions{
		AutoRefresh: false,
		Limit:       10,
	}

	// DefaultQuestionRandomOptions is the default options of Question Random
	// method.
	DefaultQuestionRandomOptions = &QuestionRandomOptions{
		AutoRefresh: false,
	}
)
View Source
var (
	// ErrTokenEmpty is returned when the Open Trivia API has
	// returned all possible questions for the specified query.
	//
	// Resetting the Token is necessary to keep on running.
	ErrTokenEmpty = errors.New("opentrivia: token has returned all possible questions for the specified query")

	// ErrTokenNotFound is returned when the Open Trivia API
	// do not found the provided token.
	ErrTokenNotFound = errors.New("opentrivia: token does not exist")
)
View Source
var DefaultClient = NewClient(nil)

DefaultClient is the default client for Open Trivia API. It is the same as calling opentrivia.NewClient(nil).

Functions

This section is empty.

Types

type Client

type Client struct {

	// Base URL for API requests. Defaults to the public Open Trivia API.
	// BaseURL should always be especified with a trailing slash.
	BaseURL *url.URL

	// Services used for talking to different parts of the Open Trivia API.
	// TODO: Add the services.
	Question *QuestionService
	Token    *TokenService
	// contains filtered or unexported fields
}

A Client manages communication with the Open Trivia API.

func NewClient

func NewClient(httpClient *http.Client) *Client

NewClient returns a new Open Trivia API client. If a nil httpClient is provided, http.DefaultClient will be used.

func (*Client) Do

func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error)

Do sends an API request and returns an API response.The API response is decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred.

func (*Client) NewRequest

func (c *Client) NewRequest(r string, q url.Values) (*http.Request, error)

NewRequest creates an API request.

type Question

type Question struct {
	Category         string   `json:"category"`
	Type             string   `json:"type"`
	Difficulty       string   `json:"difficulty"`
	Question         string   `json:"question"`
	CorrectAnswer    string   `json:"correct_answer"`
	IncorrectAnswers []string `json:"incorrect_answers"`
}

Question is the model of the Open Trivia API Question related methods.

func (*Question) IsAnswerCorrect

func (q *Question) IsAnswerCorrect(answer string) bool

IsAnswerCorrect helps to find out if the provided answer is correct

func (*Question) ShuffleAnswers

func (q *Question) ShuffleAnswers() []string

ShuffleAnswers merging the correct answer with the incorrect answers

type QuestionCategory

type QuestionCategory uint8

QuestionCategory is the type for category option.

const (
	// QuestionCategoryGeneralKnowledge is the value for
	// "general knowledge" category type.
	QuestionCategoryGeneralKnowledge QuestionCategory = 9

	// QuestionCategoryBook is the value for "book" category type.
	QuestionCategoryBook QuestionCategory = 10

	// QuestionCategoryFilm is the value for "film" category type.
	QuestionCategoryFilm QuestionCategory = 11

	// QuestionCategoryMusic is the value for "music" category type.
	QuestionCategoryMusic QuestionCategory = 12

	// QuestionCategoryMusical is the value for "musical" category type.
	QuestionCategoryMusical QuestionCategory = 13

	// QuestionCategoryTelevision is the value for "television" category type.
	QuestionCategoryTelevision QuestionCategory = 14

	// QuestionCategoryVideoGame is the value for "video game" category type.
	QuestionCategoryVideoGame QuestionCategory = 15

	// QuestionCategoryBoardGame is the value for "board game" category type.
	QuestionCategoryBoardGame QuestionCategory = 16

	// QuestionCategoryNature is the value for "nature" category type.
	QuestionCategoryNature QuestionCategory = 17

	// QuestionCategoryComputer is the value for "computer" category type.
	QuestionCategoryComputer QuestionCategory = 18

	// QuestionCategoryMath is the value for "math" category type.
	QuestionCategoryMath QuestionCategory = 19

	// QuestionCategoryMythology is the value for "mythology" category type.
	QuestionCategoryMythology QuestionCategory = 20

	// QuestionCategorySport is the value for "sport" category type.
	QuestionCategorySport QuestionCategory = 21

	// QuestionCategoryGeography is the value for "geography" category type.
	QuestionCategoryGeography QuestionCategory = 22

	// QuestionCategoryHistory is the value for "history" category type.
	QuestionCategoryHistory QuestionCategory = 23

	// QuestionCategoryPolitics is the value for "politics" category type.
	QuestionCategoryPolitics QuestionCategory = 24

	// QuestionCategoryArt is the value for "art" category type.
	QuestionCategoryArt QuestionCategory = 25

	// QuestionCategoryCelebrity is the value for "celebrity" category type.
	QuestionCategoryCelebrity QuestionCategory = 26

	// QuestionCategoryAnimal is the value for "animal" category type.
	QuestionCategoryAnimal QuestionCategory = 27

	// QuestionCategoryVehicles is the value for "vehicle" category type.
	QuestionCategoryVehicles QuestionCategory = 28

	// QuestionCategoryComic is the value for "comic" category type.
	QuestionCategoryComic QuestionCategory = 29

	// QuestionCategoryGadget is the value for "gadget" category type.
	QuestionCategoryGadget QuestionCategory = 30

	// QuestionCategoryAnime is the value for "anime" category type.
	QuestionCategoryAnime QuestionCategory = 31

	// QuestionCategoryCartoon is the value for "cartoon" category type.
	QuestionCategoryCartoon QuestionCategory = 32
)

type QuestionDifficulty

type QuestionDifficulty string

QuestionDifficulty is the type for difficulty option.

const (
	// QuestionDifficultyEasy is the value for "easy" difficulty type.
	QuestionDifficultyEasy QuestionDifficulty = "easy"

	// QuestionDifficultyMedium is the value for "medium" difficulty type.
	QuestionDifficultyMedium QuestionDifficulty = "medium"

	// QuestionDifficultyHard is the value for "hard" difficulty type.
	QuestionDifficultyHard QuestionDifficulty = "hard"
)

type QuestionListOptions

type QuestionListOptions struct {
	// If true, the request will refresh the provided token when needed.
	AutoRefresh bool `url:"-"`

	// The maximum limit is 50.
	Category   QuestionCategory   `url:"category,omitempty"`
	Difficulty QuestionDifficulty `url:"difficulty,omitempty"`
	Limit      uint8              `url:"amount,omitempty"`
	Token      Token              `url:"token,omitempty"`
	Type       QuestionType       `url:"type,omitempty"`
}

QuestionListOptions are the options for QuestionService List method.

type QuestionRandomOptions

type QuestionRandomOptions struct {
	// If true, the request will refresh the provided token when needed.
	AutoRefresh bool `url:"-"`

	Category   QuestionCategory   `url:"category,omitempty"`
	Difficulty QuestionDifficulty `url:"difficulty,omitempty"`
	Token      Token              `url:"token,omitempty"`
	Type       QuestionType       `url:"type,omitempty"`
}

QuestionRandomOptions are the options for QuestionService Random method.

type QuestionService

type QuestionService service

QuestionService handles communication with the question related methods of the Open Trivia API.

Ref.: https://opentdb.com/api_config.php

func (*QuestionService) List

func (q *QuestionService) List(options *QuestionListOptions) ([]Question, error)

List returns a list of random questions from Open Trivia API.

If options is nil, List will use opentrivia.DefaultQuestionListOptions.

func (*QuestionService) Random

func (q *QuestionService) Random(options *QuestionRandomOptions) (Question, error)

Random returns a random question from Open Trivia API.

If options is nil, Random will use opentrivia.DefaultQuestionRandomOptions.

type QuestionType

type QuestionType string

QuestionType is the type for question type option.

const (
	// QuestionTypeMultiple is the value for "multiple" question type.
	QuestionTypeMultiple QuestionType = "multiple"

	// QuestionTypeTrueFalse is the value for "true/false" question type.
	QuestionTypeTrueFalse QuestionType = "boolean"
)

type Token

type Token string

Token is the type for tokens.

type TokenService

type TokenService service

TokenService handles communication with the token related methods of the Open Trivia API

Ref.: https://opentdb.com/api_config.php

func (*TokenService) Create

func (t *TokenService) Create() (Token, error)

Create returns a brand new token from Open Trivia API. Each token provides the guarantee that every new requested question was not already retrieved.

By sending a token to an API Call, the API will never return the same question twice.

If all questions for a given category has already been returned, the request will return an opentrivia.ErrTokenEmpty.

func (*TokenService) Refresh

func (t *TokenService) Refresh(token Token) (Token, error)

Refresh the provided token.

If the provided token is invalid, the request will return an opentrivia.ErrTokenNotFound.

Jump to

Keyboard shortcuts

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