mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
dbeb3ee886
* WIP * WIP * WIP * WIP * WIP * WIP * WIP * Onboard validator's Beacon REST API usage to e2e tests * Remove unused variables * Remove use_beacon_api tags * Fix DeepSource errors * Revert unneeded changes * Revert evaluator changes * Revert import reordering * Address PR comments * Remove all REST API e2e tests except minimal one * Fix validator pointing to inexisting beacon node port Co-authored-by: Radosław Kapka <rkapka@wp.pl>
53 lines
1.6 KiB
Go
53 lines
1.6 KiB
Go
package beacon_api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/prysmaticlabs/prysm/v3/api/gateway/apimiddleware"
|
|
)
|
|
|
|
type jsonRestHandler interface {
|
|
GetRestJsonResponse(query string, responseJson interface{}) (*apimiddleware.DefaultErrorJson, error)
|
|
}
|
|
|
|
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.
|
|
func (c beaconApiJsonRestHandler) GetRestJsonResponse(apiEndpoint string, responseJson interface{}) (*apimiddleware.DefaultErrorJson, error) {
|
|
if responseJson == nil {
|
|
return nil, errors.New("responseJson is nil")
|
|
}
|
|
|
|
url := c.host + apiEndpoint
|
|
resp, err := c.httpClient.Get(url)
|
|
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
|
|
}
|
|
}()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
errorJson := &apimiddleware.DefaultErrorJson{}
|
|
if err := json.NewDecoder(resp.Body).Decode(errorJson); err != nil {
|
|
return nil, errors.Wrapf(err, "failed to decode error json for %s", url)
|
|
}
|
|
|
|
return errorJson, errors.Errorf("error %d: %s", errorJson.Code, errorJson.Message)
|
|
}
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(responseJson); err != nil {
|
|
return nil, errors.Wrapf(err, "failed to decode response json for %s", url)
|
|
}
|
|
|
|
return nil, nil
|
|
}
|