blob: eb3a1ca9633f41f297c34533925bd22ec4553427 [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"
Alex Perry81f96ba2022-03-13 18:26:19 -070017 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_notes_for_team_response"
Milo Lin1d59f0c2022-06-22 20:30:58 -070018 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_shift_schedule_response"
Filip Kujawa210a03b2022-11-24 14:41:11 -080019 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_driver_ranking_response"
Alex Perry81f96ba2022-03-13 18:26:19 -070020 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_notes_response"
Milo Lin1d59f0c2022-06-22 20:30:58 -070021 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_shift_schedule_response"
Philipp Schrader86bf2b92022-03-30 20:57:30 -070022 flatbuffers "github.com/google/flatbuffers/go"
Philipp Schraderd9096a32022-02-24 17:53:09 -080023)
24
Philipp Schraderfae8a7e2022-03-13 22:51:54 -070025// The username to submit the various requests as.
26const DefaultUsername = "debug_cli"
27
Philipp Schraderd9096a32022-02-24 17:53:09 -080028// A struct that can be used as an `error`. It contains information about the
29// why the server was unhappy and what the corresponding request was.
30type ResponseError struct {
31 Url string
32 StatusCode int
33 ErrorResponse *error_response.ErrorResponse
34}
35
36// Required to implement the `error` interface.
37func (err *ResponseError) Error() string {
38 return fmt.Sprintf(
39 "%s returned %d %s: %s", err.Url, err.StatusCode,
40 http.StatusText(err.StatusCode), err.ErrorResponse.ErrorMessage())
41}
42
43// Parse an `ErrorResponse` message that the server sent back. This happens
44// whenever the status code is something other than 200. If the message is
45// successfully parsed, it's turned into a `ResponseError` which implements the
46// `error` interface.
47func parseErrorResponse(url string, statusCode int, responseBytes []byte) error {
48 getRootErrMessage := ""
49 defer func() {
50 if r := recover(); r != nil {
51 getRootErrMessage = fmt.Sprintf("%v", r)
52 }
53 }()
54 errorMessage := error_response.GetRootAsErrorResponse(responseBytes, 0)
55 if getRootErrMessage != "" {
56 return errors.New(fmt.Sprintf(
57 "Failed to parse response from %s with status %d %s (bytes %v) as ErrorResponse: %s",
58 url, statusCode, http.StatusText(statusCode), responseBytes, getRootErrMessage))
59 }
60
61 return &ResponseError{
62 Url: url,
63 StatusCode: statusCode,
64 ErrorResponse: errorMessage,
65 }
66}
67
68// Performs a POST request with the specified payload. The bytes that the
69// server responds with are returned.
70func performPost(url string, requestBytes []byte) ([]byte, error) {
Philipp Schraderfae8a7e2022-03-13 22:51:54 -070071 req, err := http.NewRequest("POST", url, bytes.NewReader(requestBytes))
72 if err != nil {
73 log.Printf("Failed to create a new POST request to %s: %v", url, err)
74 return nil, err
75 }
76 req.Header.Add("Authorization", "Basic "+
77 base64.StdEncoding.EncodeToString([]byte(DefaultUsername+":")))
78
79 client := &http.Client{}
80 resp, err := client.Do(req)
Philipp Schraderd9096a32022-02-24 17:53:09 -080081 if err != nil {
82 log.Printf("Failed to send POST request to %s: %v", url, err)
83 return nil, err
84 }
85 responseBytes, err := io.ReadAll(resp.Body)
86 if err != nil {
87 log.Printf("Failed to parse response bytes from POST to %s: %v", url, err)
88 return nil, err
89 }
90 if resp.StatusCode != http.StatusOK {
91 return nil, parseErrorResponse(url, resp.StatusCode, responseBytes)
92 }
93 return responseBytes, nil
94}
95
Philipp Schrader86bf2b92022-03-30 20:57:30 -070096// Sends a message to the server and returns the deserialized response.
97// The first generic argument must be specified.
98func sendMessage[FbT interface{}, Fb interface{ UnPack() *FbT }](url string, requestBytes []byte, parser func([]byte, flatbuffers.UOffsetT) Fb) (*FbT, error) {
99 responseBytes, err := performPost(url, requestBytes)
100 if err != nil {
101 return nil, err
102 }
103 response := parser(responseBytes, 0)
104 return response.UnPack(), nil
105}
106
Philipp Schrader20440f82022-03-16 20:07:09 -0700107func RequestAllMatches(server string, requestBytes []byte) (*request_all_matches_response.RequestAllMatchesResponseT, error) {
Philipp Schrader86bf2b92022-03-30 20:57:30 -0700108 return sendMessage[request_all_matches_response.RequestAllMatchesResponseT](
109 server+"/requests/request/all_matches", requestBytes,
110 request_all_matches_response.GetRootAsRequestAllMatchesResponse)
Philipp Schradercbf5c6a2022-02-27 23:25:19 -0800111}
Philipp Schraderd1c4bef2022-02-28 22:51:30 -0800112
Filip Kujawaf882e022022-12-14 13:14:08 -0800113func RequestAllDriverRankings(server string, requestBytes []byte) (*request_all_driver_rankings_response.RequestAllDriverRankingsResponseT, error) {
114 return sendMessage[request_all_driver_rankings_response.RequestAllDriverRankingsResponseT](
115 server+"/requests/request/all_driver_rankings", requestBytes,
116 request_all_driver_rankings_response.GetRootAsRequestAllDriverRankingsResponse)
117}
118
Emily Markova290147d2023-03-03 22:40:06 -0800119func Request2023DataScouting(server string, requestBytes []byte) (*request_2023_data_scouting_response.Request2023DataScoutingResponseT, error) {
120 return sendMessage[request_2023_data_scouting_response.Request2023DataScoutingResponseT](
121 server+"/requests/request/2023_data_scouting", requestBytes,
122 request_2023_data_scouting_response.GetRootAsRequest2023DataScoutingResponse)
123}
124
Alex Perry81f96ba2022-03-13 18:26:19 -0700125func SubmitNotes(server string, requestBytes []byte) (*submit_notes_response.SubmitNotesResponseT, error) {
Philipp Schrader86bf2b92022-03-30 20:57:30 -0700126 return sendMessage[submit_notes_response.SubmitNotesResponseT](
127 server+"/requests/submit/submit_notes", requestBytes,
128 submit_notes_response.GetRootAsSubmitNotesResponse)
Alex Perry81f96ba2022-03-13 18:26:19 -0700129}
130
131func RequestNotes(server string, requestBytes []byte) (*request_notes_for_team_response.RequestNotesForTeamResponseT, error) {
Philipp Schrader86bf2b92022-03-30 20:57:30 -0700132 return sendMessage[request_notes_for_team_response.RequestNotesForTeamResponseT](
133 server+"/requests/request/notes_for_team", requestBytes,
134 request_notes_for_team_response.GetRootAsRequestNotesForTeamResponse)
Alex Perry81f96ba2022-03-13 18:26:19 -0700135}
Milo Lin1d59f0c2022-06-22 20:30:58 -0700136
Filip Kujawaf882e022022-12-14 13:14:08 -0800137func RequestAllNotes(server string, requestBytes []byte) (*request_all_notes_response.RequestAllNotesResponseT, error) {
138 return sendMessage[request_all_notes_response.RequestAllNotesResponseT](
139 server+"/requests/request/all_notes", requestBytes,
140 request_all_notes_response.GetRootAsRequestAllNotesResponse)
141}
142
Milo Lin1d59f0c2022-06-22 20:30:58 -0700143func RequestShiftSchedule(server string, requestBytes []byte) (*request_shift_schedule_response.RequestShiftScheduleResponseT, error) {
144 return sendMessage[request_shift_schedule_response.RequestShiftScheduleResponseT](
145 server+"/requests/request/shift_schedule", requestBytes,
146 request_shift_schedule_response.GetRootAsRequestShiftScheduleResponse)
147}
148
149func SubmitShiftSchedule(server string, requestBytes []byte) (*submit_shift_schedule_response.SubmitShiftScheduleResponseT, error) {
150 return sendMessage[submit_shift_schedule_response.SubmitShiftScheduleResponseT](
151 server+"/requests/submit/shift_schedule", requestBytes,
152 submit_shift_schedule_response.GetRootAsSubmitShiftScheduleResponse)
153}
Filip Kujawa210a03b2022-11-24 14:41:11 -0800154
155func SubmitDriverRanking(server string, requestBytes []byte) (*submit_driver_ranking_response.SubmitDriverRankingResponseT, error) {
156 return sendMessage[submit_driver_ranking_response.SubmitDriverRankingResponseT](
157 server+"/requests/submit/submit_driver_ranking", requestBytes,
158 submit_driver_ranking_response.GetRootAsSubmitDriverRankingResponse)
159}