2026-05-15 19:18:10 +03:00

58 lines
1.4 KiB
Go

package v1
import (
"encoding/json"
"fmt"
"net/http"
"clawchain/cmd/claw-price-feeder/oracle/types"
)
// Response constants
const (
StatusAvailable = "available"
)
type (
// HealthZResponse defines the response type for the healthy API handler.
HealthZResponse struct {
Status string `json:"status" yaml:"status"`
Oracle struct {
LastSync string `json:"last_sync"`
} `json:"oracle"`
}
// PricesResponse defines the response type for getting the latest exchange
// rates from the oracle.
PricesResponse struct {
Prices types.CurrencyPairDec `json:"prices"`
}
PricesPerProviderResponse struct {
Prices types.CurrencyPairDecByProvider `json:"providers"`
}
)
// errorResponse defines the attributes of a JSON error response.
type errorResponse struct {
Code int `json:"code,omitempty"`
Error string `json:"error"`
}
// newErrorResponse creates a new errorResponse instance.
func newErrorResponse(code int, err string) errorResponse {
return errorResponse{Code: code, Error: err}
}
// writeErrorResponse prepares and writes a HTTP error
// given a status code and an error message.
func writeErrorResponse(w http.ResponseWriter, status int, err string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if bz, err := json.Marshal(newErrorResponse(0, err)); err == nil {
_, _ = w.Write(bz)
} else {
_, _ = w.Write([]byte(fmt.Sprintf("failed to marshal error response: %s", err)))
}
}