Documentation
¶
Overview ¶
package directdecisions is the Direct Decisions API v1 client for Go.
Example (ErrorHandling) ¶
package main
import (
"context"
"errors"
"log"
"directdecisions.com/directdecisions"
)
func main() {
client := directdecisions.NewClient("my-api-key", nil)
ctx := context.Background()
_, err := client.Votings.Create(ctx, []string{"Margarita", "Pepperoni", "Capricciosa"})
if err != nil {
if errors.Is(err, directdecisions.ErrHTTPStatusUnauthorized) {
log.Fatal("Invalid API key")
}
if errors.Is(err, directdecisions.ErrChoiceTooLong) {
log.Fatal("Some of the choices are too long")
}
// ...
log.Fatal(err)
}
}
Index ¶
- Variables
- type ChoiceStrength
- type Client
- type ClientOptions
- type Duel
- type Rate
- type Result
- type Voting
- type VotingsService
- func (s *VotingsService) Ballot(ctx context.Context, votingID, voterID string) (ballot map[string]int, err error)
- func (s *VotingsService) Create(ctx context.Context, choices []string) (v *Voting, err error)
- func (s *VotingsService) Delete(ctx context.Context, votingID string) (err error)
- func (s *VotingsService) Duels(ctx context.Context, votingID string) (results []Result, duels []Duel, tie bool, err error)
- func (s *VotingsService) Results(ctx context.Context, votingID string) (results []Result, tie bool, err error)
- func (s *VotingsService) Set(ctx context.Context, votingID, choice string, index int) (choices []string, err error)
- func (s *VotingsService) Unvote(ctx context.Context, votingID, voterID string) error
- func (s *VotingsService) Vote(ctx context.Context, votingID, voterID string, ballot map[string]int) (revoted bool, err error)
- func (s *VotingsService) Voting(ctx context.Context, votingID string) (v *Voting, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrHTTPStatusBadRequest = errors.New("http status: " + http.StatusText(http.StatusBadRequest)) ErrHTTPStatusForbidden = errors.New("http status: " + http.StatusText(http.StatusForbidden)) ErrHTTPStatusNotFound = errors.New("http status: " + http.StatusText(http.StatusNotFound)) ErrHTTPStatusMethodNotAllowed = errors.New("http status: " + http.StatusText(http.StatusMethodNotAllowed)) ErrHTTPStatusTooManyRequests = errors.New("http status: " + http.StatusText(http.StatusTooManyRequests)) ErrHTTPStatusInternalServerError = errors.New("http status: " + http.StatusText(http.StatusInternalServerError)) ErrHTTPStatusBadGateway = errors.New("http status: " + http.StatusText(http.StatusBadGateway)) ErrInvalidData = errors.New("Invalid Data") ErrMissingChoices = errors.New("Missing Choices") ErrChoiceRequired = errors.New("Choice Required") ErrChoiceTooLong = errors.New("Choice Too Long") ErrTooManyChoices = errors.New("Too Many Choices") ErrBallotRequired = errors.New("Ballot Required") ErrVoterIDTooLong = errors.New("Voter ID Too Long") ErrInvalidVoterID = errors.New("Invalid Voter ID") )
Errors that are returned by the API.
Functions ¶
This section is empty.
Types ¶
type ChoiceStrength ¶ added in v0.2.1
type Client ¶
type Client struct {
// Services that API provides.
Votings *VotingsService
// contains filtered or unexported fields
}
Client manages communication with the Direct Decisions API.
func NewClient ¶
func NewClient(key string, o *ClientOptions) (c *Client)
NewClient constructs a new Client that uses API key authentication.
Example ¶
package main
import (
"context"
"log"
"directdecisions.com/directdecisions"
)
func main() {
client := directdecisions.NewClient("my-api-key", nil)
ctx := context.Background()
v, err := client.Votings.Create(ctx, []string{"Margarita", "Pepperoni", "Capricciosa"})
if err != nil {
log.Fatal(err)
}
log.Printf("Created voting with ID %s", v.ID)
if _, err := client.Votings.Vote(ctx, v.ID, "Leonardo", map[string]int{
"Pepperoni": 1,
"Margarita": 2,
}); err != nil {
log.Fatal(err)
}
log.Printf("Leonardo Voted for Pepperoni in voting with ID %s", v.ID)
if _, err := client.Votings.Vote(ctx, v.ID, "Michelangelo", map[string]int{
"Capricciosa": 1,
"Margarita": 2,
"Pepperoni": 2,
}); err != nil {
log.Fatal(err)
}
log.Printf("Michelangelo Voted for Capricciosa in voting with ID %s", v.ID)
results, tie, err := client.Votings.Results(ctx, v.ID)
if err != nil {
log.Fatal(err)
}
if tie {
log.Printf("Voting with ID %s is tied", v.ID)
} else {
log.Printf("Voting with ID %s is not tied", v.ID)
}
log.Printf("Results for voting with ID %s: %v", v.ID, results)
}
type ClientOptions ¶
ClientOptions holds optional parameters for the Client.
type Duel ¶ added in v0.2.1
type Duel struct {
Left ChoiceStrength
Right ChoiceStrength
}
type Rate ¶
type Rate struct {
Limit int // The maximum number of requests that the user is permitted to make per hour.
Remaining int // The number of requests remaining in the current rate limit window.
Reset time.Time // Seconds until current rate limit window will reset to the maximal value.
Retry time.Time // Seconds until new requests are permitted when limit is reached.
}
Rate contains the request rate limit information.
type VotingsService ¶
type VotingsService service
VotingsService provides information and methods to manage votings and ballots.
func (*VotingsService) Delete ¶
func (s *VotingsService) Delete(ctx context.Context, votingID string) (err error)
Delete removes a voting referenced by its ID.
func (*VotingsService) Set ¶
func (s *VotingsService) Set(ctx context.Context, votingID, choice string, index int) (choices []string, err error)
Set adds, moves or removes a choice in a voting.
func (*VotingsService) Unvote ¶
func (s *VotingsService) Unvote(ctx context.Context, votingID, voterID string) error
Click to show internal directories.
Click to hide internal directories.