Skip to content

Commit

Permalink
progress with example 1/x
Browse files Browse the repository at this point in the history
  • Loading branch information
natural committed Nov 24, 2023
1 parent bfafb34 commit 7e05c0a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
52 changes: 52 additions & 0 deletions example/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"fmt"
"math/rand"
"time"

"github.com/vements/client-go/vements"
)

const (
API_KEY = ""
PROJECT_ID = ""
SCOREBOARD_ID = ""
)

func main() {
client, _ := vements.NewClient(API_KEY, []string{"production"}, vements.NewConfig())
id := rand.Intn(1000)

participantCreateResponse, _ := client.Participant.Create(
vements.ParticipantCreateRequest{
ProjectId: PROJECT_ID,
Display: fmt.Sprintf("Example Player %v", id),
ExternalId: fmt.Sprintf("example player %v", id),
Image: "",
Extra: nil,
},
)

participant := participantCreateResponse.InsertParticipantOne

fmt.Printf("Participant Created: %+v\n", participantCreateResponse)

for i := 0; i < 5; i++ {
_, _ = client.Scoreboard.Record(
SCOREBOARD_ID,
vements.ScoreboardScoreRequest{
ParticipantId: participant.ParticipantId,
Value: rand.Intn(1000) + 1,
Recorded: time.Now(),
},
)
}

scoresResponse, _ := client.Scoreboard.Scores(SCOREBOARD_ID, time.Now().Add(time.Hour*-24), time.Now())
scores := scoresResponse.ScoreboardScores

for _, score := range scores {
fmt.Printf("Rank: %v Player: %v Total: %v\n", score.Rank, score.Participant.Display, score.Total)
}
}
36 changes: 36 additions & 0 deletions example/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## Go Example for Vements

This directory contains an example of how to use the Vements Client Library for Go to create a client and use that client to create a new participant, scores for that participant, and finally view a scoreboard with those scores.

### Prerequisites

Before you can continue, you must have the following:

- API key
- Project ID
- Scoreboard ID

To create these resources and get their IDs, create a trial or paid account at [Vements](https://vements.io). Then log into the dashboard and create a new project. Once you have a project, you can create an API key and a scoreboard. Important: create a "read-write" API key. The example will not work with a "read-only" API key.

### Running the Example

To run the example, first clone the repo:

```shell
$ git clone https://github.com/vements/client-go
```

Then update the `example.go` file to use your Vements API key and the ID of your project. Change the following lines:

```go
API_KEY = "put your API key here"
PROJECT_ID = "put your project ID here"
SCOREBOARD_ID = "put your scoreboard ID here"
```

Then run the example:

```shell
$ cd client-go
$ go run example/example.go
```
4 changes: 2 additions & 2 deletions vements/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,8 @@ func (endpoint *ScoreboardEndpoint) Scores(

res, err := endpoint.Request().
SetResult(&val).
SetQueryParam("from", fmt.Sprintf("%v", from)).
SetQueryParam("to", fmt.Sprintf("%v", to)).
SetQueryParam("from", from.Format(time.RFC3339)).
SetQueryParam("to", to.Format(time.RFC3339)).
Get(endpoint.BaseUrl + url)

if res.StatusCode() > 299 || res.StatusCode() < 200 {
Expand Down

0 comments on commit 7e05c0a

Please sign in to comment.