server/remote/challenge-sync-airbus/api.go

200 lines
4.7 KiB
Go
Raw Normal View History

2022-06-06 10:55:39 +00:00
package main
import (
2024-03-19 10:49:39 +00:00
"bytes"
2022-06-07 22:09:49 +00:00
"crypto/tls"
2022-06-06 10:55:39 +00:00
"encoding/json"
"fmt"
"io"
2023-04-06 01:48:52 +00:00
"log"
2022-06-06 10:55:39 +00:00
"net/http"
"strconv"
)
type AirbusAPI struct {
2024-03-19 10:49:39 +00:00
BaseURL string
Token string
SessionID int64
InsecureSkipVerify bool
2022-06-06 10:55:39 +00:00
}
2023-04-06 13:37:05 +00:00
func (a *AirbusAPI) request(method, endpoint string, data io.Reader, out interface{}) error {
2022-06-07 22:09:49 +00:00
var req *http.Request
var err error
2023-04-06 13:37:05 +00:00
client := &http.Client{
Transport: &http.Transport{
2024-03-19 10:49:39 +00:00
TLSClientConfig: &tls.Config{InsecureSkipVerify: a.InsecureSkipVerify},
2023-04-06 13:37:05 +00:00
},
}
2022-06-07 22:09:49 +00:00
2022-06-06 10:55:39 +00:00
if data != nil {
2023-04-06 13:37:05 +00:00
req, err = http.NewRequest(method, a.BaseURL+endpoint, data)
2022-06-07 22:09:49 +00:00
} else {
req, err = http.NewRequest(method, a.BaseURL+endpoint, nil)
2022-06-06 10:55:39 +00:00
}
if err != nil {
return fmt.Errorf("unable to prepare request to %q: %w", endpoint, err)
}
2023-04-06 01:48:52 +00:00
req.Header.Add("Authorization", "Bearer "+a.Token)
2024-03-19 10:49:39 +00:00
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
2022-06-06 10:55:39 +00:00
2023-04-06 13:37:05 +00:00
resp, err := client.Do(req)
2022-06-06 10:55:39 +00:00
if err != nil {
return fmt.Errorf("error during request execution to %q: %w", endpoint, err)
}
defer resp.Body.Close()
2023-04-06 01:48:52 +00:00
if resp.StatusCode == http.StatusOK {
2022-06-06 10:55:39 +00:00
if out != nil {
jdec := json.NewDecoder(resp.Body)
if err := jdec.Decode(out); err != nil {
return fmt.Errorf("an error occurs when trying to decode response: %w", err)
}
}
} else if all, err := io.ReadAll(resp.Body); err != nil {
return fmt.Errorf("error returned by the API + error on decoding: %d // %w", resp.StatusCode, err)
} else {
return fmt.Errorf("error returned by the API: %d -> %s", resp.StatusCode, all)
}
return nil
}
type AirbusUserId int64
2022-06-07 14:06:36 +00:00
func NewAirbusUserId(externalid string) AirbusUserId {
v, _ := strconv.ParseInt(externalid, 10, 64)
return AirbusUserId(v)
}
2022-06-06 10:55:39 +00:00
func (aui AirbusUserId) String() string {
return strconv.FormatInt(int64(aui), 10)
}
2022-06-07 22:09:49 +00:00
type AirbusUserData struct {
Data []AirbusUser `json:"data"`
}
2022-06-06 10:55:39 +00:00
type AirbusUser struct {
2022-06-07 22:09:49 +00:00
Id AirbusUserId `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
Nickname string `json:"nickname"`
2022-06-06 10:55:39 +00:00
}
2022-06-07 22:09:49 +00:00
func (a *AirbusAPI) GetUsers() (users AirbusUserData, err error) {
2022-06-06 10:55:39 +00:00
err = a.request("GET", fmt.Sprintf("/sessions/%d/users", a.SessionID), nil, &users)
return
}
func (a *AirbusAPI) GetUserFromName(name string) (*AirbusUser, error) {
users, err := a.GetUsers()
if err != nil {
return nil, fmt.Errorf("unable to retrieve users list: %w", err)
}
2022-06-07 22:09:49 +00:00
for _, u := range users.Data {
2022-06-06 10:55:39 +00:00
if u.Name == name {
return &u, nil
}
}
return nil, fmt.Errorf("unable to find user %q", name)
}
2022-06-07 22:09:49 +00:00
func (a *AirbusAPI) GetUserFromEmail(email string) (*AirbusUser, error) {
users, err := a.GetUsers()
if err != nil {
return nil, fmt.Errorf("unable to retrieve users list: %w", err)
}
for _, u := range users.Data {
if u.Email == email {
return &u, nil
}
}
return nil, fmt.Errorf("unable to find user with email %q", email)
}
2022-06-06 10:55:39 +00:00
type AirbusChallengeId int64
func (aci AirbusChallengeId) String() string {
return strconv.FormatInt(int64(aci), 10)
}
2022-06-07 22:09:49 +00:00
type AirbusChallengeData struct {
Data []AirbusChallenge `json:"data"`
}
2022-06-06 10:55:39 +00:00
type AirbusChallenge struct {
Id AirbusChallengeId `json:"id"`
Name string `json:"name"`
}
2022-06-07 22:09:49 +00:00
func (a *AirbusAPI) GetChallenges() (challenges AirbusChallengeData, err error) {
err = a.request("GET", fmt.Sprintf("/v1/sessions/%d/challenges", a.SessionID), nil, &challenges)
2022-06-06 10:55:39 +00:00
return
}
func (a *AirbusAPI) GetChallengeFromName(name string) (*AirbusChallenge, error) {
challenges, err := a.GetChallenges()
if err != nil {
return nil, fmt.Errorf("unable to retrieve challenges list: %w", err)
}
2022-06-07 22:09:49 +00:00
for _, c := range challenges.Data {
2022-06-06 10:55:39 +00:00
if c.Name == name {
return &c, nil
}
}
return nil, fmt.Errorf("unable to find challenge %q", name)
}
2023-04-06 01:48:52 +00:00
func (a *AirbusAPI) ValidateChallengeFromUser(team *AirbusTeam, challengeId AirbusChallengeId) (err error) {
2023-04-06 13:37:05 +00:00
log.Printf("ValidateChallenge: %d, %s, %d", a.SessionID, challengeId.String(), team.Members[0].ID)
2023-04-06 01:48:52 +00:00
if dryRun {
return
}
err = a.request("GET", fmt.Sprintf("/v1/sessions/%d/%s/%d/validate", a.SessionID, challengeId.String(), team.Members[0].ID), nil, nil)
2022-06-06 10:55:39 +00:00
return
}
type AirbusUserAwards struct {
2023-04-06 01:48:52 +00:00
UserId int64 `json:"gaming_user_id"`
Message string `json:"name"`
Value int64 `json:"value"`
2022-06-06 10:55:39 +00:00
}
2023-04-06 01:48:52 +00:00
func (a *AirbusAPI) AwardUser(team *AirbusTeam, value int64, message string) (err error) {
2022-06-06 10:55:39 +00:00
awards := AirbusUserAwards{
2023-04-06 01:48:52 +00:00
UserId: team.Members[0].ID,
2022-06-06 10:55:39 +00:00
Message: message,
Value: value,
}
2024-03-19 10:49:39 +00:00
var marshalled []byte
marshalled, err = json.Marshal(awards)
if err != nil {
2023-04-06 01:48:52 +00:00
return
}
2024-03-19 10:49:39 +00:00
log.Printf("AwardUser: %s", marshalled)
if dryRun {
return
}
2022-06-06 10:55:39 +00:00
2024-03-19 10:49:39 +00:00
err = a.request("POST", fmt.Sprintf("/v1/sessions/%d/awards", a.SessionID), bytes.NewReader(marshalled), nil)
2023-04-06 01:48:52 +00:00
if err != nil {
return err
}
2022-06-06 10:55:39 +00:00
return
}