blob: 60d14be9acdfeeb35caaedcaf6adad6b1b223a45 [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
12 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/error_response"
Emily Markova290147d2023-03-03 22:40:06 -080013 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_2023_data_scouting_response"
Filip Kujawaf882e022022-12-14 13:14:08 -080014 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_driver_rankings_response"
Philipp Schradercbf5c6a2022-02-27 23:25:19 -080015 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_matches_response"
Filip Kujawaf882e022022-12-14 13:14:08 -080016 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_notes_response"
Philipp Schraderacf96232022-03-01 22:03:30 -080017 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_data_scouting_response"
Alex Perry81f96ba2022-03-13 18:26:19 -070018 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_notes_for_team_response"
Milo Lin1d59f0c2022-06-22 20:30:58 -070019 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_shift_schedule_response"
Philipp Schraderd9096a32022-02-24 17:53:09 -080020 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_data_scouting_response"
Filip Kujawa210a03b2022-11-24 14:41:11 -080021 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_driver_ranking_response"
Alex Perry81f96ba2022-03-13 18:26:19 -070022 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_notes_response"
Milo Lin1d59f0c2022-06-22 20:30:58 -070023 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_shift_schedule_response"
Philipp Schrader86bf2b92022-03-30 20:57:30 -070024 flatbuffers "github.com/google/flatbuffers/go"
Philipp Schraderd9096a32022-02-24 17:53:09 -080025)
26
Philipp Schraderfae8a7e2022-03-13 22:51:54 -070027// The username to submit the various requests as.
28const DefaultUsername = "debug_cli"
29
Philipp Schraderd9096a32022-02-24 17:53:09 -080030// A struct that can be used as an `error`. It contains information about the
31// why the server was unhappy and what the corresponding request was.
32type ResponseError struct {
33 Url string
34 StatusCode int
35 ErrorResponse *error_response.ErrorResponse
36}
37
38// Required to implement the `error` interface.
39func (err *ResponseError) Error() string {
40 return fmt.Sprintf(
41 "%s returned %d %s: %s", err.Url, err.StatusCode,
42 http.StatusText(err.StatusCode), err.ErrorResponse.ErrorMessage())
43}
44
45// Parse an `ErrorResponse` message that the server sent back. This happens
46// whenever the status code is something other than 200. If the message is
47// successfully parsed, it's turned into a `ResponseError` which implements the
48// `error` interface.
49func parseErrorResponse(url string, statusCode int, responseBytes []byte) error {
50 getRootErrMessage := ""
51 defer func() {
52 if r := recover(); r != nil {
53 getRootErrMessage = fmt.Sprintf("%v", r)
54 }
55 }()
56 errorMessage := error_response.GetRootAsErrorResponse(responseBytes, 0)
57 if getRootErrMessage != "" {
58 return errors.New(fmt.Sprintf(
59 "Failed to parse response from %s with status %d %s (bytes %v) as ErrorResponse: %s",
60 url, statusCode, http.StatusText(statusCode), responseBytes, getRootErrMessage))
61 }
62
63 return &ResponseError{
64 Url: url,
65 StatusCode: statusCode,
66 ErrorResponse: errorMessage,
67 }
68}
69
70// Performs a POST request with the specified payload. The bytes that the
71// server responds with are returned.
72func performPost(url string, requestBytes []byte) ([]byte, error) {
Philipp Schraderfae8a7e2022-03-13 22:51:54 -070073 req, err := http.NewRequest("POST", url, bytes.NewReader(requestBytes))
74 if err != nil {
75 log.Printf("Failed to create a new POST request to %s: %v", url, err)
76 return nil, err
77 }
78 req.Header.Add("Authorization", "Basic "+
79 base64.StdEncoding.EncodeToString([]byte(DefaultUsername+":")))
80
81 client := &http.Client{}
82 resp, err := client.Do(req)
Philipp Schraderd9096a32022-02-24 17:53:09 -080083 if err != nil {
84 log.Printf("Failed to send POST request to %s: %v", url, err)
85 return nil, err
86 }
87 responseBytes, err := io.ReadAll(resp.Body)
88 if err != nil {
89 log.Printf("Failed to parse response bytes from POST to %s: %v", url, err)
90 return nil, err
91 }
92 if resp.StatusCode != http.StatusOK {
93 return nil, parseErrorResponse(url, resp.StatusCode, responseBytes)
94 }
95 return responseBytes, nil
96}
97
Philipp Schrader86bf2b92022-03-30 20:57:30 -070098// Sends a message to the server and returns the deserialized response.
99// The first generic argument must be specified.
100func sendMessage[FbT interface{}, Fb interface{ UnPack() *FbT }](url string, requestBytes []byte, parser func([]byte, flatbuffers.UOffsetT) Fb) (*FbT, error) {
101 responseBytes, err := performPost(url, requestBytes)
102 if err != nil {
103 return nil, err
104 }
105 response := parser(responseBytes, 0)
106 return response.UnPack(), nil
107}
108
Philipp Schrader20440f82022-03-16 20:07:09 -0700109func SubmitDataScouting(server string, requestBytes []byte) (*submit_data_scouting_response.SubmitDataScoutingResponseT, error) {
Philipp Schrader86bf2b92022-03-30 20:57:30 -0700110 return sendMessage[submit_data_scouting_response.SubmitDataScoutingResponseT](
111 server+"/requests/submit/data_scouting", requestBytes,
112 submit_data_scouting_response.GetRootAsSubmitDataScoutingResponse)
Philipp Schraderd9096a32022-02-24 17:53:09 -0800113}
Philipp Schradercbf5c6a2022-02-27 23:25:19 -0800114
Philipp Schrader20440f82022-03-16 20:07:09 -0700115func RequestAllMatches(server string, requestBytes []byte) (*request_all_matches_response.RequestAllMatchesResponseT, error) {
Philipp Schrader86bf2b92022-03-30 20:57:30 -0700116 return sendMessage[request_all_matches_response.RequestAllMatchesResponseT](
117 server+"/requests/request/all_matches", requestBytes,
118 request_all_matches_response.GetRootAsRequestAllMatchesResponse)
Philipp Schradercbf5c6a2022-02-27 23:25:19 -0800119}
Philipp Schraderd1c4bef2022-02-28 22:51:30 -0800120
Filip Kujawaf882e022022-12-14 13:14:08 -0800121func RequestAllDriverRankings(server string, requestBytes []byte) (*request_all_driver_rankings_response.RequestAllDriverRankingsResponseT, error) {
122 return sendMessage[request_all_driver_rankings_response.RequestAllDriverRankingsResponseT](
123 server+"/requests/request/all_driver_rankings", requestBytes,
124 request_all_driver_rankings_response.GetRootAsRequestAllDriverRankingsResponse)
125}
126
Philipp Schrader20440f82022-03-16 20:07:09 -0700127func RequestDataScouting(server string, requestBytes []byte) (*request_data_scouting_response.RequestDataScoutingResponseT, error) {
Philipp Schrader86bf2b92022-03-30 20:57:30 -0700128 return sendMessage[request_data_scouting_response.RequestDataScoutingResponseT](
129 server+"/requests/request/data_scouting", requestBytes,
130 request_data_scouting_response.GetRootAsRequestDataScoutingResponse)
Philipp Schraderacf96232022-03-01 22:03:30 -0800131}
Philipp Schraderd3fac192022-03-02 20:35:46 -0800132
Emily Markova290147d2023-03-03 22:40:06 -0800133func Request2023DataScouting(server string, requestBytes []byte) (*request_2023_data_scouting_response.Request2023DataScoutingResponseT, error) {
134 return sendMessage[request_2023_data_scouting_response.Request2023DataScoutingResponseT](
135 server+"/requests/request/2023_data_scouting", requestBytes,
136 request_2023_data_scouting_response.GetRootAsRequest2023DataScoutingResponse)
137}
138
Alex Perry81f96ba2022-03-13 18:26:19 -0700139func SubmitNotes(server string, requestBytes []byte) (*submit_notes_response.SubmitNotesResponseT, error) {
Philipp Schrader86bf2b92022-03-30 20:57:30 -0700140 return sendMessage[submit_notes_response.SubmitNotesResponseT](
141 server+"/requests/submit/submit_notes", requestBytes,
142 submit_notes_response.GetRootAsSubmitNotesResponse)
Alex Perry81f96ba2022-03-13 18:26:19 -0700143}
144
145func RequestNotes(server string, requestBytes []byte) (*request_notes_for_team_response.RequestNotesForTeamResponseT, error) {
Philipp Schrader86bf2b92022-03-30 20:57:30 -0700146 return sendMessage[request_notes_for_team_response.RequestNotesForTeamResponseT](
147 server+"/requests/request/notes_for_team", requestBytes,
148 request_notes_for_team_response.GetRootAsRequestNotesForTeamResponse)
Alex Perry81f96ba2022-03-13 18:26:19 -0700149}
Milo Lin1d59f0c2022-06-22 20:30:58 -0700150
Filip Kujawaf882e022022-12-14 13:14:08 -0800151func RequestAllNotes(server string, requestBytes []byte) (*request_all_notes_response.RequestAllNotesResponseT, error) {
152 return sendMessage[request_all_notes_response.RequestAllNotesResponseT](
153 server+"/requests/request/all_notes", requestBytes,
154 request_all_notes_response.GetRootAsRequestAllNotesResponse)
155}
156
Milo Lin1d59f0c2022-06-22 20:30:58 -0700157func RequestShiftSchedule(server string, requestBytes []byte) (*request_shift_schedule_response.RequestShiftScheduleResponseT, error) {
158 return sendMessage[request_shift_schedule_response.RequestShiftScheduleResponseT](
159 server+"/requests/request/shift_schedule", requestBytes,
160 request_shift_schedule_response.GetRootAsRequestShiftScheduleResponse)
161}
162
163func SubmitShiftSchedule(server string, requestBytes []byte) (*submit_shift_schedule_response.SubmitShiftScheduleResponseT, error) {
164 return sendMessage[submit_shift_schedule_response.SubmitShiftScheduleResponseT](
165 server+"/requests/submit/shift_schedule", requestBytes,
166 submit_shift_schedule_response.GetRootAsSubmitShiftScheduleResponse)
167}
Filip Kujawa210a03b2022-11-24 14:41:11 -0800168
169func SubmitDriverRanking(server string, requestBytes []byte) (*submit_driver_ranking_response.SubmitDriverRankingResponseT, error) {
170 return sendMessage[submit_driver_ranking_response.SubmitDriverRankingResponseT](
171 server+"/requests/submit/submit_driver_ranking", requestBytes,
172 submit_driver_ranking_response.GetRootAsSubmitDriverRankingResponse)
173}