-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbank.go
67 lines (60 loc) · 1.54 KB
/
bank.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func computePointsDiff(currentPoints, previousPoints map[int]uint64) map[int]uint64 {
diff := make(map[int]uint64)
for team, current := range currentPoints {
previous := previousPoints[team]
diff[team] = current - previous
log.Printf("Team %d: %d - %d = %d\n", team, current, previous, diff[team])
}
return diff
}
type Deposit struct {
Team int `json:"team"`
Amount string `json:"amount"`
}
func makeDeposit(client http.Client, teamPoints map[int]uint64) {
depositUrl := conf.BankBaseUrl + "/api/bank/accounts/deposits/"
var deposits []Deposit
for team, points := range teamPoints {
deposit := Deposit{
Team: team,
Amount: fmt.Sprintf("%d.00", points*conf.BankAmountPerPoint),
}
deposits = append(deposits, deposit)
}
data := map[string]interface{}{
"deposits": deposits,
"description": "Payment for service uptime",
}
jsonData, err := json.Marshal(data)
if err != nil {
panic(err)
}
req, err := http.NewRequest("POST", depositUrl, bytes.NewBuffer(jsonData))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(conf.BankUsername, conf.BankPassword)
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
var errBody string
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
errBody = string(body)
}
panic(fmt.Sprintf("Failed to deposit, Code: %s. Error: %s", resp.Status, errBody))
}
}