challenge-sync-airbus: Ready for 2024
This commit is contained in:
parent
5a6d9047c2
commit
ac966f9023
5 changed files with 273 additions and 115 deletions
|
|
@ -1,22 +1,21 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type AirbusAPI struct {
|
||||
BaseURL string
|
||||
Token string
|
||||
SessionID int64
|
||||
SessionUUID string
|
||||
BaseURL string
|
||||
Token string
|
||||
SessionID int64
|
||||
InsecureSkipVerify bool
|
||||
}
|
||||
|
||||
func (a *AirbusAPI) request(method, endpoint string, data io.Reader, out interface{}) error {
|
||||
|
|
@ -25,7 +24,7 @@ func (a *AirbusAPI) request(method, endpoint string, data io.Reader, out interfa
|
|||
|
||||
client := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: a.InsecureSkipVerify},
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -39,7 +38,8 @@ func (a *AirbusAPI) request(method, endpoint string, data io.Reader, out interfa
|
|||
}
|
||||
|
||||
req.Header.Add("Authorization", "Bearer "+a.Token)
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
|
|
@ -179,68 +179,21 @@ func (a *AirbusAPI) AwardUser(team *AirbusTeam, value int64, message string) (er
|
|||
Value: value,
|
||||
}
|
||||
|
||||
log.Printf("AwardUser: %v", awards)
|
||||
var marshalled []byte
|
||||
marshalled, err = json.Marshal(awards)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("AwardUser: %s", marshalled)
|
||||
if dryRun {
|
||||
return
|
||||
}
|
||||
|
||||
data := url.Values{}
|
||||
data.Set("gaming_user_id", fmt.Sprintf("%d", awards.UserId))
|
||||
data.Set("name", awards.Message)
|
||||
data.Set("value", fmt.Sprintf("%d", awards.Value))
|
||||
|
||||
err = a.request("POST", fmt.Sprintf("/v1/sessions/%d/awards", a.SessionID), strings.NewReader(data.Encode()), nil)
|
||||
err = a.request("POST", fmt.Sprintf("/v1/sessions/%d/awards", a.SessionID), bytes.NewReader(marshalled), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
type AirbusStats struct {
|
||||
Data AirbusStatsData `json:"data"`
|
||||
}
|
||||
|
||||
type AirbusStatsData struct {
|
||||
BySessions []AirbusStatsSession `json:"by_sessions"`
|
||||
}
|
||||
|
||||
func (s AirbusStatsData) GetSession(sessionId AirbusUUID) *AirbusStatsSession {
|
||||
for _, session := range s.BySessions {
|
||||
if session.UUID == sessionId {
|
||||
return &session
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type AirbusUUID string
|
||||
|
||||
type AirbusStatsSession struct {
|
||||
UUID AirbusUUID `json:"uuid"`
|
||||
Name string `json:"name"`
|
||||
Duration string `json:"duration"`
|
||||
TeamStats []AirbusTeamStats `json:"team_stats"`
|
||||
}
|
||||
|
||||
func (s AirbusStatsSession) GetTeam(teamId AirbusUUID) *AirbusTeamStats {
|
||||
for _, team := range s.TeamStats {
|
||||
if team.UUID == teamId {
|
||||
return &team
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type AirbusTeamStats struct {
|
||||
UUID AirbusUUID `json:"uuid"`
|
||||
Name string `json:"name"`
|
||||
Score int64 `json:"score"`
|
||||
}
|
||||
|
||||
func (a *AirbusAPI) GetCurrentStats() (stats AirbusStats, err error) {
|
||||
err = a.request("GET", "/stats", nil, &stats)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue