-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoken.go
112 lines (92 loc) · 2.69 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package opentrivia
import "github.com/pkg/errors"
import "github.com/google/go-querystring/query"
type (
// Token is the type for tokens.
Token string
tokenCommand string
)
const (
tokenCommandCreate tokenCommand = "request"
tokenCommandRefresh tokenCommand = "reset"
)
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")
)
type tokenOptions struct {
Command tokenCommand `url:"command,omitempty"`
Token Token `url:"token,omitempty"`
}
type tokenResponse struct {
ResponseCode responseCode `json:"response_code,omitempty"`
ResponseMessage string `json:"response_message,omitempty"`
Token Token `json:"token,omitempty"`
}
// TokenService handles communication with the token related
// methods of the Open Trivia API
//
// Ref.: https://opentdb.com/api_config.php
type TokenService service
// 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 (t *TokenService) Create() (Token, error) {
options := &tokenOptions{
Command: tokenCommandCreate,
}
v, err := query.Values(options)
if err != nil {
return "", err
}
req, err := t.client.NewRequest(defaultTokenRoute, v)
if err != nil {
return "", err
}
var resp tokenResponse
if _, err := t.client.Do(req, &resp); err != nil {
return "", err
}
return resp.Token, nil
}
// Refresh the provided token.
//
// If the provided token is invalid, the request will return an
// opentrivia.ErrTokenNotFound.
func (t *TokenService) Refresh(token Token) (Token, error) {
options := &tokenOptions{
Command: tokenCommandRefresh,
Token: token,
}
v, err := query.Values(options)
if err != nil {
return "", err
}
req, err := t.client.NewRequest(defaultTokenRoute, v)
if err != nil {
return "", err
}
var resp tokenResponse
if _, err := t.client.Do(req, &resp); err != nil {
return "", err
}
switch resp.ResponseCode {
case responseCodeInvalidParameter:
return "", ErrInvalidParameter
case responseCodeTokenNotFound:
return "", ErrTokenNotFound
}
return resp.Token, nil
}