2020-05-01 05:47:46 +00:00
|
|
|
package admin
|
2017-03-13 21:46:11 +00:00
|
|
|
|
2017-03-22 21:43:36 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
)
|
2017-03-13 21:46:11 +00:00
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
//Response -- Standard response structure
|
|
|
|
type Response struct {
|
2017-03-13 21:46:11 +00:00
|
|
|
TransactionType string `json:"type"`
|
|
|
|
Schema string `json:"schema"`
|
|
|
|
TransactionTimeStamp int64 `json:"txts"`
|
|
|
|
TransactionID int64 `json:"txid"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
ErrorDescription string `json:"error_description"`
|
|
|
|
ErrorURI string `json:"error_uri"`
|
|
|
|
Result interface{} `json:"result"`
|
|
|
|
}
|
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
//NewResponse -- Constructor
|
|
|
|
func NewResponse(transactionType string) (p *Response) {
|
2020-04-30 05:49:09 +00:00
|
|
|
// TODO BUG use atomic
|
2017-03-13 21:46:11 +00:00
|
|
|
transactionID++
|
|
|
|
|
2020-05-01 05:47:46 +00:00
|
|
|
p = &Response{}
|
2017-03-13 21:46:11 +00:00
|
|
|
p.TransactionType = transactionType
|
|
|
|
p.TransactionID = transactionID
|
|
|
|
p.TransactionTimeStamp = time.Now().Unix()
|
|
|
|
p.Error = "ok"
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//Generate -- encode into JSON and return string
|
2020-05-01 05:47:46 +00:00
|
|
|
func (e *Response) Generate() string {
|
2017-03-13 21:46:11 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
json.NewEncoder(buf).Encode(e)
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
//GenerateWriter --
|
2020-05-01 05:47:46 +00:00
|
|
|
func (e *Response) GenerateWriter(w io.Writer) {
|
2017-03-13 21:46:11 +00:00
|
|
|
json.NewEncoder(w).Encode(e)
|
|
|
|
}
|