challenge-sync-airbus: Done

This commit is contained in:
nemunaire 2023-04-06 15:37:05 +02:00
commit 0d5b87b3f7
6 changed files with 73 additions and 68 deletions

View file

@ -1,14 +1,15 @@
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strconv"
"strings"
)
type AirbusAPI struct {
@ -18,14 +19,18 @@ type AirbusAPI struct {
SessionUUID string
}
func (a *AirbusAPI) request(method, endpoint string, data []byte, out interface{}) error {
func (a *AirbusAPI) request(method, endpoint string, data io.Reader, out interface{}) error {
var req *http.Request
var err error
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
if data != nil {
req, err = http.NewRequest(method, a.BaseURL+endpoint, bytes.NewReader(data))
req, err = http.NewRequest(method, a.BaseURL+endpoint, data)
} else {
req, err = http.NewRequest(method, a.BaseURL+endpoint, nil)
}
@ -34,9 +39,9 @@ func (a *AirbusAPI) request(method, endpoint string, data []byte, out interface{
}
req.Header.Add("Authorization", "Bearer "+a.Token)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error during request execution to %q: %w", endpoint, err)
}
@ -152,8 +157,8 @@ func (a *AirbusAPI) GetChallengeFromName(name string) (*AirbusChallenge, error)
}
func (a *AirbusAPI) ValidateChallengeFromUser(team *AirbusTeam, challengeId AirbusChallengeId) (err error) {
log.Printf("ValidateChallenge: %d, %s, %d", a.SessionID, challengeId.String(), team.Members[0].ID)
if dryRun {
log.Printf("ValidateChallenge: %d, %s, %d", a.SessionID, challengeId.String(), team.Members[0].ID)
return
}
@ -174,17 +179,17 @@ func (a *AirbusAPI) AwardUser(team *AirbusTeam, value int64, message string) (er
Value: value,
}
log.Printf("AwardUser: %v", awards)
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)
}
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), j, nil)
err = a.request("POST", fmt.Sprintf("/v1/sessions/%d/awards", a.SessionID), strings.NewReader(data.Encode()), nil)
if err != nil {
return err
}