2022-12-06 12:27:26 +00:00
|
|
|
package beacon_api
|
|
|
|
|
|
|
|
import (
|
2022-12-12 10:39:51 +00:00
|
|
|
"bytes"
|
2023-01-06 03:32:13 +00:00
|
|
|
"context"
|
2022-12-06 12:27:26 +00:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/api/gateway/apimiddleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
type jsonRestHandler interface {
|
2023-01-06 03:32:13 +00:00
|
|
|
GetRestJsonResponse(ctx context.Context, query string, responseJson interface{}) (*apimiddleware.DefaultErrorJson, error)
|
|
|
|
PostRestJson(ctx context.Context, apiEndpoint string, headers map[string]string, data *bytes.Buffer, responseJson interface{}) (*apimiddleware.DefaultErrorJson, error)
|
2022-12-06 12:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type beaconApiJsonRestHandler struct {
|
|
|
|
httpClient http.Client
|
|
|
|
host string
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRestJsonResponse sends a GET requests to apiEndpoint and decodes the response body as a JSON object into responseJson.
|
|
|
|
// If an HTTP error is returned, the body is decoded as a DefaultErrorJson JSON object instead and returned as the first return value.
|
2023-01-06 03:32:13 +00:00
|
|
|
// TODO: GetRestJsonResponse and PostRestJson have converged to the point of being nearly identical, but with some inconsistencies
|
|
|
|
// (like responseJson is being checked for nil one but not the other). We should merge them into a single method
|
|
|
|
// with variadic functional options for headers and data.
|
|
|
|
func (c beaconApiJsonRestHandler) GetRestJsonResponse(ctx context.Context, apiEndpoint string, responseJson interface{}) (*apimiddleware.DefaultErrorJson, error) {
|
2022-12-06 12:27:26 +00:00
|
|
|
if responseJson == nil {
|
|
|
|
return nil, errors.New("responseJson is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
url := c.host + apiEndpoint
|
2023-01-06 03:32:13 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create request with context")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.httpClient.Do(req)
|
2022-12-06 12:27:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to query REST API %s", url)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-12-12 10:39:51 +00:00
|
|
|
return decodeJsonResp(resp, responseJson)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostRestJson sends a POST requests to apiEndpoint and decodes the response body as a JSON object into responseJson. If responseJson
|
|
|
|
// is nil, nothing is decoded. If an HTTP error is returned, the body is decoded as a DefaultErrorJson JSON object instead and returned
|
|
|
|
// as the first return value.
|
2023-01-06 03:32:13 +00:00
|
|
|
func (c beaconApiJsonRestHandler) PostRestJson(ctx context.Context, apiEndpoint string, headers map[string]string, data *bytes.Buffer, responseJson interface{}) (*apimiddleware.DefaultErrorJson, error) {
|
2022-12-12 10:39:51 +00:00
|
|
|
if data == nil {
|
|
|
|
return nil, errors.New("POST data is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
url := c.host + apiEndpoint
|
2023-01-06 03:32:13 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create request with context")
|
|
|
|
}
|
2022-12-12 10:39:51 +00:00
|
|
|
|
|
|
|
for headerKey, headerValue := range headers {
|
|
|
|
req.Header.Set(headerKey, headerValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.httpClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to send POST data to REST endpoint %s", url)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err = resp.Body.Close(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return decodeJsonResp(resp, responseJson)
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeJsonResp(resp *http.Response, responseJson interface{}) (*apimiddleware.DefaultErrorJson, error) {
|
2022-12-15 22:34:05 +00:00
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
|
|
decoder.DisallowUnknownFields()
|
|
|
|
|
2022-12-06 12:27:26 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
errorJson := &apimiddleware.DefaultErrorJson{}
|
2022-12-15 22:34:05 +00:00
|
|
|
if err := decoder.Decode(errorJson); err != nil {
|
2022-12-12 10:39:51 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to decode error json for %s", resp.Request.URL)
|
2022-12-06 12:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return errorJson, errors.Errorf("error %d: %s", errorJson.Code, errorJson.Message)
|
|
|
|
}
|
|
|
|
|
2022-12-12 10:39:51 +00:00
|
|
|
if responseJson != nil {
|
2022-12-15 22:34:05 +00:00
|
|
|
if err := decoder.Decode(responseJson); err != nil {
|
2022-12-12 10:39:51 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to decode response json for %s", resp.Request.URL)
|
|
|
|
}
|
2022-12-06 12:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|