[Update] Define method #5

Merged
trunglt251292 merged 10 commits from v1 into master 2022-03-24 09:49:54 +00:00
3 changed files with 17 additions and 17 deletions
Showing only changes of commit 37c217be50 - Show all commits

View File

@ -53,17 +53,17 @@ func GetClient() *Client {
return client return client
} }
// RequestNats // requestNats
// publish message to nats and waiting response // publish message to nats and waiting response
func (c *Client) RequestNats(subject string, data []byte) (*Response, error) { func requestNats(subject string, data []byte) (*Response, error) {
var ( var (
req = RequestBody{ req = RequestBody{
ApiKey: c.Config.ApiKey, ApiKey: client.Config.ApiKey,
Body: data, Body: data,
} }
res *Response res *Response
) )
msg, err := c.natsServer.Request(subject, toBytes(req)) msg, err := client.natsServer.Request(subject, toBytes(req))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -76,16 +76,16 @@ func (c *Client) RequestNats(subject string, data []byte) (*Response, error) {
return res, nil return res, nil
} }
// PublishWithJetStream // publishWithJetStream
// Sync data to services ES with JetStream // Sync data to services ES with JetStream
func (c *Client) PublishWithJetStream(streamName, subject string, data []byte) (bool, error) { func publishWithJetStream(streamName, subject string, data []byte) (bool, error) {
var ( var (
req = RequestBody{ req = RequestBody{
ApiKey: c.Config.ApiKey, ApiKey: client.Config.ApiKey,
Body: data, Body: data,
} }
) )
err := c.natsJetStream.Publish(streamName, subject, toBytes(req)) err := client.natsJetStream.Publish(streamName, subject, toBytes(req))
if err != nil { if err != nil {
return false, err return false, err
} }

View File

@ -6,15 +6,15 @@ type Pull struct {
// ProductUpsert ... // ProductUpsert ...
func (r Pull) ProductUpsert(query UpdateDataPayload) (bool, error) { func (r Pull) ProductUpsert(query UpdateDataPayload) (bool, error) {
return GetClient().PublishWithJetStream(JetStreamSearchService, SubjectPullProductUpsert, toBytes(query)) return publishWithJetStream(JetStreamSearchService, SubjectPullProductUpsert, toBytes(query))
} }
// UserUpsert ... // UserUpsert ...
func (r Pull) UserUpsert(query UpdateDataPayload) (bool, error) { func (r Pull) UserUpsert(query UpdateDataPayload) (bool, error) {
return GetClient().PublishWithJetStream(JetStreamSearchService, SubjectPullUserUpsert, toBytes(query)) return publishWithJetStream(JetStreamSearchService, SubjectPullUserUpsert, toBytes(query))
} }
// OrderUpsert ... // OrderUpsert ...
func (r Pull) OrderUpsert(query UpdateDataPayload) (bool, error) { func (r Pull) OrderUpsert(query UpdateDataPayload) (bool, error) {
return GetClient().PublishWithJetStream(JetStreamSearchService, SubjectPullOrderUpsert, toBytes(query)) return publishWithJetStream(JetStreamSearchService, SubjectPullOrderUpsert, toBytes(query))
} }

View File

@ -6,30 +6,30 @@ type Request struct {
// ProductSearch ... // ProductSearch ...
func (r Request) ProductSearch(query ESQuery) (*Response, error) { func (r Request) ProductSearch(query ESQuery) (*Response, error) {
return GetClient().RequestNats(SubjectRequestProductSearch, toBytes(query)) return requestNats(SubjectRequestProductSearch, toBytes(query))
} }
// ProductUpsert ... // ProductUpsert ...
func (r Request) ProductUpsert(query UpdateDataPayload) (*Response, error) { func (r Request) ProductUpsert(query UpdateDataPayload) (*Response, error) {
return GetClient().RequestNats(SubjectRequestProductUpsert, toBytes(query)) return requestNats(SubjectRequestProductUpsert, toBytes(query))
} }
// UserSearch ... // UserSearch ...
func (r Request) UserSearch(query ESQuery) (*Response, error) { func (r Request) UserSearch(query ESQuery) (*Response, error) {
return GetClient().RequestNats(SubjectRequestUserSearch, toBytes(query)) return requestNats(SubjectRequestUserSearch, toBytes(query))
} }
// UserUpsert ... // UserUpsert ...
func (r Request) UserUpsert(query UpdateDataPayload) (*Response, error) { func (r Request) UserUpsert(query UpdateDataPayload) (*Response, error) {
return GetClient().RequestNats(SubjectRequestUserUpsert, toBytes(query)) return requestNats(SubjectRequestUserUpsert, toBytes(query))
} }
// OrderSearch ... // OrderSearch ...
func (r Request) OrderSearch(query ESQuery) (*Response, error) { func (r Request) OrderSearch(query ESQuery) (*Response, error) {
return GetClient().RequestNats(SubjectRequestOrderSearch, toBytes(query)) return requestNats(SubjectRequestOrderSearch, toBytes(query))
} }
// OrderUpsert ... // OrderUpsert ...
func (r Request) OrderUpsert(query UpdateDataPayload) (*Response, error) { func (r Request) OrderUpsert(query UpdateDataPayload) (*Response, error) {
return GetClient().RequestNats(SubjectRequestOrderUpsert, toBytes(query)) return requestNats(SubjectRequestOrderUpsert, toBytes(query))
} }