mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
webapi: Export EncodeJSON, DecodeJSON
This commit is contained in:
@@ -79,7 +79,8 @@ func startWebServer(WebListen string, UseSSL bool, CertificateFile, CertificateK
|
||||
}
|
||||
}
|
||||
|
||||
func apiEncodeJSON(w http.ResponseWriter, r *http.Request, data interface{}) (err error) {
|
||||
// EncodeJSON encodes the data as JSON
|
||||
func EncodeJSON(w http.ResponseWriter, r *http.Request, data interface{}) (err error) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
err = json.NewEncoder(w).Encode(data)
|
||||
@@ -90,9 +91,9 @@ func apiEncodeJSON(w http.ResponseWriter, r *http.Request, data interface{}) (er
|
||||
return err
|
||||
}
|
||||
|
||||
// apiDecodeJSON decodes input JSON data server side sent either via GET or POST. It does not limit the maximum amount to read.
|
||||
// DecodeJSON decodes input JSON data server side sent either via GET or POST. It does not limit the maximum amount to read.
|
||||
// In case of error it will automatically send an error to the client.
|
||||
func apiDecodeJSON(w http.ResponseWriter, r *http.Request, data interface{}) (err error) {
|
||||
func DecodeJSON(w http.ResponseWriter, r *http.Request, data interface{}) (err error) {
|
||||
if r.Body == nil {
|
||||
http.Error(w, "", http.StatusBadRequest)
|
||||
return errors.New("no data")
|
||||
|
||||
@@ -33,7 +33,7 @@ Result: 200 with JSON structure apiResponsePeerSelf
|
||||
func apiBlockchainSelfHeader(w http.ResponseWriter, r *http.Request) {
|
||||
publicKey, height, version := core.UserBlockchainHeader()
|
||||
|
||||
apiEncodeJSON(w, r, apiBlockchainHeader{Version: version, Height: height, PeerID: hex.EncodeToString(publicKey.SerializeCompressed())})
|
||||
EncodeJSON(w, r, apiBlockchainHeader{Version: version, Height: height, PeerID: hex.EncodeToString(publicKey.SerializeCompressed())})
|
||||
}
|
||||
|
||||
type apiBlockRecordRaw struct {
|
||||
@@ -60,7 +60,7 @@ Response: 200 with JSON structure apiBlockchainBlockStatus
|
||||
*/
|
||||
func apiBlockchainSelfAppend(w http.ResponseWriter, r *http.Request) {
|
||||
var input apiBlockchainBlockRaw
|
||||
if err := apiDecodeJSON(w, r, &input); err != nil {
|
||||
if err := DecodeJSON(w, r, &input); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ func apiBlockchainSelfAppend(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
newHeight, status := core.UserBlockchainAppend(records)
|
||||
|
||||
apiEncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight})
|
||||
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight})
|
||||
}
|
||||
|
||||
type apiBlockchainBlock struct {
|
||||
@@ -174,7 +174,7 @@ func apiBlockchainSelfRead(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
apiEncodeJSON(w, r, result)
|
||||
EncodeJSON(w, r, result)
|
||||
}
|
||||
|
||||
// apiBlockAddFiles contains the file metadata to add to the blockchain
|
||||
@@ -191,7 +191,7 @@ Response: 200 with JSON structure apiBlockchainBlockStatus
|
||||
*/
|
||||
func apiBlockchainSelfAddFile(w http.ResponseWriter, r *http.Request) {
|
||||
var input apiBlockAddFiles
|
||||
if err := apiDecodeJSON(w, r, &input); err != nil {
|
||||
if err := DecodeJSON(w, r, &input); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -207,7 +207,7 @@ func apiBlockchainSelfAddFile(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
newHeight, status := core.UserBlockchainAddFiles(filesAdd)
|
||||
|
||||
apiEncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight})
|
||||
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight})
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -227,7 +227,7 @@ func apiBlockchainSelfListFile(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
result.Status = status
|
||||
|
||||
apiEncodeJSON(w, r, result)
|
||||
EncodeJSON(w, r, result)
|
||||
}
|
||||
|
||||
// --- conversion from core to API data ---
|
||||
|
||||
@@ -37,7 +37,7 @@ func apiProfileList(w http.ResponseWriter, r *http.Request) {
|
||||
result.Blobs = append(result.Blobs, apiBlockRecordProfileBlob{Type: blobs[n].Type, Data: blobs[n].Data})
|
||||
}
|
||||
|
||||
apiEncodeJSON(w, r, result)
|
||||
EncodeJSON(w, r, result)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -71,7 +71,7 @@ func apiProfileRead(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
apiEncodeJSON(w, r, result)
|
||||
EncodeJSON(w, r, result)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -83,7 +83,7 @@ Response: 200 with JSON structure apiBlockchainBlockStatus
|
||||
*/
|
||||
func apiProfileWrite(w http.ResponseWriter, r *http.Request) {
|
||||
var input apiProfileData
|
||||
if err := apiDecodeJSON(w, r, &input); err != nil {
|
||||
if err := DecodeJSON(w, r, &input); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -98,7 +98,7 @@ func apiProfileWrite(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
newHeight, status := core.UserProfileWrite(profile)
|
||||
|
||||
apiEncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight})
|
||||
EncodeJSON(w, r, apiBlockchainBlockStatus{Status: status, Height: newHeight})
|
||||
}
|
||||
|
||||
// --- conversion from core to API data ---
|
||||
|
||||
@@ -36,7 +36,7 @@ func apiStatus(w http.ResponseWriter, r *http.Request) {
|
||||
// Instead, the core should keep a count of "active peers".
|
||||
status.IsConnected = status.CountPeerList >= 2
|
||||
|
||||
apiEncodeJSON(w, r, status)
|
||||
EncodeJSON(w, r, status)
|
||||
}
|
||||
|
||||
type apiResponsePeerSelf struct {
|
||||
@@ -56,5 +56,5 @@ func apiPeerSelf(w http.ResponseWriter, r *http.Request) {
|
||||
_, publicKey := core.ExportPrivateKey()
|
||||
response.PeerID = hex.EncodeToString(publicKey.SerializeCompressed())
|
||||
|
||||
apiEncodeJSON(w, r, response)
|
||||
EncodeJSON(w, r, response)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user