Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

feat(BUX-162): moves headers opts out of tx struct #14

Merged
merged 2 commits into from
Aug 17, 2023
Merged

Conversation

wregulski
Copy link
Contributor

No description provided.

Comment on lines 157 to 177
func WithCallback(callbackURL string, callbackToken ...string) broadcast.TransactionOptFunc {
return func(o *broadcast.TransactionOpts) {
o.CallbackToken = callbackURL
if len(callbackToken) > 0 {
o.CallbackToken = callbackToken[0]
}
}
}

func WithMerkleProof() broadcast.TransactionOptFunc {
return func(o *broadcast.TransactionOpts) {
o.MerkleProof = true
}
}

func WithWaitForStatus(status broadcast.TxStatus) broadcast.TransactionOptFunc {
return func(o *broadcast.TransactionOpts) {
o.WaitForStatus = status
}
}

func appendSubmitTxHeaders(pld *httpclient.HTTPRequest, opts *broadcast.TransactionOpts) {
if opts == nil {
return
}

if opts.MerkleProof {
pld.AddHeader("X-MerkleProof", "true")
}

if tx.CallBackURL != "" {
pld.AddHeader("X-CallbackUrl", tx.CallBackURL)
if opts.CallbackURL != "" {
pld.AddHeader("X-CallbackUrl", opts.CallbackURL)
}

if tx.CallBackToken != "" {
pld.AddHeader("X-CallbackToken", tx.CallBackToken)
if opts.CallbackToken != "" {
pld.AddHeader("X-CallbackToken", opts.CallbackToken)
}

if statusCode, ok := broadcast.MapTxStatusToInt(tx.WaitForStatus); ok {
if statusCode, ok := broadcast.MapTxStatusToInt(opts.WaitForStatus); ok {
pld.AddHeader("X-WaitForStatus", strconv.Itoa(statusCode))
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor

I would rather want to see it as those With*** will append headers by themselves.
I will try to present that in the following pseudo code:

func submitTransaction(ctx context.Context, arc *ArcClient, tx *broadcast.Transaction, opts ...broadcast.TransactionOptFunc) (*broadcast.SubmitTxResponse, error) {
       // (...)
       // instead of this => appendSubmitTxHeaders(&pld, opts), there will be following: 
      	for _, opt := range opts {
		opt(&pld)
	}
     
      //(...)
}


func WithCallback(callbackURL string, callbackToken ...string) broadcast.TransactionOptFunc {
	return func(o *broadcast.TransactionOpts) {
		pld.AddHeader("X-CallbackUrl", callbackURL)

	        if len(callbackToken) > 0 {
		    pld.AddHeader("X-CallbackToken", callbackToken[])
	        }
	}
}

// ... rest of With methods

dorzepowski
dorzepowski previously approved these changes Aug 16, 2023
Copy link
Contributor

@arkadiuszos4chain arkadiuszos4chain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just

func (a *ArcClient) SubmitTransaction(ctx context.Context, tx *broadcast.Transaction, options *TransactionOpts)

I don't understand why we need "functional" aproach here.

@wregulski wregulski merged commit f06b5db into main Aug 17, 2023
@wregulski wregulski deleted the feat-bux-162 branch August 17, 2023 07:22
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants