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

Commit

Permalink
feat(BUX-500): add X-DeploymentID header Arc client option (#84)
Browse files Browse the repository at this point in the history
* feat: add arc client headers

* fix: fix the name of xdeployment-id header

* feat: add required header for to all examples

* docs: update readme
  • Loading branch information
wregulski authored Feb 2, 2024
1 parent 22d947f commit f14864f
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 13 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ custom features to work with multiple nodes and retry logic.
}

client := broadcast_client.Builder().
WithArc(cfg).
WithArc(cfg, &logger, broadcast_client.WithXDeploymentID("broadcast-client-example")).
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: 26 additions & 0 deletions broadcast/broadcast-client/arc_client_opts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package broadcast_client

// ArcClientOptFunc defines an optional arguments that can be passed to the SubmitTransaction method.
type ArcClientOptFunc func(o *ArcClientOpts)

type ArcClientOpts struct {
// XDeploymentID is the deployment id (id of instance using Arc API) that will be sent in the request header.
XDeploymentID string
}

// GetApiUrl returns the API url.
func (c *ArcClientOpts) GetArcClientHeaders() map[string]string {
headers := make(map[string]string)
if c.XDeploymentID != "" {
headers["XDeployment-ID"] = c.XDeploymentID
}

return headers
}

// WithXDeploymentID is an option that allows you to set the deployment id (id of instance using Arc API) that will be sent in the request header.
func WithXDeploymentID(deploymentID string) ArcClientOptFunc {
return func(o *ArcClientOpts) {
o.XDeploymentID = deploymentID
}
}
9 changes: 7 additions & 2 deletions broadcast/broadcast-client/client_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ 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) *builder {
func (cb *builder) WithArc(config ArcClientConfig, log *zerolog.Logger, opts ...ArcClientOptFunc) *builder {
options := &ArcClientOpts{}
for _, opt := range opts {
opt(options)
}

cb.factories = append(cb.factories, func() broadcast_api.Client {
return arc.NewArcClient(&config, cb.client, log)
return arc.NewArcClient(&config, cb.client, log, options)
})
return cb
}
Expand Down
8 changes: 7 additions & 1 deletion broadcast/internal/arc/arc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const (
arcSubmitBatchTxsRoute = "/v1/txs"
)

type ClientOptions interface {
GetArcClientHeaders() map[string]string
}

type Config interface {
GetApiUrl() string
GetToken() string
Expand All @@ -21,16 +25,18 @@ type Config interface {
type ArcClient struct {
apiURL string
token string
headers map[string]string
HTTPClient httpclient.HTTPInterface
Logger *zerolog.Logger
}

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

arcClient := &ArcClient{
headers: opts.GetArcClientHeaders(),
apiURL: config.GetApiUrl(),
token: config.GetToken(),
HTTPClient: client,
Expand Down
4 changes: 4 additions & 0 deletions broadcast/internal/arc/arc_policy_quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func getPolicyQuote(ctx context.Context, arc *ArcClient) (*broadcast.PolicyQuote
nil,
)

if arc.headers != nil {
pld.Headers = arc.headers
}

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

if arc.headers != nil {
pld.Headers = arc.headers
}

parseResponse := func(resp *http.Response) (*broadcast.QueryTxResponse, error) {
return decodeQueryResponseBody(resp, arc)
}
Expand Down
12 changes: 9 additions & 3 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)
appendSubmitTxHeaders(&pld, opts, arc.headers)

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)
appendSubmitTxHeaders(&pld, opts, arc.headers)

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) {
func appendSubmitTxHeaders(pld *httpclient.HTTPRequest, opts *broadcast.TransactionOpts, clientHeaders map[string]string) {
if opts == nil {
return
}
Expand All @@ -190,6 +190,12 @@ func appendSubmitTxHeaders(pld *httpclient.HTTPRequest, opts *broadcast.Transact
if statusCode, ok := broadcast.MapTxStatusToInt(opts.WaitForStatus); ok {
pld.AddHeader("X-WaitForStatus", strconv.Itoa(statusCode))
}

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

func decodeSubmitResponseBody(resp *http.Response) (*broadcast.SubmittedTx, error) {
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)
appendSubmitTxHeaders(&expectedPayload, nil, client.headers)

mockHttpClient.On("DoRequest", context.Background(), expectedPayload).
Return(tc.httpResponse, tc.httpError).Once()
Expand Down
2 changes: 1 addition & 1 deletion examples/get_fee_quote/get_fee_quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {
}

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

feeQuotes, err := client.GetFeeQuote(context.Background())
Expand Down
2 changes: 1 addition & 1 deletion examples/get_policy_quote/get_policy_quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {
}

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

policyQuotes, err := client.GetPolicyQuote(context.Background())
Expand Down
2 changes: 1 addition & 1 deletion examples/query_transaction/query_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {
}

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

result, err := client.QueryTransaction(context.Background(), txID)
Expand Down
2 changes: 1 addition & 1 deletion examples/submit_batch_transactions/submit_batch_txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
}

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

result, err := client.SubmitBatchTransactions(context.Background(), txs, broadcast.WithRawFormat())
Expand Down
2 changes: 1 addition & 1 deletion examples/submit_transaction/submit_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {
}

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

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

0 comments on commit f14864f

Please sign in to comment.