|
1 | | -# client-go |
2 | | -Direct Decisions API v1 Go client |
| 1 | +# Direct Decisions API v1 Go client |
| 2 | + |
| 3 | +[](https://github.com/directdecisions/client-go/actions) |
| 4 | +[](https://pkg.go.dev/directdecisions.com/directdecisions) |
| 5 | +[](https://newreleases.io/github/directdecisions/client-go) |
| 6 | + |
| 7 | +Package directdecisions is a Go client library for accessing the [Direct Decisions](https://directdecisions.com) v1 API. |
| 8 | + |
| 9 | +You can view the client API docs here: [https://pkg.go.dev/directdecisions.com/directdecisions](https://pkg.go.dev/directdecisions.com/directdecisions) |
| 10 | + |
| 11 | +You can view Direct Decisions API v1 docs here: [https://api.directdecisions.com/v1](https://api.directdecisions.com/v1) |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +This package requires Go 1.20 version or later. |
| 16 | + |
| 17 | +Run `go get directdecisions.com/directdecisions` from command line. |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +```go |
| 22 | +import "directdecisions.com/directdecisions" |
| 23 | +``` |
| 24 | + |
| 25 | +Create a new Client, then use the exposed services to access different parts of the API. |
| 26 | + |
| 27 | +## Features |
| 28 | + |
| 29 | +This client implements all Direct API features. |
| 30 | + |
| 31 | +- Create votings |
| 32 | +- Retrieve voting information |
| 33 | +- Set voting choices |
| 34 | +- Delete votings |
| 35 | +- Vote with a ballot |
| 36 | +- Unvote |
| 37 | +- Get submitted ballot |
| 38 | +- Calculate results |
| 39 | + |
| 40 | +## Examples |
| 41 | + |
| 42 | +To run a voting: |
| 43 | + |
| 44 | +```go |
| 45 | +package main |
| 46 | + |
| 47 | +import ( |
| 48 | + "context" |
| 49 | + "log" |
| 50 | + |
| 51 | + "directdecisions.com/directdecisions" |
| 52 | +) |
| 53 | + |
| 54 | +func main() { |
| 55 | + client := directdecisions.NewClient("my-api-key", nil) |
| 56 | + |
| 57 | + ctx := context.Background() |
| 58 | + |
| 59 | + v, err := client.Votings.Create(ctx, []string{"Margarita", "Pepperoni", "Capricciosa"}) |
| 60 | + if err != nil { |
| 61 | + log.Fatal(err) |
| 62 | + } |
| 63 | + |
| 64 | + log.Printf("Created voting with ID %s", v.ID) |
| 65 | + |
| 66 | + if _, err := client.Votings.Vote(ctx, v.ID, "Leonardo", map[string]int{ |
| 67 | + "Pepperoni": 1, |
| 68 | + "Margarita": 2, |
| 69 | + }); err != nil { |
| 70 | + log.Fatal(err) |
| 71 | + } |
| 72 | + |
| 73 | + log.Printf("Leonardo Voted for Pepperoni in voting with ID %s", v.ID) |
| 74 | + |
| 75 | + if _, err := client.Votings.Vote(ctx, v.ID, "Michelangelo", map[string]int{ |
| 76 | + "Capricciosa": 1, |
| 77 | + "Margarita": 2, |
| 78 | + "Pepperoni": 2, |
| 79 | + }); err != nil { |
| 80 | + log.Fatal(err) |
| 81 | + } |
| 82 | + |
| 83 | + log.Printf("Michelangelo Voted for Capricciosa in voting with ID %s", v.ID) |
| 84 | + |
| 85 | + results, tie, err := client.Votings.Results(ctx, v.ID) |
| 86 | + if err != nil { |
| 87 | + log.Fatal(err) |
| 88 | + } |
| 89 | + |
| 90 | + if tie { |
| 91 | + log.Printf("Voting with ID %s is tied", v.ID) |
| 92 | + } else { |
| 93 | + log.Printf("Voting with ID %s is not tied", v.ID) |
| 94 | + } |
| 95 | + |
| 96 | + log.Printf("Results for voting with ID %s: %v", v.ID, results) |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +Error handling with Go 1.20 multiple errors wrapped: |
| 101 | + |
| 102 | +```go |
| 103 | +package main |
| 104 | + |
| 105 | +import ( |
| 106 | + "context" |
| 107 | + "log" |
| 108 | + |
| 109 | + "directdecisions.com/directdecisions" |
| 110 | +) |
| 111 | + |
| 112 | +func main() { |
| 113 | + client := directdecisions.NewClient("my-api-key", nil) |
| 114 | + |
| 115 | + ctx := context.Background() |
| 116 | + |
| 117 | + _, err := client.Votings.Create(ctx, []string{"Margarita", "Pepperoni", "Capricciosa"}) |
| 118 | + if err != nil { |
| 119 | + if errors.Is(err, directdecisions.ErrHTTPStatusUnauthorized) { |
| 120 | + log.Fatal("Invalid API key") |
| 121 | + } |
| 122 | + if errors.Is(err, directdecisions.ErrChoiceTooLong) { |
| 123 | + log.Fatal("Some of the choices are too long") |
| 124 | + } |
| 125 | + // ... |
| 126 | + log.Fatal(err) |
| 127 | + } |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +## Versioning |
| 132 | + |
| 133 | +Each version of the client is tagged and the version is updated accordingly. |
| 134 | + |
| 135 | +This package uses Go modules. |
| 136 | + |
| 137 | +To see the list of past versions, run `git tag`. |
| 138 | + |
| 139 | +## Contributing |
| 140 | + |
| 141 | +We love pull requests! Please see the [contribution guidelines](CONTRIBUTING.md). |
| 142 | + |
| 143 | +## License |
| 144 | + |
| 145 | +This library is distributed under the BSD-style license found in the [LICENSE](LICENSE) file. |
0 commit comments