Added retrieval of robot name from TBA.

This commit is contained in:
Patrick Fairbank
2015-09-01 22:37:34 -07:00
parent bcf7b965eb
commit 41e571ec62
2 changed files with 38 additions and 3 deletions

View File

@@ -275,6 +275,11 @@ func getOfficialTeamInfo(teamId int) (*Team, error) {
if tbaTeam.TeamNumber == 0 {
team = Team{Id: teamId}
} else {
robotName, err := getRobotNameFromTba(teamId, 2015)
if err != nil {
return nil, err
}
recentAwards, err := getTeamAwardsFromTba(teamId)
if err != nil {
return nil, err
@@ -290,9 +295,9 @@ func getOfficialTeamInfo(teamId int) (*Team, error) {
}
// Use those variables to make a team object
team = Team{Id: teamId, Name: tbaTeam.Name, Nickname: tbaTeam.Nickname,
City: tbaTeam.Locality, StateProv: tbaTeam.Reigon,
Country: tbaTeam.Country, RookieYear: tbaTeam.RookieYear, Accomplishments: accomplishmentsBuffer.String()}
team = Team{Id: teamId, Name: tbaTeam.Name, Nickname: tbaTeam.Nickname, City: tbaTeam.Locality,
StateProv: tbaTeam.Reigon, Country: tbaTeam.Country, RookieYear: tbaTeam.RookieYear,
RobotName: robotName, Accomplishments: accomplishmentsBuffer.String()}
}
} else {
// If team grab is disabled, just use the team number

30
tba.go
View File

@@ -18,6 +18,7 @@ import (
// Distinct endpoints are necessary for testing.
var tbaBaseUrl = "http://www.thebluealliance.com"
var tbaTeamBaseUrl = tbaBaseUrl
var tbaTeamRobotsBaseUrl = tbaBaseUrl
var tbaTeamAwardsBaseUrl = tbaBaseUrl
var tbaEventBaseUrl = tbaBaseUrl
@@ -70,6 +71,10 @@ type TbaTeam struct {
Nickname string `json:"nickname"`
}
type TbaRobot struct {
Name string `json:"name"`
}
type TbaAward struct {
Name string `json:"name"`
EventKey string `json:"event_key"`
@@ -103,6 +108,31 @@ func getTeamFromTba(teamNumber int) (*TbaTeam, error) {
return &teamData, err
}
func getRobotNameFromTba(teamNumber int, year int) (string, error) {
url := fmt.Sprintf("%s/api/v2/team/frc%d/history/robots", tbaTeamRobotsBaseUrl, teamNumber)
resp, err := getTbaRequest(url)
if err != nil {
return "", err
}
// Get the response and handle errors
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var robots map[string]TbaRobot
err = json.Unmarshal(body, &robots)
if err != nil {
return "", err
}
if robotName, ok := robots[strconv.Itoa(year)]; ok {
return robotName.Name, nil
}
return "", nil
}
func getTeamAwardsFromTba(teamNumber int) ([]*TbaAward, error) {
url := fmt.Sprintf("%s/api/v2/team/%s/history/awards", tbaTeamAwardsBaseUrl, getTbaTeam(teamNumber))
resp, err := getTbaRequest(url)