blob: 4837cfe37d6a9f638b7d4a912b3d954136e07ed2 [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"
Milo Lin1d59f0c2022-06-22 20:30:58 -070019 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_shift_schedule_response"
Sabina Leaver9b4eb312023-02-20 19:58:17 -080020 "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_actions_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 RequestAllMatches(server string, requestBytes []byte) (*request_all_matches_response.RequestAllMatchesResponseT, error) {
Philipp Schrader86bf2b92022-03-30 20:57:30 -0700110 return sendMessage[request_all_matches_response.RequestAllMatchesResponseT](
111 server+"/requests/request/all_matches", requestBytes,
112 request_all_matches_response.GetRootAsRequestAllMatchesResponse)
Philipp Schradercbf5c6a2022-02-27 23:25:19 -0800113}
Philipp Schraderd1c4bef2022-02-28 22:51:30 -0800114
Filip Kujawaf882e022022-12-14 13:14:08 -0800115func RequestAllDriverRankings(server string, requestBytes []byte) (*request_all_driver_rankings_response.RequestAllDriverRankingsResponseT, error) {
116 return sendMessage[request_all_driver_rankings_response.RequestAllDriverRankingsResponseT](
117 server+"/requests/request/all_driver_rankings", requestBytes,
118 request_all_driver_rankings_response.GetRootAsRequestAllDriverRankingsResponse)
119}
120
Emily Markova290147d2023-03-03 22:40:06 -0800121func Request2023DataScouting(server string, requestBytes []byte) (*request_2023_data_scouting_response.Request2023DataScoutingResponseT, error) {
122 return sendMessage[request_2023_data_scouting_response.Request2023DataScoutingResponseT](
123 server+"/requests/request/2023_data_scouting", requestBytes,
124 request_2023_data_scouting_response.GetRootAsRequest2023DataScoutingResponse)
125}
126
Alex Perry81f96ba2022-03-13 18:26:19 -0700127func SubmitNotes(server string, requestBytes []byte) (*submit_notes_response.SubmitNotesResponseT, error) {
Philipp Schrader86bf2b92022-03-30 20:57:30 -0700128 return sendMessage[submit_notes_response.SubmitNotesResponseT](
129 server+"/requests/submit/submit_notes", requestBytes,
130 submit_notes_response.GetRootAsSubmitNotesResponse)
Alex Perry81f96ba2022-03-13 18:26:19 -0700131}
132
133func RequestNotes(server string, requestBytes []byte) (*request_notes_for_team_response.RequestNotesForTeamResponseT, error) {
Philipp Schrader86bf2b92022-03-30 20:57:30 -0700134 return sendMessage[request_notes_for_team_response.RequestNotesForTeamResponseT](
135 server+"/requests/request/notes_for_team", requestBytes,
136 request_notes_for_team_response.GetRootAsRequestNotesForTeamResponse)
Alex Perry81f96ba2022-03-13 18:26:19 -0700137}
Milo Lin1d59f0c2022-06-22 20:30:58 -0700138
Filip Kujawaf882e022022-12-14 13:14:08 -0800139func RequestAllNotes(server string, requestBytes []byte) (*request_all_notes_response.RequestAllNotesResponseT, error) {
140 return sendMessage[request_all_notes_response.RequestAllNotesResponseT](
141 server+"/requests/request/all_notes", requestBytes,
142 request_all_notes_response.GetRootAsRequestAllNotesResponse)
143}
144
Milo Lin1d59f0c2022-06-22 20:30:58 -0700145func RequestShiftSchedule(server string, requestBytes []byte) (*request_shift_schedule_response.RequestShiftScheduleResponseT, error) {
146 return sendMessage[request_shift_schedule_response.RequestShiftScheduleResponseT](
147 server+"/requests/request/shift_schedule", requestBytes,
148 request_shift_schedule_response.GetRootAsRequestShiftScheduleResponse)
149}
150
151func SubmitShiftSchedule(server string, requestBytes []byte) (*submit_shift_schedule_response.SubmitShiftScheduleResponseT, error) {
152 return sendMessage[submit_shift_schedule_response.SubmitShiftScheduleResponseT](
153 server+"/requests/submit/shift_schedule", requestBytes,
154 submit_shift_schedule_response.GetRootAsSubmitShiftScheduleResponse)
155}
Filip Kujawa210a03b2022-11-24 14:41:11 -0800156
157func SubmitDriverRanking(server string, requestBytes []byte) (*submit_driver_ranking_response.SubmitDriverRankingResponseT, error) {
158 return sendMessage[submit_driver_ranking_response.SubmitDriverRankingResponseT](
159 server+"/requests/submit/submit_driver_ranking", requestBytes,
160 submit_driver_ranking_response.GetRootAsSubmitDriverRankingResponse)
161}
Sabina Leaver9b4eb312023-02-20 19:58:17 -0800162
163func SubmitActions(server string, requestBytes []byte) (*submit_actions_response.SubmitActionsResponseT, error) {
164 return sendMessage[submit_actions_response.SubmitActionsResponseT](
165 server+"/requests/submit/submit_actions", requestBytes,
166 submit_actions_response.GetRootAsSubmitActionsResponse)
167}
Filip Kujawac1ded372023-05-27 14:33:43 -0700168
169func Delete2023DataScouting(server string, requestBytes []byte) (*delete_2023_data_scouting_response.Delete2023DataScoutingResponseT, error) {
170 return sendMessage[delete_2023_data_scouting_response.Delete2023DataScoutingResponseT](
171 server+"/requests/delete/delete_2023_data_scouting", requestBytes,
172 delete_2023_data_scouting_response.GetRootAsDelete2023DataScoutingResponse)
173}