Compare commits

...

2 Commits

3 changed files with 25 additions and 31 deletions

View File

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

View File

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

View File

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