remote-challenge-sync-airbus: WIP
This commit is contained in:
parent
a82defe2a7
commit
0831ea6088
3 changed files with 65 additions and 22 deletions
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
|
@ -10,17 +11,23 @@ import (
|
|||
)
|
||||
|
||||
type AirbusAPI struct {
|
||||
BaseURL string
|
||||
Token string
|
||||
SessionID int64
|
||||
BaseURL string
|
||||
Token string
|
||||
SessionID int64
|
||||
SessionUUID string
|
||||
}
|
||||
|
||||
func (a *AirbusAPI) request(method, endpoint string, data []byte, out interface{}) error {
|
||||
var reader *bytes.Reader
|
||||
var req *http.Request
|
||||
var err error
|
||||
|
||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
|
||||
if data != nil {
|
||||
reader = bytes.NewReader(data)
|
||||
req, err = http.NewRequest(method, a.BaseURL+endpoint, bytes.NewReader(data))
|
||||
} else {
|
||||
req, err = http.NewRequest(method, a.BaseURL+endpoint, nil)
|
||||
}
|
||||
req, err := http.NewRequest(method, a.BaseURL+endpoint, reader)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to prepare request to %q: %w", endpoint, err)
|
||||
}
|
||||
|
|
@ -61,12 +68,18 @@ func (aui AirbusUserId) String() string {
|
|||
return strconv.FormatInt(int64(aui), 10)
|
||||
}
|
||||
|
||||
type AirbusUser struct {
|
||||
Id AirbusUserId `json:"id"`
|
||||
Name string `json:"name"`
|
||||
type AirbusUserData struct {
|
||||
Data []AirbusUser `json:"data"`
|
||||
}
|
||||
|
||||
func (a *AirbusAPI) GetUsers() (users []AirbusUser, err error) {
|
||||
type AirbusUser struct {
|
||||
Id AirbusUserId `json:"id"`
|
||||
Email string `json:"email"`
|
||||
Name string `json:"name"`
|
||||
Nickname string `json:"nickname"`
|
||||
}
|
||||
|
||||
func (a *AirbusAPI) GetUsers() (users AirbusUserData, err error) {
|
||||
err = a.request("GET", fmt.Sprintf("/sessions/%d/users", a.SessionID), nil, &users)
|
||||
return
|
||||
}
|
||||
|
|
@ -77,7 +90,7 @@ func (a *AirbusAPI) GetUserFromName(name string) (*AirbusUser, error) {
|
|||
return nil, fmt.Errorf("unable to retrieve users list: %w", err)
|
||||
}
|
||||
|
||||
for _, u := range users {
|
||||
for _, u := range users.Data {
|
||||
if u.Name == name {
|
||||
return &u, nil
|
||||
}
|
||||
|
|
@ -86,19 +99,38 @@ func (a *AirbusAPI) GetUserFromName(name string) (*AirbusUser, error) {
|
|||
return nil, fmt.Errorf("unable to find user %q", name)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
type AirbusChallengeId int64
|
||||
|
||||
func (aci AirbusChallengeId) String() string {
|
||||
return strconv.FormatInt(int64(aci), 10)
|
||||
}
|
||||
|
||||
type AirbusChallengeData struct {
|
||||
Data []AirbusChallenge `json:"data"`
|
||||
}
|
||||
|
||||
type AirbusChallenge struct {
|
||||
Id AirbusChallengeId `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func (a *AirbusAPI) GetChallenges() (challenges []AirbusChallenge, err error) {
|
||||
err = a.request("GET", fmt.Sprintf("/sessions/%d/challenges", a.SessionID), nil, &challenges)
|
||||
func (a *AirbusAPI) GetChallenges() (challenges AirbusChallengeData, err error) {
|
||||
err = a.request("GET", fmt.Sprintf("/v1/sessions/%d/challenges", a.SessionID), nil, &challenges)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -108,7 +140,7 @@ func (a *AirbusAPI) GetChallengeFromName(name string) (*AirbusChallenge, error)
|
|||
return nil, fmt.Errorf("unable to retrieve challenges list: %w", err)
|
||||
}
|
||||
|
||||
for _, c := range challenges {
|
||||
for _, c := range challenges.Data {
|
||||
if c.Name == name {
|
||||
return &c, nil
|
||||
}
|
||||
|
|
@ -118,7 +150,7 @@ func (a *AirbusAPI) GetChallengeFromName(name string) (*AirbusChallenge, error)
|
|||
}
|
||||
|
||||
func (a *AirbusAPI) ValidateChallengeFromUser(userId AirbusUserId, challengeId AirbusChallengeId) (err error) {
|
||||
err = a.request("GET", fmt.Sprintf("/sessions/%d/%s/%s/validate", a.SessionID, challengeId.String(), userId.String()), nil, nil)
|
||||
err = a.request("GET", fmt.Sprintf("/v1/sessions/%d/%s/%s/validate", a.SessionID, challengeId.String(), userId.String()), nil, nil)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -140,7 +172,7 @@ func (a *AirbusAPI) AwardUser(userId AirbusUserId, value int64, message string)
|
|||
return fmt.Errorf("unable to marshall JSON from awards struct: %w", err)
|
||||
}
|
||||
|
||||
err = a.request("POST", fmt.Sprintf("/sessions/%d/awards", a.SessionID), j, nil)
|
||||
err = a.request("POST", fmt.Sprintf("/v1/sessions/%d/awards", a.SessionID), j, nil)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
Reference in a new issue