challenge-sync-airbus: Do job

This commit is contained in:
nemunaire 2023-04-06 03:48:52 +02:00
commit 3344e05e0d
6 changed files with 256 additions and 65 deletions

View file

@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strconv"
)
@ -32,6 +33,7 @@ func (a *AirbusAPI) request(method, endpoint string, data []byte, out interface{
return fmt.Errorf("unable to prepare request to %q: %w", endpoint, err)
}
req.Header.Add("Authorization", "Bearer "+a.Token)
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
@ -40,7 +42,7 @@ func (a *AirbusAPI) request(method, endpoint string, data []byte, out interface{
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusOK {
if out != nil {
jdec := json.NewDecoder(resp.Body)
@ -149,30 +151,44 @@ func (a *AirbusAPI) GetChallengeFromName(name string) (*AirbusChallenge, error)
return nil, fmt.Errorf("unable to find challenge %q", name)
}
func (a *AirbusAPI) ValidateChallengeFromUser(userId AirbusUserId, challengeId AirbusChallengeId) (err error) {
err = a.request("GET", fmt.Sprintf("/v1/sessions/%d/%s/%s/validate", a.SessionID, challengeId.String(), userId.String()), nil, nil)
func (a *AirbusAPI) ValidateChallengeFromUser(team *AirbusTeam, challengeId AirbusChallengeId) (err error) {
if dryRun {
log.Printf("ValidateChallenge: %d, %s, %d", a.SessionID, challengeId.String(), team.Members[0].ID)
return
}
err = a.request("GET", fmt.Sprintf("/v1/sessions/%d/%s/%d/validate", a.SessionID, challengeId.String(), team.Members[0].ID), nil, nil)
return
}
type AirbusUserAwards struct {
UserId AirbusUserId `json:"gaming_user_id"`
Message string `json:"name"`
Value int64 `json:"value"`
UserId int64 `json:"gaming_user_id"`
Message string `json:"name"`
Value int64 `json:"value"`
}
func (a *AirbusAPI) AwardUser(userId AirbusUserId, value int64, message string) (err error) {
func (a *AirbusAPI) AwardUser(team *AirbusTeam, value int64, message string) (err error) {
awards := AirbusUserAwards{
UserId: userId,
UserId: team.Members[0].ID,
Message: message,
Value: value,
}
if dryRun {
log.Printf("AwardUser: %v", awards)
return
}
j, err := json.Marshal(awards)
if err != nil {
return fmt.Errorf("unable to marshall JSON from awards struct: %w", err)
}
err = a.request("POST", fmt.Sprintf("/v1/sessions/%d/awards", a.SessionID), j, nil)
if err != nil {
return err
}
return
}