WIP refactor models usage
This commit is contained in:
parent
4e81e2d869
commit
1e638238ed
@ -1,18 +1,67 @@
|
||||
package config
|
||||
|
||||
import "git.grassecon.net/urdt/ussd/initializers"
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"git.grassecon.net/urdt/ussd/initializers"
|
||||
)
|
||||
|
||||
const (
|
||||
createAccountPath = "/api/v2/account/create"
|
||||
trackStatusPath = "/api/track"
|
||||
balancePathPrefix = "/api/account"
|
||||
trackPath = "/api/v2/account/status"
|
||||
voucherHoldingsPathPrefix = "/api/v1/holdings"
|
||||
voucherTransfersPathPrefix = "/api/v1/transfers/last10"
|
||||
)
|
||||
|
||||
var (
|
||||
custodialURLBase string
|
||||
dataURLBase string
|
||||
CustodialAPIKey string
|
||||
DataAPIKey string
|
||||
)
|
||||
|
||||
var (
|
||||
CreateAccountURL string
|
||||
TrackStatusURL string
|
||||
BalanceURL string
|
||||
TrackURL string
|
||||
BalanceURL string
|
||||
TrackURL string
|
||||
VoucherHoldingsURL string
|
||||
VoucherTransfersURL string
|
||||
)
|
||||
|
||||
// LoadConfig initializes the configuration values after environment variables are loaded.
|
||||
func LoadConfig() {
|
||||
CreateAccountURL = initializers.GetEnv("CREATE_ACCOUNT_URL", "http://localhost:5003/api/v2/account/create")
|
||||
TrackStatusURL = initializers.GetEnv("TRACK_STATUS_URL", "https://custodial.sarafu.africa/api/track/")
|
||||
BalanceURL = initializers.GetEnv("BALANCE_URL", "https://custodial.sarafu.africa/api/account/status/")
|
||||
TrackURL = initializers.GetEnv("TRACK_URL", "http://localhost:5003/api/v2/account/status")
|
||||
func setBase() error {
|
||||
var err error
|
||||
|
||||
custodialURLBase = initializers.GetEnv("CUSTODIAL_URL_BASE", "http://localhost:5003")
|
||||
dataURLBase = initializers.GetEnv("DATA_URL_BASE", "http://localhost:5006")
|
||||
CustodialAPIKey = initializers.GetEnv("CUSTODIAL_API_KEY", "xd")
|
||||
DataAPIKey = initializers.GetEnv("DATA_API_KEY", "xd")
|
||||
|
||||
_, err = url.JoinPath(custodialURLBase, "/foo")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = url.JoinPath(dataURLBase, "/bar")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadConfig initializes the configuration values after environment variables are loaded.
|
||||
func LoadConfig() error {
|
||||
err := setBase()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
CreateAccountURL, _ = url.JoinPath(custodialURLBase, createAccountPath)
|
||||
TrackStatusURL, _ = url.JoinPath(custodialURLBase, trackStatusPath)
|
||||
BalanceURL, _ = url.JoinPath(custodialURLBase, balancePathPrefix)
|
||||
TrackURL, _ = url.JoinPath(custodialURLBase, trackPath)
|
||||
VoucherHoldingsURL, _ = url.JoinPath(dataURLBase, voucherHoldingsPathPrefix)
|
||||
VoucherTransfersURL, _ = url.JoinPath(dataURLBase, voucherTransfersPathPrefix)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -149,12 +149,12 @@ func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (r
|
||||
|
||||
func (h *Handlers) createAccountNoExist(ctx context.Context, sessionId string, res *resource.Result) error {
|
||||
flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
|
||||
okResponse, err := h.accountService.CreateAccount(ctx)
|
||||
r, err := h.accountService.CreateAccount(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
trackingId := okResponse.Result["trackingId"].(string)
|
||||
publicKey := okResponse.Result["publicKey"].(string)
|
||||
trackingId := r.TrackingId
|
||||
publicKey := r.PublicKey
|
||||
|
||||
data := map[common.DataTyp]string{
|
||||
common.DATA_TRACKING_ID: trackingId,
|
||||
@ -682,6 +682,7 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) (
|
||||
|
||||
func (h *Handlers) FetchCustodialBalances(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
|
||||
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
|
||||
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
@ -699,14 +700,13 @@ func (h *Handlers) FetchCustodialBalances(ctx context.Context, sym string, input
|
||||
|
||||
balanceResponse, err := h.accountService.CheckBalance(ctx, string(publicKey))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
if !balanceResponse.Ok {
|
||||
res.FlagSet = append(res.FlagSet, flag_api_error)
|
||||
return res, nil
|
||||
}
|
||||
res.FlagReset = append(res.FlagReset, flag_api_error)
|
||||
balance := balanceResponse.Result.Balance
|
||||
|
||||
//balance := balanceResponse.Result.Balance
|
||||
balance := balanceResponse.Balance
|
||||
|
||||
switch balanceType {
|
||||
case "my":
|
||||
@ -1066,13 +1066,13 @@ func (h *Handlers) SetDefaultVoucher(ctx context.Context, sym string, input []by
|
||||
}
|
||||
|
||||
// Return if there is no voucher
|
||||
if len(vouchersResp.Result.Holdings) == 0 {
|
||||
if len(vouchersResp) == 0 {
|
||||
res.FlagSet = append(res.FlagSet, flag_no_active_voucher)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// Use only the first voucher
|
||||
firstVoucher := vouchersResp.Result.Holdings[0]
|
||||
firstVoucher := vouchersResp[0]
|
||||
defaultSym := firstVoucher.TokenSymbol
|
||||
defaultBal := firstVoucher.Balance
|
||||
|
||||
@ -1119,7 +1119,7 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
data := utils.ProcessVouchers(vouchersResp.Result.Holdings)
|
||||
data := utils.ProcessVouchers(vouchersResp)
|
||||
|
||||
// Store all voucher data
|
||||
dataMap := map[string]string{
|
||||
|
@ -1159,7 +1159,6 @@ func TestCheckAccountStatus(t *testing.T) {
|
||||
|
||||
// Assert that expectations were met
|
||||
mockDataStore.AssertExpectations(t)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,6 @@
|
||||
package models
|
||||
|
||||
type AccountResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Description string `json:"description"` // Include the description field
|
||||
Result struct {
|
||||
PublicKey string `json:"publicKey"`
|
||||
TrackingId string `json:"trackingId"`
|
||||
} `json:"result"`
|
||||
type AccountResult struct {
|
||||
PublicKey string `json:"publicKey"`
|
||||
TrackingId string `json:"trackingId"`
|
||||
}
|
||||
|
@ -3,10 +3,15 @@ package models
|
||||
import "encoding/json"
|
||||
|
||||
|
||||
type BalanceResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Result struct {
|
||||
Balance string `json:"balance"`
|
||||
Nonce json.Number `json:"nonce"`
|
||||
} `json:"result"`
|
||||
//type BalanceResponse struct {
|
||||
// Ok bool `json:"ok"`
|
||||
// Result struct {
|
||||
// Balance string `json:"balance"`
|
||||
// Nonce json.Number `json:"nonce"`
|
||||
// } `json:"result"`
|
||||
//}
|
||||
//
|
||||
type BalanceResult struct {
|
||||
Balance string `json:"balance"`
|
||||
Nonce json.Number `json:"nonce"`
|
||||
}
|
||||
|
@ -4,23 +4,18 @@ import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
type Transaction struct {
|
||||
//type Transaction struct {
|
||||
// CreatedAt time.Time `json:"createdAt"`
|
||||
// Status string `json:"status"`
|
||||
// TransferValue json.Number `json:"transferValue"`
|
||||
// TxHash string `json:"txHash"`
|
||||
// TxType string `json:"txType"`
|
||||
//}
|
||||
|
||||
type TrackStatusResult struct {
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Status string `json:"status"`
|
||||
TransferValue json.Number `json:"transferValue"`
|
||||
TxHash string `json:"txHash"`
|
||||
TxType string `json:"txType"`
|
||||
}
|
||||
|
||||
type TrackStatusResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Result struct {
|
||||
Transaction struct {
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Status string `json:"status"`
|
||||
TransferValue json.Number `json:"transferValue"`
|
||||
TxHash string `json:"txHash"`
|
||||
TxType string `json:"txType"`
|
||||
}
|
||||
} `json:"result"`
|
||||
}
|
||||
|
@ -1,14 +1 @@
|
||||
package models
|
||||
|
||||
import dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
|
||||
type VoucherHoldingResponse struct {
|
||||
Ok bool `json:"ok"`
|
||||
Description string `json:"description"`
|
||||
Result VoucherResult `json:"result"`
|
||||
}
|
||||
|
||||
// VoucherResult holds the list of token holdings
|
||||
type VoucherResult struct {
|
||||
Holdings []dataserviceapi.TokenHoldings `json:"holdings"`
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
|
||||
"git.grassecon.net/urdt/ussd/internal/models"
|
||||
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
@ -13,28 +14,28 @@ type MockAccountService struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (m *MockAccountService) CreateAccount(ctx context.Context) (*api.OKResponse, error) {
|
||||
func (m *MockAccountService) CreateAccount(ctx context.Context) (*models.AccountResult, error) {
|
||||
args := m.Called()
|
||||
return args.Get(0).(*api.OKResponse), args.Error(1)
|
||||
return args.Get(0).(*models.AccountResult), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockAccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResponse, error) {
|
||||
func (m *MockAccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error) {
|
||||
args := m.Called(publicKey)
|
||||
return args.Get(0).(*models.BalanceResponse), args.Error(1)
|
||||
return args.Get(0).(*models.BalanceResult), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockAccountService) CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResponse, error) {
|
||||
func (m *MockAccountService) TrackAccountStatus(ctx context.Context, trackingId string) (*api.OKResponse, error) {
|
||||
args := m.Called(trackingId)
|
||||
return args.Get(0).(*models.TrackStatusResponse), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockAccountService) TrackAccountStatus(ctx context.Context,publicKey string) (*api.OKResponse, error) {
|
||||
args := m.Called(publicKey)
|
||||
return args.Get(0).(*api.OKResponse), args.Error(1)
|
||||
}
|
||||
|
||||
|
||||
func (m *MockAccountService) FetchVouchers(ctx context.Context, publicKey string) (*models.VoucherHoldingResponse, error) {
|
||||
func (m *MockAccountService) CheckAccountStatus(ctx context.Context,publicKey string) (*models.TrackStatusResult, error) {
|
||||
args := m.Called(publicKey)
|
||||
return args.Get(0).(*models.VoucherHoldingResponse), args.Error(1)
|
||||
return args.Get(0).(*models.TrackStatusResult), args.Error(1)
|
||||
}
|
||||
|
||||
|
||||
func (m *MockAccountService) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
||||
args := m.Called(publicKey)
|
||||
return args.Get(0).([]dataserviceapi.TokenHoldings), args.Error(1)
|
||||
}
|
||||
|
@ -13,54 +13,29 @@ import (
|
||||
type TestAccountService struct {
|
||||
}
|
||||
|
||||
func (tas *TestAccountService) CreateAccount(ctx context.Context) (*api.OKResponse, error) {
|
||||
return &api.OKResponse{
|
||||
Ok: true,
|
||||
Description: "Account creation succeeded",
|
||||
Result: map[string]any{
|
||||
"trackingId": "075ccc86-f6ef-4d33-97d5-e91cfb37aa0d",
|
||||
"publicKey": "0x623EFAFa8868df4B934dd12a8B26CB3Dd75A7AdD",
|
||||
},
|
||||
func (tas *TestAccountService) CreateAccount(ctx context.Context) (*models.AccountResult, error) {
|
||||
return &models.AccountResult {
|
||||
TrackingId: "075ccc86-f6ef-4d33-97d5-e91cfb37aa0d",
|
||||
PublicKey: "0x623EFAFa8868df4B934dd12a8B26CB3Dd75A7AdD",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (tas *TestAccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResponse, error) {
|
||||
balanceResponse := &models.BalanceResponse{
|
||||
Ok: true,
|
||||
Result: struct {
|
||||
Balance string `json:"balance"`
|
||||
Nonce json.Number `json:"nonce"`
|
||||
}{
|
||||
Balance: "0.003 CELO",
|
||||
Nonce: json.Number("0"),
|
||||
},
|
||||
func (tas *TestAccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error) {
|
||||
balanceResponse := &models.BalanceResult {
|
||||
Balance: "0.003 CELO",
|
||||
Nonce: json.Number("0"),
|
||||
}
|
||||
|
||||
return balanceResponse, nil
|
||||
}
|
||||
|
||||
func (tas *TestAccountService) CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResponse, error) {
|
||||
trackResponse := &models.TrackStatusResponse{
|
||||
Ok: true,
|
||||
Result: struct {
|
||||
Transaction struct {
|
||||
CreatedAt time.Time "json:\"createdAt\""
|
||||
Status string "json:\"status\""
|
||||
TransferValue json.Number "json:\"transferValue\""
|
||||
TxHash string "json:\"txHash\""
|
||||
TxType string "json:\"txType\""
|
||||
}
|
||||
}{
|
||||
Transaction: models.Transaction{
|
||||
CreatedAt: time.Now(),
|
||||
Status: "SUCCESS",
|
||||
TransferValue: json.Number("0.5"),
|
||||
TxHash: "0x123abc456def",
|
||||
TxType: "transfer",
|
||||
},
|
||||
},
|
||||
}
|
||||
return trackResponse, nil
|
||||
func (tas *TestAccountService) CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResult, error) {
|
||||
return &models.TrackStatusResult {
|
||||
CreatedAt: time.Now(),
|
||||
Status: "SUCCESS",
|
||||
TransferValue: json.Number("0.5"),
|
||||
TxHash: "0x123abc456def",
|
||||
TxType: "transfer",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (tas *TestAccountService) TrackAccountStatus(ctx context.Context, publicKey string) (*api.OKResponse, error) {
|
||||
@ -73,18 +48,13 @@ func (tas *TestAccountService) TrackAccountStatus(ctx context.Context, publicKey
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (tas *TestAccountService) FetchVouchers(ctx context.Context, publicKey string) (*models.VoucherHoldingResponse, error) {
|
||||
return &models.VoucherHoldingResponse{
|
||||
Ok: true,
|
||||
Result: models.VoucherResult{
|
||||
Holdings: []dataserviceapi.TokenHoldings{
|
||||
{
|
||||
ContractAddress: "0x6CC75A06ac72eB4Db2eE22F781F5D100d8ec03ee",
|
||||
TokenSymbol: "SRF",
|
||||
TokenDecimals: "6",
|
||||
Balance: "2745987",
|
||||
},
|
||||
},
|
||||
func (tas *TestAccountService) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
||||
return []dataserviceapi.TokenHoldings {
|
||||
dataserviceapi.TokenHoldings {
|
||||
ContractAddress: "0x6CC75A06ac72eB4Db2eE22F781F5D100d8ec03ee",
|
||||
TokenSymbol: "SRF",
|
||||
TokenDecimals: "6",
|
||||
Balance: "2745987",
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
@ -4,27 +4,29 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"net/url"
|
||||
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||
"git.grassecon.net/urdt/ussd/config"
|
||||
"git.grassecon.net/urdt/ussd/internal/models"
|
||||
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||
)
|
||||
|
||||
var (
|
||||
okResponse api.OKResponse
|
||||
errResponse api.ErrResponse
|
||||
)
|
||||
|
||||
type AccountServiceInterface interface {
|
||||
CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResponse, error)
|
||||
CreateAccount(ctx context.Context) (*api.OKResponse, error)
|
||||
CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResponse, error)
|
||||
//CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResponse, error)
|
||||
CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error)
|
||||
CreateAccount(ctx context.Context) (*models.AccountResult, error)
|
||||
//CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResponse, error)
|
||||
// TODO: poorly named method seems to be checking transaction
|
||||
CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResult, error)
|
||||
TrackAccountStatus(ctx context.Context, publicKey string) (*api.OKResponse, error)
|
||||
FetchVouchers(ctx context.Context, publicKey string) (*models.VoucherHoldingResponse, error)
|
||||
FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error)
|
||||
//FetchVouchers(ctx context.Context, publicKey string) (*api.OKResponse, error)
|
||||
}
|
||||
|
||||
type AccountService struct {
|
||||
@ -39,32 +41,38 @@ type AccountService struct {
|
||||
// - string: The status of the transaction as a string. If there is an error during the request or processing, this will be an empty string.
|
||||
// - error: An error if any occurred during the HTTP request, reading the response, or unmarshalling the JSON data.
|
||||
// If no error occurs, this will be nil
|
||||
func (as *AccountService) CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResponse, error) {
|
||||
resp, err := http.Get(config.BalanceURL + trackingId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
func (as *AccountService) CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResult, error) {
|
||||
var r models.TrackStatusResult
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
ep, err := url.JoinPath(config.TrackURL, trackingId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var trackResp models.TrackStatusResponse
|
||||
err = json.Unmarshal(body, &trackResp)
|
||||
req, err := http.NewRequest("GET", ep, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &trackResp, nil
|
||||
|
||||
_, err = doCustodialRequest(ctx, req, &r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (as *AccountService) TrackAccountStatus(ctx context.Context, publicKey string) (*api.OKResponse, error) {
|
||||
var okResponse api.OKResponse
|
||||
var errResponse api.ErrResponse
|
||||
var err error
|
||||
// Construct the URL with the path parameter
|
||||
url := fmt.Sprintf("%s/%s", config.TrackURL, publicKey)
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
|
||||
ep, err := url.JoinPath(config.TrackURL, publicKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", ep, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -104,41 +112,83 @@ func (as *AccountService) TrackAccountStatus(ctx context.Context, publicKey stri
|
||||
// CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint.
|
||||
// Parameters:
|
||||
// - publicKey: The public key associated with the account whose balance needs to be checked.
|
||||
func (as *AccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResponse, error) {
|
||||
resp, err := http.Get(config.BalanceURL + publicKey)
|
||||
func (as *AccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error) {
|
||||
var balanceResult models.BalanceResult
|
||||
|
||||
ep, err := url.JoinPath(config.BalanceURL, publicKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
|
||||
req, err := http.NewRequest("GET", ep, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var balanceResp models.BalanceResponse
|
||||
err = json.Unmarshal(body, &balanceResp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &balanceResp, nil
|
||||
|
||||
_, err = doCustodialRequest(ctx, req, &balanceResult)
|
||||
return &balanceResult, err
|
||||
}
|
||||
|
||||
|
||||
// CreateAccount creates a new account in the custodial system.
|
||||
// Returns:
|
||||
// - *models.AccountResponse: A pointer to an AccountResponse struct containing the details of the created account.
|
||||
// If there is an error during the request or processing, this will be nil.
|
||||
// - error: An error if any occurred during the HTTP request, reading the response, or unmarshalling the JSON data.
|
||||
// If no error occurs, this will be nil.
|
||||
func (as *AccountService) CreateAccount(ctx context.Context) (*api.OKResponse, error) {
|
||||
var err error
|
||||
|
||||
func (as *AccountService) CreateAccount(ctx context.Context) (*models.AccountResult, error) {
|
||||
var r models.AccountResult
|
||||
// Create a new request
|
||||
req, err := http.NewRequest("POST", config.CreateAccountURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-GE-KEY", "xd")
|
||||
|
||||
_, err = doCustodialRequest(ctx, req, &r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
// FetchVouchers retrieves the token holdings for a given public key from the custodial holdings API endpoint
|
||||
// Parameters:
|
||||
// - publicKey: The public key associated with the account.
|
||||
func (as *AccountService) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
||||
var r []dataserviceapi.TokenHoldings
|
||||
|
||||
req, err := http.NewRequest("GET", config.VoucherHoldingsURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = doDataRequest(ctx, req, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return r, nil
|
||||
|
||||
//
|
||||
// file, err := os.Open("sample_tokens.json")
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// defer file.Close()
|
||||
// var holdings models.VoucherHoldingResponse
|
||||
//
|
||||
// if err := json.NewDecoder(file).Decode(&holdings); err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// return &holdings, nil
|
||||
}
|
||||
|
||||
func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
||||
var okResponse api.OKResponse
|
||||
var errResponse api.ErrResponse
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
errResponse.Description = err.Error()
|
||||
@ -165,21 +215,22 @@ func (as *AccountService) CreateAccount(ctx context.Context) (*api.OKResponse, e
|
||||
return nil, errors.New("Empty api result")
|
||||
}
|
||||
return &okResponse, nil
|
||||
}
|
||||
|
||||
// FetchVouchers retrieves the token holdings for a given public key from the custodial holdings API endpoint
|
||||
// Parameters:
|
||||
// - publicKey: The public key associated with the account.
|
||||
func (as *AccountService) FetchVouchers(ctx context.Context, publicKey string) (*models.VoucherHoldingResponse, error) {
|
||||
file, err := os.Open("sample_tokens.json")
|
||||
v, err := json.Marshal(okResponse.Result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
var holdings models.VoucherHoldingResponse
|
||||
|
||||
if err := json.NewDecoder(file).Decode(&holdings); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &holdings, nil
|
||||
err = json.Unmarshal(v, &rcpt)
|
||||
return &okResponse, err
|
||||
}
|
||||
|
||||
func doCustodialRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
||||
req.Header.Set("X-GE-KEY", config.CustodialAPIKey)
|
||||
return doRequest(ctx, req, rcpt)
|
||||
}
|
||||
|
||||
func doDataRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
||||
req.Header.Set("X-GE-KEY", config.DataAPIKey)
|
||||
return doRequest(ctx, req, rcpt)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user