Skip to content

Commit 955b851

Browse files
authored
Initial client implementation (#1)
1 parent 4c7b079 commit 955b851

File tree

18 files changed

+1338
-2
lines changed

18 files changed

+1338
-2
lines changed

.github/workflows/go.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Go
2+
on: [push, pull_request]
3+
jobs:
4+
5+
build:
6+
name: Build
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
matrix:
10+
os: [ubuntu-latest, macos-latest, windows-latest]
11+
12+
steps:
13+
14+
- name: Set up Go
15+
uses: actions/setup-go@v3
16+
with:
17+
go-version: 1.20.0-rc.1
18+
check-latest: true
19+
20+
- name: Checkout
21+
uses: actions/checkout@v1
22+
with:
23+
fetch-depth: 1
24+
25+
- name: Cache Go modules
26+
uses: actions/cache@v1
27+
with:
28+
path: ~/go/pkg/mod
29+
key: ${{ runner.os }}-build-${{ hashFiles('**/go.sum') }}
30+
restore-keys: |
31+
${{ runner.OS }}-build-${{ env.cache-name }}-
32+
${{ runner.OS }}-build-
33+
${{ runner.OS }}-
34+
35+
# TODO: Enable linter when Go 1.20 is supported
36+
# - name: Lint
37+
# uses: golangci/golangci-lint-action@v2
38+
# with:
39+
# version: v1.50.1
40+
# args: --timeout 10m
41+
42+
- name: Vet
43+
if: matrix.os == 'ubuntu-latest'
44+
run: go vet -v ./...
45+
46+
- name: Build
47+
env:
48+
CGO_ENABLED: 0
49+
run: go build -ldflags "-s -w" ./...
50+
51+
- name: Test
52+
run: go test -v -race ./...

AUTHORS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# This is the official list of Direct Decisions Go client authors for copyright purposes.
2+
3+
Janoš Guljaš <janos@resenje.org>

CONTRIBUTING.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# How to contribute
2+
3+
We'd love to accept your patches and contributions to this project. There are just a few small guidelines you need to follow.
4+
5+
1. Code should be `go fmt` formatted.
6+
2. Exported types, constants, variables and functions should be documented.
7+
3. Changes must be covered with tests.
8+
4. All tests must pass constantly `go test .`.
9+
10+
## Versioning
11+
12+
Direct Decisions Go client follows semantic versioning. New functionality should be accompanied by increment to the minor version number.
13+
14+
## Releasing
15+
16+
Any code which is complete, tested, reviewed, and merged to master can be released.
17+
18+
1. Update the `version` number in `directdecisions.go`.
19+
2. Make a pull request with these changes.
20+
3. Once the pull request has been merged, visit [https://github.com/directdecisions/client-go/releases](https://github.com/directdecisions/client-go/release) and click `Draft a new release`.
21+
4. Update the `Tag version` and `Release title` field with the new Direct Decisions Go client version. Be sure the version has a `v` prefixed in both places, e.g. `v1.25.0`.
22+
5. Publish the release.

LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2022, Direct Decisions Go client AUTHORS.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
* Neither the name of this project nor the names of its
15+
contributors may be used to endorse or promote products derived from
16+
this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 145 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,145 @@
1-
# client-go
2-
Direct Decisions API v1 Go client
1+
# Direct Decisions API v1 Go client
2+
3+
[![Go](https://github.com/directdecisions/client-go/workflows/Go/badge.svg)](https://github.com/directdecisions/client-go/actions)
4+
[![PkgGoDev](https://pkg.go.dev/badge/directdecisions.com/directdecisions)](https://pkg.go.dev/directdecisions.com/directdecisions)
5+
[![NewReleases](https://newreleases.io/badge.svg)](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

Comments
 (0)