generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(SPV-1061): arc requests moved to a single package (#721)
- Loading branch information
1 parent
e3bff84
commit 62ba56c
Showing
12 changed files
with
181 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,19 @@ | ||
package chain | ||
|
||
import ( | ||
"github.com/bitcoin-sv/spv-wallet/engine/chain/internal/policy" | ||
"github.com/bitcoin-sv/spv-wallet/engine/chain/internal/query" | ||
"github.com/bitcoin-sv/spv-wallet/engine/chain/internal/arc" | ||
"github.com/bitcoin-sv/spv-wallet/engine/chain/models" | ||
"github.com/go-resty/resty/v2" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
type chainService struct { | ||
QueryService | ||
PolicyService | ||
ARCService | ||
} | ||
|
||
// NewChainService creates a new chain service. | ||
func NewChainService(logger zerolog.Logger, httpClient *resty.Client, arcCfg chainmodels.ARCConfig) Service { | ||
return &chainService{ | ||
query.NewQueryService(logger.With().Str("chain", "query").Logger(), httpClient, arcCfg), | ||
policy.NewPolicyService(logger.With().Str("chain", "policy").Logger(), httpClient, arcCfg), | ||
arc.NewARCService(logger.With().Str("chain", "arc").Logger(), httpClient, arcCfg), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package arc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
chainmodels "github.com/bitcoin-sv/spv-wallet/engine/chain/models" | ||
"github.com/bitcoin-sv/spv-wallet/engine/spverrors" | ||
) | ||
|
||
// GetPolicy requests ARC server for the policy | ||
func (s *Service) GetPolicy(ctx context.Context) (*chainmodels.Policy, error) { | ||
result := &chainmodels.Policy{} | ||
arcErr := &chainmodels.ArcError{} | ||
req := s.prepareARCRequest(ctx). | ||
SetResult(result). | ||
SetError(arcErr) | ||
|
||
response, err := req.Get(fmt.Sprintf("%s/v1/policy", s.arcCfg.URL)) | ||
|
||
if err != nil { | ||
return nil, s.wrapRequestError(err) | ||
} | ||
|
||
switch response.StatusCode() { | ||
case http.StatusOK: | ||
return result, nil | ||
case http.StatusUnauthorized, http.StatusForbidden: | ||
return nil, s.wrapARCError(spverrors.ErrARCUnauthorized, arcErr) | ||
case http.StatusNotFound: | ||
return nil, spverrors.ErrARCUnreachable | ||
default: | ||
return nil, s.wrapARCError(spverrors.ErrARCUnsupportedStatusCode, arcErr) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package arc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/bitcoin-sv/spv-wallet/engine/chain/models" | ||
"github.com/bitcoin-sv/spv-wallet/engine/spverrors" | ||
) | ||
|
||
// QueryTransaction a transaction. | ||
func (s *Service) QueryTransaction(ctx context.Context, txID string) (*chainmodels.TXInfo, error) { | ||
result := &chainmodels.TXInfo{} | ||
arcErr := &chainmodels.ArcError{} | ||
req := s.prepareARCRequest(ctx). | ||
SetResult(result). | ||
SetError(arcErr) | ||
|
||
response, err := req.Get(fmt.Sprintf("%s/v1/tx/%s", s.arcCfg.URL, txID)) | ||
|
||
if err != nil { | ||
return nil, s.wrapRequestError(err) | ||
} | ||
|
||
switch response.StatusCode() { | ||
case http.StatusOK: | ||
return result, nil | ||
case http.StatusUnauthorized, http.StatusForbidden: | ||
return nil, s.wrapARCError(spverrors.ErrARCUnauthorized, arcErr) | ||
case http.StatusNotFound: | ||
if !arcErr.IsEmpty() { | ||
// ARC returns 404 when transaction is not found | ||
return nil, nil // By convention, nil is returned when transaction is not found | ||
} | ||
return nil, spverrors.ErrARCUnreachable | ||
case http.StatusConflict: | ||
return nil, s.wrapARCError(spverrors.ErrARCGenericError, arcErr) | ||
default: | ||
return nil, s.wrapARCError(spverrors.ErrARCUnsupportedStatusCode, arcErr) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package arc | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net" | ||
|
||
chainmodels "github.com/bitcoin-sv/spv-wallet/engine/chain/models" | ||
"github.com/bitcoin-sv/spv-wallet/engine/spverrors" | ||
"github.com/bitcoin-sv/spv-wallet/models" | ||
"github.com/go-resty/resty/v2" | ||
) | ||
|
||
func (s *Service) prepareARCRequest(ctx context.Context) *resty.Request { | ||
req := s.httpClient.R(). | ||
SetContext(ctx). | ||
SetHeader("Content-Type", "application/json") | ||
|
||
if s.arcCfg.Token != "" { | ||
req.SetHeader("Authorization", s.arcCfg.Token) | ||
} | ||
|
||
if s.arcCfg.DeploymentID != "" { | ||
req.SetHeader("XDeployment-ID", s.arcCfg.DeploymentID) | ||
} | ||
|
||
return req | ||
} | ||
|
||
func (s *Service) wrapRequestError(err error) error { | ||
var e net.Error | ||
if errors.As(err, &e) { | ||
return spverrors.ErrARCUnreachable.Wrap(e) | ||
} | ||
return spverrors.ErrInternal.Wrap(err) | ||
} | ||
|
||
func (s *Service) wrapARCError(baseError models.SPVError, errResult *chainmodels.ArcError) error { | ||
if errResult == nil || errResult.IsEmpty() { | ||
return baseError | ||
} | ||
return baseError.Wrap(errResult) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package arc | ||
|
||
import ( | ||
chainmodels "github.com/bitcoin-sv/spv-wallet/engine/chain/models" | ||
"github.com/go-resty/resty/v2" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
// Service for arc requests. | ||
type Service struct { | ||
logger zerolog.Logger | ||
httpClient *resty.Client | ||
arcCfg chainmodels.ARCConfig | ||
} | ||
|
||
// NewARCService creates a new arc service. | ||
func NewARCService(logger zerolog.Logger, httpClient *resty.Client, arcCfg chainmodels.ARCConfig) *Service { | ||
return &Service{ | ||
logger: logger, | ||
httpClient: httpClient, | ||
arcCfg: arcCfg, | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.