Block rewards API endpoint (#12020)

Co-authored-by: terencechain <terence@prysmaticlabs.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
Radosław Kapka
2023-03-28 18:44:41 +02:00
committed by GitHub
parent 5cbbd26df4
commit 98949d8075
50 changed files with 1772 additions and 2569 deletions

View File

@@ -6,6 +6,7 @@ go_library(
"auth.go",
"endpoint.go",
"external_ip.go",
"writer.go",
],
importpath = "github.com/prysmaticlabs/prysm/v4/network",
visibility = ["//visibility:public"],
@@ -13,6 +14,7 @@ go_library(
"//network/authorization:go_default_library",
"@com_github_golang_jwt_jwt_v4//:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)

41
network/writer.go Normal file
View File

@@ -0,0 +1,41 @@
package network
import (
"bytes"
"encoding/json"
"io"
"net/http"
"strconv"
log "github.com/sirupsen/logrus"
)
// DefaultErrorJson is a JSON representation of a simple error value, containing only a message and an error code.
type DefaultErrorJson struct {
Message string `json:"message"`
Code int `json:"code"`
}
// WriteJson writes the response message in JSON format.
func WriteJson(w http.ResponseWriter, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(v); err != nil {
log.WithError(err).Error("Could not write response message")
}
}
// WriteError writes the error by manipulating headers and the body of the final response.
func WriteError(w http.ResponseWriter, errJson *DefaultErrorJson) {
j, err := json.Marshal(errJson)
if err != nil {
log.WithError(err).Error("Could not marshal error message")
return
}
w.Header().Set("Content-Length", strconv.Itoa(len(j)))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(errJson.Code)
if _, err := io.Copy(w, io.NopCloser(bytes.NewReader(j))); err != nil {
log.WithError(err).Error("Could not write error message")
}
}