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 #External API Calls
CUSTODIAL_URL_BASE=http://localhost:5003 CUSTODIAL_URL_BASE=http://localhost:5003
BEARER_TOKEN=eyJeSIsInRcCI6IkpXVCJ.yJwdWJsaWNLZXkiOiIwrrrrrr CUSTODIAL_BEARER_TOKEN=eyJeSIsInRcCI6IkpXVCJ.yJwdWJsaWNLZXkiOiIwrrrrrr
DATA_URL_BASE=http://localhost:5006 DATA_URL_BASE=http://localhost:5006
DATA_BEARER_TOKEN=eyJeSIsIRcCI6IXVCJ.yJwdWJsaLZXkiOiIwrrrrrr

View File

@ -20,7 +20,8 @@ const (
var ( var (
custodialURLBase string custodialURLBase string
dataURLBase string dataURLBase string
BearerToken string CustodialBearerToken string
DataBearerToken string
) )
var ( var (
@ -39,7 +40,8 @@ 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")
BearerToken = initializers.GetEnv("BEARER_TOKEN", "") CustodialBearerToken = initializers.GetEnv("CUSTODIAL_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 = doRequest(ctx, req, &r) _, err = doCustodialRequest(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 = doRequest(ctx, req, &balanceResult) _, err = doCustodialRequest(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 = doRequest(ctx, req, &r) _, err = doCustodialRequest(ctx, req, &r)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -104,9 +104,7 @@ 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 struct { var r []dataserviceapi.TokenHoldings
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 {
@ -118,21 +116,19 @@ func (as *AccountService) FetchVouchers(ctx context.Context, publicKey string) (
return nil, err return nil, err
} }
_, err = doRequest(ctx, req, &r) _, err = doDataRequest(ctx, req, r)
if err != nil { if err != nil {
return nil, err 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 // 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 struct { var r []dataserviceapi.Last10TxResponse
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 {
@ -144,12 +140,12 @@ func (as *AccountService) FetchTransactions(ctx context.Context, publicKey strin
return nil, err return nil, err
} }
_, err = doRequest(ctx, req, &r) _, err = doDataRequest(ctx, req, r)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return r.Transfers, nil return r, nil
} }
// VoucherData retrieves voucher metadata from the data indexer API endpoint. // 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 return nil, err
} }
_, err = doRequest(ctx, req, &voucherDataResult) _, err = doCustodialRequest(ctx, req, &voucherDataResult)
return &voucherDataResult, err return &voucherDataResult, err
} }
@ -199,7 +195,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 = doRequest(ctx, req, &r) _, err = doCustodialRequest(ctx, req, &r)
if err != nil { if err != nil {
return nil, err 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) { 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())
@ -253,6 +244,18 @@ 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")