api-context #127

Merged
lash merged 7 commits from api-context into master 2024-10-24 16:45:37 +02:00
4 changed files with 27 additions and 22 deletions

2
go.mod
View File

@ -44,3 +44,5 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )

View File

@ -1,6 +1,7 @@
package server package server
import ( import (
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -19,10 +20,10 @@ var (
) )
type AccountServiceInterface interface { type AccountServiceInterface interface {
CheckBalance(publicKey string) (*models.BalanceResponse, error) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResponse, error)
CreateAccount() (*api.OKResponse, error) CreateAccount(ctx context.Context) (*api.OKResponse, error)
CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error) CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResponse, error)
TrackAccountStatus(publicKey string) (*api.OKResponse, error) TrackAccountStatus(ctx context.Context, publicKey string) (*api.OKResponse, error)
} }
type AccountService struct { type AccountService struct {
@ -40,7 +41,7 @@ type TestAccountService 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. // - 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. // - 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 // If no error occurs, this will be nil
func (as *AccountService) CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error) { func (as *AccountService) CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResponse, error) {
resp, err := http.Get(config.BalanceURL + trackingId) resp, err := http.Get(config.BalanceURL + trackingId)
if err != nil { if err != nil {
return nil, err return nil, err
@ -61,7 +62,7 @@ func (as *AccountService) CheckAccountStatus(trackingId string) (*models.TrackSt
} }
func (as *AccountService) TrackAccountStatus(publicKey string) (*api.OKResponse, error) { func (as *AccountService) TrackAccountStatus(ctx context.Context, publicKey string) (*api.OKResponse, error) {
var err error var err error
// Construct the URL with the path parameter // Construct the URL with the path parameter
url := fmt.Sprintf("%s/%s", config.TrackURL, publicKey) url := fmt.Sprintf("%s/%s", config.TrackURL, publicKey)
@ -105,7 +106,7 @@ func (as *AccountService) TrackAccountStatus(publicKey string) (*api.OKResponse,
// CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint. // CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint.
// Parameters: // Parameters:
// - publicKey: The public key associated with the account whose balance needs to be checked. // - publicKey: The public key associated with the account whose balance needs to be checked.
func (as *AccountService) CheckBalance(publicKey string) (*models.BalanceResponse, error) { func (as *AccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResponse, error) {
resp, err := http.Get(config.BalanceURL + publicKey) resp, err := http.Get(config.BalanceURL + publicKey)
if err != nil { if err != nil {
return nil, err return nil, err
@ -129,7 +130,7 @@ func (as *AccountService) CheckBalance(publicKey string) (*models.BalanceRespons
// If there is an error during the request or processing, this will be nil. // 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. // - 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. // If no error occurs, this will be nil.
func (as *AccountService) CreateAccount() (*api.OKResponse, error) { func (as *AccountService) CreateAccount(ctx context.Context) (*api.OKResponse, error) {
var err error var err error
// Create a new request // Create a new request
@ -168,7 +169,7 @@ func (as *AccountService) CreateAccount() (*api.OKResponse, error) {
return &okResponse, nil return &okResponse, nil
} }
func (tas *TestAccountService) CreateAccount() (*api.OKResponse, error) { func (tas *TestAccountService) CreateAccount(ctx context.Context) (*api.OKResponse, error) {
return &api.OKResponse{ return &api.OKResponse{
Ok: true, Ok: true,
Description: "Account creation request received successfully", Description: "Account creation request received successfully",
@ -177,7 +178,7 @@ func (tas *TestAccountService) CreateAccount() (*api.OKResponse, error) {
} }
func (tas *TestAccountService) CheckBalance(publicKey string) (*models.BalanceResponse, error) { func (tas *TestAccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResponse, error) {
balanceResponse := &models.BalanceResponse{ balanceResponse := &models.BalanceResponse{
Ok: true, Ok: true,
Result: struct { Result: struct {
@ -191,7 +192,7 @@ func (tas *TestAccountService) CheckBalance(publicKey string) (*models.BalanceRe
return balanceResponse, nil return balanceResponse, nil
} }
func (tas *TestAccountService) TrackAccountStatus(publicKey string) (*api.OKResponse, error) { func (tas *TestAccountService) TrackAccountStatus(ctx context.Context, publicKey string) (*api.OKResponse, error) {
return &api.OKResponse{ return &api.OKResponse{
Ok: true, Ok: true,
Description: "Account creation succeeded", Description: "Account creation succeeded",
@ -201,7 +202,7 @@ func (tas *TestAccountService) TrackAccountStatus(publicKey string) (*api.OKResp
}, nil }, nil
} }
func (tas *TestAccountService) CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error) { func (tas *TestAccountService) CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResponse, error) {
trackResponse := &models.TrackStatusResponse{ trackResponse := &models.TrackStatusResponse{
Ok: true, Ok: true,
Result: struct { Result: struct {

View File

@ -140,7 +140,7 @@ 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 { func (h *Handlers) createAccountNoExist(ctx context.Context, sessionId string, res *resource.Result) error {
flag_account_created, _ := h.flagManager.GetFlag("flag_account_created") flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
okResponse, err := h.accountService.CreateAccount() okResponse, err := h.accountService.CreateAccount(ctx)
if err != nil { if err != nil {
return err return err
} }
@ -544,7 +544,7 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b
if err != nil { if err != nil {
return res, err return res, err
} }
okResponse, err = h.accountService.TrackAccountStatus(string(publicKey)) okResponse, err = h.accountService.TrackAccountStatus(ctx, string(publicKey))
if err != nil { if err != nil {
res.FlagSet = append(res.FlagSet, flag_api_error) res.FlagSet = append(res.FlagSet, flag_api_error)
return res, err return res, err
@ -651,7 +651,7 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) (
return res, err return res, err
} }
balanceResponse, err := h.accountService.CheckBalance(string(publicKey)) balanceResponse, err := h.accountService.CheckBalance(ctx, string(publicKey))
if err != nil { if err != nil {
return res, nil return res, nil
} }
@ -684,7 +684,7 @@ func (h *Handlers) FetchCustodialBalances(ctx context.Context, sym string, input
return res, err return res, err
} }
balanceResponse, err := h.accountService.CheckBalance(string(publicKey)) balanceResponse, err := h.accountService.CheckBalance(ctx, string(publicKey))
if err != nil { if err != nil {
return res, nil return res, nil
} }
@ -802,7 +802,7 @@ func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (res
store := h.userdataStore store := h.userdataStore
publicKey, _ := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY) publicKey, _ := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
balanceResp, err := h.accountService.CheckBalance(string(publicKey)) balanceResp, err := h.accountService.CheckBalance(ctx, string(publicKey))
if err != nil { if err != nil {
return res, nil return res, nil
} }
@ -832,7 +832,7 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte)
amountStr := string(input) amountStr := string(input)
balanceRes, err := h.accountService.CheckBalance(string(publicKey)) balanceRes, err := h.accountService.CheckBalance(ctx, string(publicKey))
balanceStr := balanceRes.Result.Balance balanceStr := balanceRes.Result.Balance
if !balanceRes.Ok { if !balanceRes.Ok {

View File

@ -1,6 +1,8 @@
package mocks package mocks
import ( import (
"context"
"git.grassecon.net/urdt/ussd/internal/models" "git.grassecon.net/urdt/ussd/internal/models"
"github.com/grassrootseconomics/eth-custodial/pkg/api" "github.com/grassrootseconomics/eth-custodial/pkg/api"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
@ -11,22 +13,22 @@ type MockAccountService struct {
mock.Mock mock.Mock
} }
func (m *MockAccountService) CreateAccount() (*api.OKResponse, error) { func (m *MockAccountService) CreateAccount(ctx context.Context) (*api.OKResponse, error) {
args := m.Called() args := m.Called()
return args.Get(0).(*api.OKResponse), args.Error(1) return args.Get(0).(*api.OKResponse), args.Error(1)
} }
func (m *MockAccountService) CheckBalance(publicKey string) (*models.BalanceResponse, error) { func (m *MockAccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResponse, error) {
args := m.Called(publicKey) args := m.Called(publicKey)
return args.Get(0).(*models.BalanceResponse), args.Error(1) return args.Get(0).(*models.BalanceResponse), args.Error(1)
} }
func (m *MockAccountService) CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error) { func (m *MockAccountService) CheckAccountStatus(ctx context.Context, trackingId string) (*models.TrackStatusResponse, error) {
args := m.Called(trackingId) args := m.Called(trackingId)
return args.Get(0).(*models.TrackStatusResponse), args.Error(1) return args.Get(0).(*models.TrackStatusResponse), args.Error(1)
} }
func (m *MockAccountService) TrackAccountStatus(publicKey string) (*api.OKResponse, error) { func (m *MockAccountService) TrackAccountStatus(ctx context.Context,publicKey string) (*api.OKResponse, error) {
args := m.Called(publicKey) args := m.Called(publicKey)
return args.Get(0).(*api.OKResponse), args.Error(1) return args.Get(0).(*api.OKResponse), args.Error(1)
} }