api-context #127

Merged
lash merged 7 commits from api-context into master 2024-10-24 16:45:37 +02:00
3 changed files with 21 additions and 18 deletions
Showing only changes of commit b41e52af63 - Show all commits

View File

@ -1,6 +1,7 @@
package server
import (
"context"
"encoding/json"
"io"
"net/http"
@ -11,9 +12,9 @@ import (
)
type AccountServiceInterface interface {
CheckBalance(publicKey string) (*models.BalanceResponse, error)
CreateAccount() (*models.AccountResponse, error)
CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error)
CheckBalance(publicKey string, ctx context.Context) (*models.BalanceResponse, error)
CreateAccount(ctx context.Context) (*models.AccountResponse, error)
CheckAccountStatus(trackingId string, ctx context.Context) (*models.TrackStatusResponse, error)
}
type AccountService struct {
@ -33,7 +34,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.
// - 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(trackingId string) (*models.TrackStatusResponse, error) {
func (as *AccountService) CheckAccountStatus(trackingId string, ctx context.Context) (*models.TrackStatusResponse, error) {
Outdated
Review

I believe context should per convention always be the first argument.

https://tip.golang.org/blog/context-and-structs#conclusion

I believe context should per convention always be the first argument. https://tip.golang.org/blog/context-and-structs#conclusion
resp, err := http.Get(config.TrackStatusURL + trackingId)
if err != nil {
return nil, err
@ -55,7 +56,7 @@ func (as *AccountService) CheckAccountStatus(trackingId string) (*models.TrackSt
// 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(publicKey string) (*models.BalanceResponse, error) {
func (as *AccountService) CheckBalance(publicKey string, ctx context.Context) (*models.BalanceResponse, error) {
resp, err := http.Get(config.BalanceURL + publicKey)
if err != nil {
return nil, err
@ -79,7 +80,7 @@ func (as *AccountService) CheckBalance(publicKey string) (*models.BalanceRespons
// 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() (*models.AccountResponse, error) {
func (as *AccountService) CreateAccount(ctx context.Context) (*models.AccountResponse, error) {
resp, err := http.Post(config.CreateAccountURL, "application/json", nil)
if err != nil {
return nil, err
@ -97,7 +98,7 @@ func (as *AccountService) CreateAccount() (*models.AccountResponse, error) {
return &accountResp, nil
}
func (tas *TestAccountService) CreateAccount() (*models.AccountResponse, error) {
func (tas *TestAccountService) CreateAccount(ctx context.Context) (*models.AccountResponse, error) {
return &models.AccountResponse{
Ok: true,
Result: struct {
@ -112,7 +113,7 @@ func (tas *TestAccountService) CreateAccount() (*models.AccountResponse, error)
}, nil
}
func (tas *TestAccountService) CheckBalance(publicKey string) (*models.BalanceResponse, error) {
func (tas *TestAccountService) CheckBalance(publicKey string, ctx context.Context) (*models.BalanceResponse, error) {
balanceResponse := &models.BalanceResponse{
Ok: true,
@ -128,7 +129,7 @@ func (tas *TestAccountService) CheckBalance(publicKey string) (*models.BalanceRe
return balanceResponse, nil
}
func (tas *TestAccountService) CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error) {
func (tas *TestAccountService) CheckAccountStatus(trackingId string, ctx context.Context) (*models.TrackStatusResponse, error) {
trackResponse := &models.TrackStatusResponse{
Ok: true,
Result: struct {

View File

@ -136,7 +136,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 {
accountResp, err := h.accountService.CreateAccount()
accountResp, err := h.accountService.CreateAccount(ctx)
data := map[utils.DataTyp]string{
utils.DATA_TRACKING_ID: accountResp.Result.TrackingId,
utils.DATA_PUBLIC_KEY: accountResp.Result.PublicKey,
@ -547,7 +547,7 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b
return res, err
}
accountStatus, err := h.accountService.CheckAccountStatus(string(trackingId))
accountStatus, err := h.accountService.CheckAccountStatus(string(trackingId),ctx)
if err != nil {
fmt.Println("Error checking account status:", err)
return res, err
@ -656,7 +656,7 @@ func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) (
return res, err
}
balanceResponse, err := h.accountService.CheckBalance(string(publicKey))
balanceResponse, err := h.accountService.CheckBalance(string(publicKey),ctx)
if err != nil {
return res, nil
}
@ -688,7 +688,7 @@ func (h *Handlers) FetchCustodialBalances(ctx context.Context, sym string, input
return res, err
}
balanceResponse, err := h.accountService.CheckBalance(string(publicKey))
balanceResponse, err := h.accountService.CheckBalance(string(publicKey),ctx)
if err != nil {
return res, nil
}
@ -806,7 +806,7 @@ func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (res
store := h.userdataStore
publicKey, _ := store.ReadEntry(ctx, sessionId, utils.DATA_PUBLIC_KEY)
balanceResp, err := h.accountService.CheckBalance(string(publicKey))
balanceResp, err := h.accountService.CheckBalance(string(publicKey),ctx)
if err != nil {
return res, nil
}
@ -836,7 +836,7 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte)
amountStr := string(input)
balanceRes, err := h.accountService.CheckBalance(string(publicKey))
balanceRes, err := h.accountService.CheckBalance(string(publicKey),ctx)
balanceStr := balanceRes.Result.Balance
if !balanceRes.Ok {

View File

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