Files
servicebase/pkg/partner/mq/http/client.go
2025-11-18 17:48:20 +08:00

94 lines
2.8 KiB
Go

package mq_http
import (
"servicebase/pkg/htools"
"servicebase/pkg/partner/mq/message"
"servicebase/pkg/partner/mq/pusher"
"encoding/json"
"fmt"
"github.com/spf13/viper"
)
type Client struct {
dataHost string
}
func init() {
//rlog.SetLogger()
}
func NewHttpClient() pusher.PushClient {
return &Client{
dataHost: viper.GetString("service.host.data"),
}
}
func (client *Client) Push(tag, key string, message []byte) error {
var e error
switch tag {
case "transaction":
_, e = htools.HttpPost(fmt.Sprintf("%s%s", client.dataHost, "/api/subscriber/transaction"), string(message))
case "transaction-update":
_, e = htools.HttpPost(fmt.Sprintf("%s%s", client.dataHost, "/api/subscriber/transaction/update"), string(message))
case "register":
_, e = htools.HttpPost(fmt.Sprintf("%s%s", client.dataHost, "/api/subscriber/register"), string(message))
case "event":
_, e = htools.HttpPost(fmt.Sprintf("%s%s", client.dataHost, "/api/subscriber/event"), string(message))
case "active":
_, e = htools.HttpPost(fmt.Sprintf("%s%s", client.dataHost, "/api/subscriber/active"), string(message))
case "error":
_, e = htools.HttpPost(fmt.Sprintf("%s%s", client.dataHost, "/api/subscriber/error"), string(message))
case "active-new":
default:
// "ignore"
}
return e
}
func (client *Client) PushString(tag, key, message string) error {
return nil
}
// 发布注册消息
func (client *Client) PushRegisterMessage(message message.RegisterMessage) (e error) {
bytes, _ := json.Marshal(message)
return client.Push("register", message.MessageId, bytes)
}
// 发布事件消息
func (client *Client) PushEventTopicMessage(message message.EventMessage) (e error) {
bytes, _ := json.Marshal(message)
return client.Push("event", message.MessageId, bytes)
}
// 发布交易消息
func (client *Client) PushTransactionMessage(message message.TransactionMessage) (e error) {
bytes, _ := json.Marshal(message)
return client.Push("transaction", message.MessageId, bytes)
}
// 发布交易更新消息
func (client *Client) PushTransactionUpdateMessage(message message.TransactionUpdateMessage) (e error) {
bytes, _ := json.Marshal(message)
return client.Push("transaction-update", message.MessageId, bytes)
}
// 发布活跃消息
func (client *Client) PushActiveTopicMessage(message message.ActiveMessage) (e error) {
bytes, _ := json.Marshal(message)
return client.Push("active", message.MessageId, bytes)
}
// 发布错误消息
func (client *Client) PushErrorTopicMessage(message message.ErrorMessage) (e error) {
bytes, _ := json.Marshal(message)
return client.Push("error", message.MessageId, bytes)
}
// 发布激活消息
func (client *Client) PushActiveNewTopicMessage(message message.ActiveNewMessage) (e error) {
bytes, _ := json.Marshal(message)
return client.Push("active-new", message.MessageId, bytes)
}