api-structs #117
@ -9,18 +9,19 @@ import (
|
|||||||
|
|
||||||
"git.grassecon.net/urdt/ussd/config"
|
"git.grassecon.net/urdt/ussd/config"
|
||||||
"git.grassecon.net/urdt/ussd/internal/models"
|
"git.grassecon.net/urdt/ussd/internal/models"
|
||||||
|
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
var apiResponse struct {
|
type ApiResponse struct {
|
||||||
|
|||||||
Ok bool `json:"ok"`
|
Ok bool `json:"ok"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AccountServiceInterface interface {
|
type AccountServiceInterface interface {
|
||||||
CheckBalance(publicKey string) (*models.BalanceResponse, error)
|
CheckBalance(publicKey string) (*models.BalanceResponse, error)
|
||||||
CreateAccount() (*OKResponse, *ErrResponse)
|
CreateAccount() (*api.OKResponse, *api.ErrResponse)
|
||||||
CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error)
|
CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error)
|
||||||
TrackAccountStatus(publicKey string) (*OKResponse, *ErrResponse)
|
TrackAccountStatus(publicKey string) (*api.OKResponse, *api.ErrResponse)
|
||||||
}
|
}
|
||||||
|
|
||||||
type AccountService struct {
|
type AccountService struct {
|
||||||
@ -59,9 +60,9 @@ func (as *AccountService) CheckAccountStatus(trackingId string) (*models.TrackSt
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *AccountService) TrackAccountStatus(publicKey string) (*OKResponse, *ErrResponse) {
|
func (as *AccountService) TrackAccountStatus(publicKey string) (*api.OKResponse, *api.ErrResponse) {
|
||||||
var errResponse ErrResponse
|
var errResponse api.ErrResponse
|
||||||
var okResponse OKResponse
|
var okResponse api.OKResponse
|
||||||
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)
|
||||||
lash marked this conversation as resolved
lash
commented
I think it is better and safer to use the I will add it as a separate issue. I think it is better and safer to use the `net/url` package for handling urls?
I will add it as a separate issue.
lash
commented
https://git.grassecon.net/urdt/ussd/issues/125
|
|||||||
@ -90,6 +91,7 @@ func (as *AccountService) TrackAccountStatus(publicKey string) (*OKResponse, *Er
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Step 2: Unmarshal into the generic struct
|
// Step 2: Unmarshal into the generic struct
|
||||||
|
var apiResponse ApiResponse
|
||||||
err = json.Unmarshal([]byte(body), &apiResponse)
|
err = json.Unmarshal([]byte(body), &apiResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lash
commented
I would just use the literal I would just use the literal `0` here
kamikazechaser
commented
If there is no matching tracking id, result.otx will be null. If there is no matching tracking id, result.otx will be null.
lash
commented
@kamikazechaser please elaborate is there a missing case? @kamikazechaser please elaborate is there a missing case?
|
|||||||
errResponse.Description = err.Error()
|
errResponse.Description = err.Error()
|
||||||
@ -139,10 +141,10 @@ 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() (*OKResponse, *ErrResponse) {
|
func (as *AccountService) CreateAccount() (*api.OKResponse, *api.ErrResponse) {
|
||||||
|
|
||||||
var errResponse ErrResponse
|
var errResponse api.ErrResponse
|
||||||
var okResponse OKResponse
|
var okResponse api.OKResponse
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// Create a new request
|
// Create a new request
|
||||||
@ -166,6 +168,7 @@ func (as *AccountService) CreateAccount() (*OKResponse, *ErrResponse) {
|
|||||||
errResponse.Description = err.Error()
|
errResponse.Description = err.Error()
|
||||||
return nil, &errResponse
|
return nil, &errResponse
|
||||||
}
|
}
|
||||||
|
var apiResponse ApiResponse
|
||||||
err = json.Unmarshal([]byte(body), &apiResponse)
|
err = json.Unmarshal([]byte(body), &apiResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, &errResponse
|
return nil, &errResponse
|
||||||
@ -187,8 +190,8 @@ func (as *AccountService) CreateAccount() (*OKResponse, *ErrResponse) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tas *TestAccountService) CreateAccount() (*OKResponse, *ErrResponse) {
|
func (tas *TestAccountService) CreateAccount() (*api.OKResponse, *api.ErrResponse) {
|
||||||
return &OKResponse{
|
return &api.OKResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Description: "Account creation request received successfully",
|
Description: "Account creation request received successfully",
|
||||||
Result: map[string]any{"publicKey": "0x48ADca309b5085852207FAaf2816eD72B52F527C", "trackingId": "28ebe84d-b925-472c-87ae-bbdfa1fb97be"},
|
Result: map[string]any{"publicKey": "0x48ADca309b5085852207FAaf2816eD72B52F527C", "trackingId": "28ebe84d-b925-472c-87ae-bbdfa1fb97be"},
|
||||||
@ -212,8 +215,8 @@ func (tas *TestAccountService) CheckBalance(publicKey string) (*models.BalanceRe
|
|||||||
return balanceResponse, nil
|
return balanceResponse, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tas *TestAccountService) TrackAccountStatus(publicKey string) (*OKResponse, *ErrResponse) {
|
func (tas *TestAccountService) TrackAccountStatus(publicKey string) (*api.OKResponse, *api.ErrResponse) {
|
||||||
return &OKResponse{
|
return &api.OKResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Description: "Account creation succeeded",
|
Description: "Account creation succeeded",
|
||||||
Result: map[string]any{
|
Result: map[string]any{
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
"git.grassecon.net/urdt/ussd/internal/models"
|
"git.grassecon.net/urdt/ussd/internal/models"
|
||||||
"git.grassecon.net/urdt/ussd/internal/utils"
|
"git.grassecon.net/urdt/ussd/internal/utils"
|
||||||
"github.com/alecthomas/assert/v2"
|
"github.com/alecthomas/assert/v2"
|
||||||
|
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||||
testdataloader "github.com/peteole/testdata-loader"
|
testdataloader "github.com/peteole/testdata-loader"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@ -74,6 +75,8 @@ func TestCreateAccount(t *testing.T) {
|
|||||||
}
|
}
|
||||||
// Create required mocks
|
// Create required mocks
|
||||||
flag_account_created, err := fm.GetFlag("flag_account_created")
|
flag_account_created, err := fm.GetFlag("flag_account_created")
|
||||||
|
//flag_api_call_error, err := fm.GetFlag("flag_api_call_error,")
|
||||||
|
flag_api_call_error, _ := fm.GetFlag("flag_api_call_error")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Logf(err.Error())
|
t.Logf(err.Error())
|
||||||
}
|
}
|
||||||
@ -85,12 +88,12 @@ func TestCreateAccount(t *testing.T) {
|
|||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
serverResponse *server.OKResponse
|
serverResponse *api.OKResponse
|
||||||
expectedResult resource.Result
|
expectedResult resource.Result
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Test account creation success",
|
name: "Test account creation success",
|
||||||
serverResponse: &server.OKResponse{
|
serverResponse: &api.OKResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Description: "Account creation successed",
|
Description: "Account creation successed",
|
||||||
Result: map[string]any{
|
Result: map[string]any{
|
||||||
@ -100,6 +103,7 @@ func TestCreateAccount(t *testing.T) {
|
|||||||
},
|
},
|
||||||
expectedResult: resource.Result{
|
expectedResult: resource.Result{
|
||||||
FlagSet: []uint32{flag_account_created},
|
FlagSet: []uint32{flag_account_created},
|
||||||
|
FlagReset: []uint32{flag_api_call_error},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -1073,14 +1077,14 @@ func TestCheckAccountStatus(t *testing.T) {
|
|||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
input []byte
|
input []byte
|
||||||
serverResponse *server.OKResponse
|
serverResponse *api.OKResponse
|
||||||
response *models.TrackStatusResponse
|
response *models.TrackStatusResponse
|
||||||
expectedResult resource.Result
|
expectedResult resource.Result
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Test when account is on the Sarafu network",
|
name: "Test when account is on the Sarafu network",
|
||||||
input: []byte("TrackingId1234"),
|
input: []byte("TrackingId1234"),
|
||||||
serverResponse: &server.OKResponse{
|
serverResponse: &api.OKResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Description: "Account creation succeeded",
|
Description: "Account creation succeeded",
|
||||||
Result: map[string]any{
|
Result: map[string]any{
|
||||||
@ -1135,7 +1139,7 @@ func TestCheckAccountStatus(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
serverResponse: &server.OKResponse{
|
serverResponse: &api.OKResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Description: "Account creation succeeded",
|
Description: "Account creation succeeded",
|
||||||
Result: map[string]any{
|
Result: map[string]any{
|
||||||
|
Loading…
Reference in New Issue
Block a user
why is this needed?
Well,at the point of making the api call and receiving a response back, we don't really know the response that will be sent back,it can either be an ErrResponse or an OKResponse but we are certain that the ok and description field will be present.So the OK field in the ApiResponse will be used to decide which type to Unmarshal on.