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

fix(BUX-500): simplify by removing options and placing deploymentID in config instead #86

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ custom features to work with multiple nodes and retry logic.
### Create client

```go
// Set the access token and url for the node
// Set the access token and url for the node
token := ""
apiURL := "https://tapi.taal.com/arc"
// also there is a possibility to set deploymentID which is optional and adds header XDeployment-ID to the request
deploymentID := "broadcast-client-example"

cfg := broadcast_client.ArcClientConfig{
Token: token,
APIUrl: apiURL,
Token: token,
APIUrl: apiURL,
DeploymentID: deploymentID,
}

client := broadcast_client.Builder().
WithArc(cfg, &logger, broadcast_client.WithXDeploymentID("broadcast-client-example")).
WithArc(cfg, &logger).
Build()
```

As you can see, you can inject the logger and set the deployment id for the client.
DeploymentID is a header that will be added to each request to the node - it is used for monitoring purposes.

### Use the method exposed by the interface

```go
Expand Down
26 changes: 0 additions & 26 deletions broadcast/broadcast-client/arc_client_opts.go

This file was deleted.

9 changes: 7 additions & 2 deletions broadcast/broadcast-client/arc_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ package broadcast_client
// ArcClientConfig is used by [WithArc] to set up the connection between the broadcast client and Arc.
// The provided token will be used as the Authorization header.
type ArcClientConfig struct {
APIUrl string
Token string
APIUrl string
Token string
DeploymentID string
}

// GetApiUrl returns the API url.
Expand All @@ -17,3 +18,7 @@ func (c *ArcClientConfig) GetApiUrl() string {
func (c *ArcClientConfig) GetToken() string {
return c.Token
}

func (c *ArcClientConfig) GetDeploymentID() string {
return c.DeploymentID
}
9 changes: 2 additions & 7 deletions broadcast/broadcast-client/client_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,9 @@ func (cb *builder) WithHttpClient(client httpclient.HTTPInterface) *builder {

// WithArc sets up the connection of the broadcast client to the Arc service using the provided ArcClientConfig.
// This method can be called multiple times with different ArcClientConfigurations to establish connections to multiple Arc instances.
func (cb *builder) WithArc(config ArcClientConfig, log *zerolog.Logger, opts ...ArcClientOptFunc) *builder {
options := &ArcClientOpts{}
for _, opt := range opts {
opt(options)
}

func (cb *builder) WithArc(config ArcClientConfig, log *zerolog.Logger) *builder {
cb.factories = append(cb.factories, func() broadcast_api.Client {
return arc.NewArcClient(&config, cb.client, log, options)
return arc.NewArcClient(&config, cb.client, log)
})
return cb
}
Expand Down
27 changes: 13 additions & 14 deletions broadcast/internal/arc/arc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,33 @@ const (
arcSubmitBatchTxsRoute = "/v1/txs"
)

type ClientOptions interface {
GetArcClientHeaders() map[string]string
}
const XDeploymentIDHeader = "XDeployment-ID"

type Config interface {
GetApiUrl() string
GetToken() string
GetDeploymentID() string
}

type ArcClient struct {
apiURL string
token string
headers map[string]string
HTTPClient httpclient.HTTPInterface
Logger *zerolog.Logger
apiURL string
token string
deploymentID string
HTTPClient httpclient.HTTPInterface
Logger *zerolog.Logger
}

func NewArcClient(config Config, client httpclient.HTTPInterface, log *zerolog.Logger, opts ClientOptions) broadcast_api.Client {
func NewArcClient(config Config, client httpclient.HTTPInterface, log *zerolog.Logger) broadcast_api.Client {
if client == nil {
client = httpclient.NewHttpClient()
}

arcClient := &ArcClient{
headers: opts.GetArcClientHeaders(),
apiURL: config.GetApiUrl(),
token: config.GetToken(),
HTTPClient: client,
Logger: log,
apiURL: config.GetApiUrl(),
token: config.GetToken(),
deploymentID: config.GetDeploymentID(),
HTTPClient: client,
Logger: log,
}

log.Debug().Msgf("Created new arc client with api url: %s", arcClient.apiURL)
Expand Down
4 changes: 2 additions & 2 deletions broadcast/internal/arc/arc_policy_quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func getPolicyQuote(ctx context.Context, arc *ArcClient) (*broadcast.PolicyQuote
nil,
)

if arc.headers != nil {
pld.Headers = arc.headers
if arc.deploymentID != "" {
pld.AddHeader(XDeploymentIDHeader, arc.deploymentID)
}

return httpclient.RequestModel(
Expand Down
4 changes: 2 additions & 2 deletions broadcast/internal/arc/arc_query_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func queryTransaction(ctx context.Context, arc *ArcClient, txHash string) (*broa
nil,
)

if arc.headers != nil {
pld.Headers = arc.headers
if arc.deploymentID != "" {
pld.AddHeader(XDeploymentIDHeader, arc.deploymentID)
}

parseResponse := func(resp *http.Response) (*broadcast.QueryTxResponse, error) {
Expand Down
12 changes: 5 additions & 7 deletions broadcast/internal/arc/arc_submit_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func submitTransaction(ctx context.Context, arc *ArcClient, tx *broadcast.Transa
arc.token,
data,
)
appendSubmitTxHeaders(&pld, opts, arc.headers)
appendSubmitTxHeaders(&pld, opts, arc.deploymentID)

return httpclient.RequestModel(
ctx,
Expand All @@ -127,7 +127,7 @@ func submitBatchTransactions(ctx context.Context, arc *ArcClient, txs []*broadca
arc.token,
data,
)
appendSubmitTxHeaders(&pld, opts, arc.headers)
appendSubmitTxHeaders(&pld, opts, arc.deploymentID)

return httpclient.RequestModel(
ctx,
Expand Down Expand Up @@ -170,7 +170,7 @@ func createSubmitBatchTxsBody(arc *ArcClient, txs []*broadcast.Transaction, txFo
return data, nil
}

func appendSubmitTxHeaders(pld *httpclient.HTTPRequest, opts *broadcast.TransactionOpts, clientHeaders map[string]string) {
func appendSubmitTxHeaders(pld *httpclient.HTTPRequest, opts *broadcast.TransactionOpts, deploymentID string) {
if opts == nil {
return
}
Expand All @@ -191,10 +191,8 @@ func appendSubmitTxHeaders(pld *httpclient.HTTPRequest, opts *broadcast.Transact
pld.AddHeader("X-WaitForStatus", strconv.Itoa(statusCode))
}

if len(clientHeaders) > 0 {
for key, value := range clientHeaders {
pld.AddHeader(key, value)
}
if deploymentID != "" {
pld.AddHeader(XDeploymentIDHeader, deploymentID)
}
}

Expand Down
2 changes: 1 addition & 1 deletion broadcast/internal/arc/arc_submit_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestSubmitTransaction(t *testing.T) {
"someToken",
body,
)
appendSubmitTxHeaders(&expectedPayload, nil, client.headers)
appendSubmitTxHeaders(&expectedPayload, nil, client.deploymentID)

mockHttpClient.On("DoRequest", context.Background(), expectedPayload).
Return(tc.httpResponse, tc.httpError).Once()
Expand Down
8 changes: 5 additions & 3 deletions examples/get_fee_quote/get_fee_quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ import (
// This example shows how to get a fee quote.
func main() {
logger := zerolog.Nop()
deploymentID := "broadcast-client-example"

gorillaCfg := broadcast_client.ArcClientConfig{
Token: "",
APIUrl: "https://arc.gorillapool.io",
Token: "",
APIUrl: "https://arc.gorillapool.io",
DeploymentID: deploymentID,
}

client := broadcast_client.Builder().
WithArc(gorillaCfg, &logger, broadcast_client.WithXDeploymentID("broadcast-client-example")).
WithArc(gorillaCfg, &logger).
Build()

feeQuotes, err := client.GetFeeQuote(context.Background())
Expand Down
8 changes: 5 additions & 3 deletions examples/get_policy_quote/get_policy_quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ import (
// This example shows how to get a policy quote.
func main() {
logger := zerolog.Nop()
deploymentID := "broadcast-client-example"

gorillaCfg := broadcast_client.ArcClientConfig{
Token: "",
APIUrl: "https://arc.gorillapool.io",
Token: "",
APIUrl: "https://arc.gorillapool.io",
DeploymentID: deploymentID,
}

client := broadcast_client.Builder().
WithArc(gorillaCfg, &logger, broadcast_client.WithXDeploymentID("broadcast-client-example")).
WithArc(gorillaCfg, &logger).
Build()

policyQuotes, err := client.GetPolicyQuote(context.Background())
Expand Down
8 changes: 5 additions & 3 deletions examples/query_transaction/query_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ func main() {
logger := zerolog.Nop()
token := ""
apiURL := "https://arc.gorillapool.io"
deploymentID := "broadcast-client-example"
txID := "469dd0f63fe4b3fe972dc72d28057e931abd69d8dfc85bf6e623efa41d50ef73"

cfg := broadcast_client.ArcClientConfig{
Token: token,
APIUrl: apiURL,
Token: token,
APIUrl: apiURL,
DeploymentID: deploymentID,
}

client := broadcast_client.Builder().
WithArc(cfg, &logger, broadcast_client.WithXDeploymentID("broadcast-client-example")).
WithArc(cfg, &logger).
Build()

result, err := client.QueryTransaction(context.Background(), txID)
Expand Down
8 changes: 5 additions & 3 deletions examples/submit_batch_transactions/submit_batch_txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func main() {
logger := zerolog.Nop()
token := ""
apiURL := "https://arc.gorillapool.io"
deploymentID := "broadcast-client-example"
txs := []*broadcast.Transaction{
{Hex: "0100000001d6d1607b208b30c0a3fe21d563569c4d2a0f913604b4c5054fe267da6be324ab220000006b4830450221009a965dcd5d42983090a63cfd761038ff8adcea621c46a68a205f326292a95383022061b8d858f366c69f3ebd30a60ccafe36faca4e242ac3d2edd3bf63b669bcf23b4121034e871e147aa4a3e2f1665eaf76cf9264d089b6a91702af92bd6ce33bac84a765ffffffff0123020000000000001976a914d8819a7197d3e221e15f4348203fdecfd29fa2b888ac00000000"},
{Hex: "0100000001bbf4d68a935126df1eea3e14714c5a6438bbda3d18b7e754bc90895cf2190196060000006a47304402206a0c3923a9ae253ac5ea22d36e667f067c51f88a8bfb2861d9124bc5402fecd00220745731fa951076c63df2de6528616e0ef97ef8ade5b17d8e1d51494cf153a64b412103af3ead8a3ab792225bf22262f0b81a72e5070788d363ee717c5868421b75a62dffffffff01000000000000000040006a0a6d793263656e74732c201c4759384d656c46634d326449674d75557356644d53776f6a66467431152c20302e323135353133313536343634343531313600000000"},
Expand All @@ -24,12 +25,13 @@ func main() {
}

cfg := broadcast_client.ArcClientConfig{
Token: token,
APIUrl: apiURL,
Token: token,
APIUrl: apiURL,
DeploymentID: deploymentID,
}

client := broadcast_client.Builder().
WithArc(cfg, &logger, broadcast_client.WithXDeploymentID("broadcast-client-example")).
WithArc(cfg, &logger).
Build()

result, err := client.SubmitBatchTransactions(context.Background(), txs, broadcast.WithRawFormat())
Expand Down
8 changes: 5 additions & 3 deletions examples/submit_transaction/submit_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ import (
func main() {
logger := zerolog.Nop()
token := ""
deploymentID := "broadcast-client-example"
apiURL := "https://arc.gorillapool.io"
tx := broadcast.Transaction{Hex: "01000000026d9a56036235bb5b5e39b04b6f188c74bc45189a9122f235cad5f0c4b668817d000000006b483045022100ae4c9b06376c42bf82f7910de20bb025d8b43daf33eb2db1966f0f1fd361d499022063594799502920ceceb17e3301e44066431a5ae1e221ce1bd89b446e602adf62412102dd2063cc1d4fbc5a770b156f8cd9f5c80cc586df4c7d148444d6bb66c81a10daffffffff6e574c52ebc1c724a13d0afd525adbcfe0b134d9666ab9004240d9d072a7906b000000006a4730440220191c938793953f931c9297a27a651b07e5cb60432fd5750a3f53488ed23fae8702204c503822986ef959af741609dfe98a51004e36596ca52a865c5e029fdbcfb3d641210253085022df5ebbdc71f9e8f555443660cda4a3d36d732685be317d7e11b9eda4ffffffff0214000000000000001976a914553236189ff9fed552837b952e404e09f78c03fa88ac01000000000000001976a914d8f00ced3f960ffa6f0516b4e352e0a9feccc3af88ac00000000"}

cfg := broadcast_client.ArcClientConfig{
Token: token,
APIUrl: apiURL,
Token: token,
APIUrl: apiURL,
DeploymentID: deploymentID,
}

client := broadcast_client.Builder().
WithArc(cfg, &logger, broadcast_client.WithXDeploymentID("broadcast-client-example")).
WithArc(cfg, &logger).
Build()

result, err := client.SubmitTransaction(context.Background(), &tx, broadcast.WithRawFormat())
Expand Down
Loading