blob: 3e5b4f9a50c5793a9ad9c4aa6d1db7283c3d02de [file] [log] [blame]
Philipp Schraderd9096a32022-02-24 17:53:09 -08001package debug
2
3import (
4 "bytes"
Philipp Schraderfae8a7e2022-03-13 22:51:54 -07005 "encoding/base64"
Philipp Schraderd9096a32022-02-24 17:53:09 -08006 "errors"
7 "fmt"
8 "io"
9 "log"
10 "net/http"
11
Filip Kujawac1ded372023-05-27 14:33:43 -070012 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/delete_2023_data_scouting_response"
Philipp Schraderd9096a32022-02-24 17:53:09 -080013 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/error_response"
Emily Markova290147d2023-03-03 22:40:06 -080014 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_2023_data_scouting_response"
Filip Kujawaf882e022022-12-14 13:14:08 -080015 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_driver_rankings_response"
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080016 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_matches_response"
Filip Kujawaf882e022022-12-14 13:14:08 -080017 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_notes_response"
Alex Perry81f96ba2022-03-13 18:26:19 -070018 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_notes_for_team_response"
Emily Markovafaecfe12023-07-01 12:40:03 -070019 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_pit_images_response"
Milo Lin1d59f0c2022-06-22 20:30:58 -070020 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_shift_schedule_response"
Sabina Leaver9b4eb312023-02-20 19:58:17 -080021 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_actions_response"
Filip Kujawa210a03b2022-11-24 14:41:11 -080022 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_driver_ranking_response"
Alex Perry81f96ba2022-03-13 18:26:19 -070023 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_notes_response"
Emily Markovafaecfe12023-07-01 12:40:03 -070024 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_pit_image_response"
Milo Lin1d59f0c2022-06-22 20:30:58 -070025 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_shift_schedule_response"
Philipp Schrader86bf2b92022-03-30 20:57:30 -070026 flatbuffers "github.com/google/flatbuffers/go"
Philipp Schraderd9096a32022-02-24 17:53:09 -080027)
28
Philipp Schraderfae8a7e2022-03-13 22:51:54 -070029// The username to submit the various requests as.
30const DefaultUsername = "debug_cli"
31
Philipp Schraderd9096a32022-02-24 17:53:09 -080032// A struct that can be used as an `error`. It contains information about the
33// why the server was unhappy and what the corresponding request was.
34type ResponseError struct {
35 Url string
36 StatusCode int
37 ErrorResponse *error_response.ErrorResponse
38}
39
40// Required to implement the `error` interface.
41func (err *ResponseError) Error() string {
42 return fmt.Sprintf(
43 "%s returned %d %s: %s", err.Url, err.StatusCode,
44 http.StatusText(err.StatusCode), err.ErrorResponse.ErrorMessage())
45}
46
47// Parse an `ErrorResponse` message that the server sent back. This happens
48// whenever the status code is something other than 200. If the message is
49// successfully parsed, it's turned into a `ResponseError` which implements the
50// `error` interface.
51func parseErrorResponse(url string, statusCode int, responseBytes []byte) error {
52 getRootErrMessage := ""
53 defer func() {
54 if r := recover(); r != nil {
55 getRootErrMessage = fmt.Sprintf("%v", r)
56 }
57 }()
58 errorMessage := error_response.GetRootAsErrorResponse(responseBytes, 0)
59 if getRootErrMessage != "" {
60 return errors.New(fmt.Sprintf(
61 "Failed to parse response from %s with status %d %s (bytes %v) as ErrorResponse: %s",
62 url, statusCode, http.StatusText(statusCode), responseBytes, getRootErrMessage))
63 }
64
65 return &ResponseError{
66 Url: url,
67 StatusCode: statusCode,
68 ErrorResponse: errorMessage,
69 }
70}
71
72// Performs a POST request with the specified payload. The bytes that the
73// server responds with are returned.
74func performPost(url string, requestBytes []byte) ([]byte, error) {
Philipp Schraderfae8a7e2022-03-13 22:51:54 -070075 req, err := http.NewRequest("POST", url, bytes.NewReader(requestBytes))
76 if err != nil {
77 log.Printf("Failed to create a new POST request to %s: %v", url, err)
78 return nil, err
79 }
80 req.Header.Add("Authorization", "Basic "+
81 base64.StdEncoding.EncodeToString([]byte(DefaultUsername+":")))
82
83 client := &http.Client{}
84 resp, err := client.Do(req)
Philipp Schraderd9096a32022-02-24 17:53:09 -080085 if err != nil {
86 log.Printf("Failed to send POST request to %s: %v", url, err)
87 return nil, err
88 }
89 responseBytes, err := io.ReadAll(resp.Body)
90 if err != nil {
91 log.Printf("Failed to parse response bytes from POST to %s: %v", url, err)
92 return nil, err
93 }
94 if resp.StatusCode != http.StatusOK {
95 return nil, parseErrorResponse(url, resp.StatusCode, responseBytes)
96 }
97 return responseBytes, nil
98}
99
Philipp Schrader86bf2b92022-03-30 20:57:30 -0700100// Sends a message to the server and returns the deserialized response.
101// The first generic argument must be specified.
102func sendMessage[FbT interface{}, Fb interface{ UnPack() *FbT }](url string, requestBytes []byte, parser func([]byte, flatbuffers.UOffsetT) Fb) (*FbT, error) {
103 responseBytes, err := performPost(url, requestBytes)
104 if err != nil {
105 return nil, err
106 }
107 response := parser(responseBytes, 0)
108 return response.UnPack(), nil
109}
110
Philipp Schrader20440f82022-03-16 20:07:09 -0700111func RequestAllMatches(server string, requestBytes []byte) (*request_all_matches_response.RequestAllMatchesResponseT, error) {
Philipp Schrader86bf2b92022-03-30 20:57:30 -0700112 return sendMessage[request_all_matches_response.RequestAllMatchesResponseT](
113 server+"/requests/request/all_matches", requestBytes,
114 request_all_matches_response.GetRootAsRequestAllMatchesResponse)
Philipp Schradercbf5c6a2022-02-27 23:25:19 -0800115}
Philipp Schraderd1c4bef2022-02-28 22:51:30 -0800116
Filip Kujawaf882e022022-12-14 13:14:08 -0800117func RequestAllDriverRankings(server string, requestBytes []byte) (*request_all_driver_rankings_response.RequestAllDriverRankingsResponseT, error) {
118 return sendMessage[request_all_driver_rankings_response.RequestAllDriverRankingsResponseT](
119 server+"/requests/request/all_driver_rankings", requestBytes,
120 request_all_driver_rankings_response.GetRootAsRequestAllDriverRankingsResponse)
121}
122
Emily Markova290147d2023-03-03 22:40:06 -0800123func Request2023DataScouting(server string, requestBytes []byte) (*request_2023_data_scouting_response.Request2023DataScoutingResponseT, error) {
124 return sendMessage[request_2023_data_scouting_response.Request2023DataScoutingResponseT](
125 server+"/requests/request/2023_data_scouting", requestBytes,
126 request_2023_data_scouting_response.GetRootAsRequest2023DataScoutingResponse)
127}
128
Alex Perry81f96ba2022-03-13 18:26:19 -0700129func SubmitNotes(server string, requestBytes []byte) (*submit_notes_response.SubmitNotesResponseT, error) {
Philipp Schrader86bf2b92022-03-30 20:57:30 -0700130 return sendMessage[submit_notes_response.SubmitNotesResponseT](
131 server+"/requests/submit/submit_notes", requestBytes,
132 submit_notes_response.GetRootAsSubmitNotesResponse)
Alex Perry81f96ba2022-03-13 18:26:19 -0700133}
134
135func RequestNotes(server string, requestBytes []byte) (*request_notes_for_team_response.RequestNotesForTeamResponseT, error) {
Philipp Schrader86bf2b92022-03-30 20:57:30 -0700136 return sendMessage[request_notes_for_team_response.RequestNotesForTeamResponseT](
137 server+"/requests/request/notes_for_team", requestBytes,
138 request_notes_for_team_response.GetRootAsRequestNotesForTeamResponse)
Alex Perry81f96ba2022-03-13 18:26:19 -0700139}
Milo Lin1d59f0c2022-06-22 20:30:58 -0700140
Emily Markovafaecfe12023-07-01 12:40:03 -0700141func RequestPitImages(server string, requestBytes []byte) (*request_pit_images_response.RequestPitImagesResponseT, error) {
142 return sendMessage[request_pit_images_response.RequestPitImagesResponseT](
143 server+"/requests/request/pit_images", requestBytes,
144 request_pit_images_response.GetRootAsRequestPitImagesResponse)
145}
146
Filip Kujawaf882e022022-12-14 13:14:08 -0800147func RequestAllNotes(server string, requestBytes []byte) (*request_all_notes_response.RequestAllNotesResponseT, error) {
148 return sendMessage[request_all_notes_response.RequestAllNotesResponseT](
149 server+"/requests/request/all_notes", requestBytes,
150 request_all_notes_response.GetRootAsRequestAllNotesResponse)
151}
152
Milo Lin1d59f0c2022-06-22 20:30:58 -0700153func RequestShiftSchedule(server string, requestBytes []byte) (*request_shift_schedule_response.RequestShiftScheduleResponseT, error) {
154 return sendMessage[request_shift_schedule_response.RequestShiftScheduleResponseT](
155 server+"/requests/request/shift_schedule", requestBytes,
156 request_shift_schedule_response.GetRootAsRequestShiftScheduleResponse)
157}
158
159func SubmitShiftSchedule(server string, requestBytes []byte) (*submit_shift_schedule_response.SubmitShiftScheduleResponseT, error) {
160 return sendMessage[submit_shift_schedule_response.SubmitShiftScheduleResponseT](
161 server+"/requests/submit/shift_schedule", requestBytes,
162 submit_shift_schedule_response.GetRootAsSubmitShiftScheduleResponse)
163}
Filip Kujawa210a03b2022-11-24 14:41:11 -0800164
165func SubmitDriverRanking(server string, requestBytes []byte) (*submit_driver_ranking_response.SubmitDriverRankingResponseT, error) {
166 return sendMessage[submit_driver_ranking_response.SubmitDriverRankingResponseT](
167 server+"/requests/submit/submit_driver_ranking", requestBytes,
168 submit_driver_ranking_response.GetRootAsSubmitDriverRankingResponse)
169}
Sabina Leaver9b4eb312023-02-20 19:58:17 -0800170
171func SubmitActions(server string, requestBytes []byte) (*submit_actions_response.SubmitActionsResponseT, error) {
172 return sendMessage[submit_actions_response.SubmitActionsResponseT](
173 server+"/requests/submit/submit_actions", requestBytes,
174 submit_actions_response.GetRootAsSubmitActionsResponse)
175}
Filip Kujawac1ded372023-05-27 14:33:43 -0700176
Emily Markovafaecfe12023-07-01 12:40:03 -0700177func SubmitPitImage(server string, requestBytes []byte) (*submit_pit_image_response.SubmitPitImageResponseT, error) {
178 return sendMessage[submit_pit_image_response.SubmitPitImageResponseT](
179 server+"/requests/submit/submit_pit_image", requestBytes,
180 submit_pit_image_response.GetRootAsSubmitPitImageResponse)
181}
182
Filip Kujawac1ded372023-05-27 14:33:43 -0700183func Delete2023DataScouting(server string, requestBytes []byte) (*delete_2023_data_scouting_response.Delete2023DataScoutingResponseT, error) {
184 return sendMessage[delete_2023_data_scouting_response.Delete2023DataScoutingResponseT](
185 server+"/requests/delete/delete_2023_data_scouting", requestBytes,
186 delete_2023_data_scouting_response.GetRootAsDelete2023DataScoutingResponse)
187}