Compare commits

..

No commits in common. "baeb5e0ccba55d937a985e4b5313ba573bfe4850" and "d3fae34290049dc67d00f24575439b2f89b2fd4a" have entirely different histories.

3 changed files with 31 additions and 25 deletions

View File

@ -16,5 +16,6 @@ DB_TIMEZONE=Africa/Nairobi
#External API Calls
CUSTODIAL_URL_BASE=http://localhost:5003
BEARER_TOKEN=eyJeSIsInRcCI6IkpXVCJ.yJwdWJsaWNLZXkiOiIwrrrrrr
CUSTODIAL_BEARER_TOKEN=eyJeSIsInRcCI6IkpXVCJ.yJwdWJsaWNLZXkiOiIwrrrrrr
DATA_URL_BASE=http://localhost:5006
DATA_BEARER_TOKEN=eyJeSIsIRcCI6IXVCJ.yJwdWJsaLZXkiOiIwrrrrrr

View File

@ -20,7 +20,8 @@ const (
var (
custodialURLBase string
dataURLBase string
BearerToken string
CustodialBearerToken string
DataBearerToken string
)
var (
@ -39,7 +40,8 @@ func setBase() error {
custodialURLBase = initializers.GetEnv("CUSTODIAL_URL_BASE", "http://localhost:5003")
dataURLBase = initializers.GetEnv("DATA_URL_BASE", "http://localhost:5006")
BearerToken = initializers.GetEnv("BEARER_TOKEN", "")
CustodialBearerToken = initializers.GetEnv("CUSTODIAL_BEARER_TOKEN", "")
DataBearerToken = initializers.GetEnv("DATA_BEARER_TOKEN", "")
_, err = url.JoinPath(custodialURLBase, "/foo")
if err != nil {

View File

@ -51,7 +51,7 @@ func (as *AccountService) TrackAccountStatus(ctx context.Context, publicKey stri
return nil, err
}
_, err = doRequest(ctx, req, &r)
_, err = doCustodialRequest(ctx, req, &r)
if err != nil {
return nil, err
}
@ -75,7 +75,7 @@ func (as *AccountService) CheckBalance(ctx context.Context, publicKey string) (*
return nil, err
}
_, err = doRequest(ctx, req, &balanceResult)
_, err = doCustodialRequest(ctx, req, &balanceResult)
return &balanceResult, err
}
@ -92,7 +92,7 @@ func (as *AccountService) CreateAccount(ctx context.Context) (*models.AccountRes
if err != nil {
return nil, err
}
_, err = doRequest(ctx, req, &r)
_, err = doCustodialRequest(ctx, req, &r)
if err != nil {
return nil, err
}
@ -104,9 +104,7 @@ func (as *AccountService) CreateAccount(ctx context.Context) (*models.AccountRes
// Parameters:
// - publicKey: The public key associated with the account.
func (as *AccountService) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
var r struct {
Holdings []dataserviceapi.TokenHoldings `json:"holdings"`
}
var r []dataserviceapi.TokenHoldings
ep, err := url.JoinPath(config.VoucherHoldingsURL, publicKey)
if err != nil {
@ -118,21 +116,19 @@ func (as *AccountService) FetchVouchers(ctx context.Context, publicKey string) (
return nil, err
}
_, err = doRequest(ctx, req, &r)
_, err = doDataRequest(ctx, req, r)
if err != nil {
return nil, err
}
return r.Holdings, nil
return r, nil
}
// FetchTransactions retrieves the last 10 transactions for a given public key from the data indexer API endpoint
// Parameters:
// - publicKey: The public key associated with the account.
func (as *AccountService) FetchTransactions(ctx context.Context, publicKey string) ([]dataserviceapi.Last10TxResponse, error) {
var r struct {
Transfers []dataserviceapi.Last10TxResponse `json:"transfers"`
}
var r []dataserviceapi.Last10TxResponse
ep, err := url.JoinPath(config.VoucherTransfersURL, publicKey)
if err != nil {
@ -144,12 +140,12 @@ func (as *AccountService) FetchTransactions(ctx context.Context, publicKey strin
return nil, err
}
_, err = doRequest(ctx, req, &r)
_, err = doDataRequest(ctx, req, r)
if err != nil {
return nil, err
}
return r.Transfers, nil
return r, nil
}
// VoucherData retrieves voucher metadata from the data indexer API endpoint.
@ -168,7 +164,7 @@ func (as *AccountService) VoucherData(ctx context.Context, address string) (*mod
return nil, err
}
_, err = doRequest(ctx, req, &voucherDataResult)
_, err = doCustodialRequest(ctx, req, &voucherDataResult)
return &voucherDataResult, err
}
@ -199,7 +195,7 @@ func (as *AccountService) TokenTransfer(ctx context.Context, amount, from, to, t
if err != nil {
return nil, err
}
_, err = doRequest(ctx, req, &r)
_, err = doCustodialRequest(ctx, req, &r)
if err != nil {
return nil, err
}
@ -210,12 +206,7 @@ func (as *AccountService) TokenTransfer(ctx context.Context, amount, from, to, t
func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
var okResponse api.OKResponse
var errResponse api.ErrResponse
req.Header.Set("Authorization", "Bearer "+config.BearerToken)
req.Header.Set("Content-Type", "application/json")
logRequestDetails(req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("Failed to make %s request to endpoint: %s with reason: %s", req.Method, req.URL, err.Error())
@ -253,6 +244,18 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons
return &okResponse, err
}
func doCustodialRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
req.Header.Set("Authorization", "Bearer "+config.CustodialBearerToken)
logRequestDetails(req)
return doRequest(ctx, req, rcpt)
}
func doDataRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
req.Header.Set("Authorization", "Bearer "+config.DataBearerToken)
logRequestDetails(req)
return doRequest(ctx, req, rcpt)
}
func logRequestDetails(req *http.Request) {
var bodyBytes []byte
contentType := req.Header.Get("Content-Type")