api-structs #117

Merged
lash merged 35 commits from api-structs into master 2024-10-24 15:53:46 +02:00
2 changed files with 25 additions and 18 deletions
Showing only changes of commit 5b0a383513 - Show all commits

View File

@ -9,18 +9,19 @@ import (
"git.grassecon.net/urdt/ussd/config"
"git.grassecon.net/urdt/ussd/internal/models"
"github.com/grassrootseconomics/eth-custodial/pkg/api"
)
var apiResponse struct {
type ApiResponse struct {
Outdated
Review

why is this needed?

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.

if apiResponse.Ok {
		err = json.Unmarshal([]byte(body), &okResponse)
		if err != nil {
			errResponse.Description = err.Error()
			return nil, &errResponse
		}
		return &okResponse, nil
	} else {
		err := json.Unmarshal([]byte(body), &errResponse)
		if err != nil {
			errResponse.Description = err.Error()
			return nil, &errResponse
		}
		return nil, &errResponse
	}
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. ``` if apiResponse.Ok { err = json.Unmarshal([]byte(body), &okResponse) if err != nil { errResponse.Description = err.Error() return nil, &errResponse } return &okResponse, nil } else { err := json.Unmarshal([]byte(body), &errResponse) if err != nil { errResponse.Description = err.Error() return nil, &errResponse } return nil, &errResponse } ```
Ok bool `json:"ok"`
Description string `json:"description"`
}
type AccountServiceInterface interface {
CheckBalance(publicKey string) (*models.BalanceResponse, error)
CreateAccount() (*OKResponse, *ErrResponse)
CreateAccount() (*api.OKResponse, *api.ErrResponse)
CheckAccountStatus(trackingId string) (*models.TrackStatusResponse, error)
TrackAccountStatus(publicKey string) (*OKResponse, *ErrResponse)
TrackAccountStatus(publicKey string) (*api.OKResponse, *api.ErrResponse)
}
type AccountService struct {
@ -59,9 +60,9 @@ func (as *AccountService) CheckAccountStatus(trackingId string) (*models.TrackSt
}
func (as *AccountService) TrackAccountStatus(publicKey string) (*OKResponse, *ErrResponse) {
var errResponse ErrResponse
var okResponse OKResponse
func (as *AccountService) TrackAccountStatus(publicKey string) (*api.OKResponse, *api.ErrResponse) {
var errResponse api.ErrResponse
var okResponse api.OKResponse
var err error
// Construct the URL with the path parameter
url := fmt.Sprintf("%s/%s", config.TrackURL, publicKey)
lash marked this conversation as resolved
Review

I think it is better and safer to use the net/url package for handling urls?

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.
Review
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
var apiResponse ApiResponse
err = json.Unmarshal([]byte(body), &apiResponse)
if err != nil {
Outdated
Review

I would just use the literal 0 here

I would just use the literal `0` here

If there is no matching tracking id, result.otx will be null.

If there is no matching tracking id, result.otx will be null.
Outdated
Review

@kamikazechaser please elaborate is there a missing case?

@kamikazechaser please elaborate is there a missing case?
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.
// - 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() (*OKResponse, *ErrResponse) {
func (as *AccountService) CreateAccount() (*api.OKResponse, *api.ErrResponse) {
var errResponse ErrResponse
var okResponse OKResponse
var errResponse api.ErrResponse
var okResponse api.OKResponse
var err error
// Create a new request
@ -166,6 +168,7 @@ func (as *AccountService) CreateAccount() (*OKResponse, *ErrResponse) {
errResponse.Description = err.Error()
return nil, &errResponse
}
var apiResponse ApiResponse
err = json.Unmarshal([]byte(body), &apiResponse)
if err != nil {
return nil, &errResponse
@ -187,8 +190,8 @@ func (as *AccountService) CreateAccount() (*OKResponse, *ErrResponse) {
}
}
func (tas *TestAccountService) CreateAccount() (*OKResponse, *ErrResponse) {
return &OKResponse{
func (tas *TestAccountService) CreateAccount() (*api.OKResponse, *api.ErrResponse) {
return &api.OKResponse{
Ok: true,
Description: "Account creation request received successfully",
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
}
func (tas *TestAccountService) TrackAccountStatus(publicKey string) (*OKResponse, *ErrResponse) {
return &OKResponse{
func (tas *TestAccountService) TrackAccountStatus(publicKey string) (*api.OKResponse, *api.ErrResponse) {
return &api.OKResponse{
Ok: true,
Description: "Account creation succeeded",
Result: map[string]any{

View File

@ -20,6 +20,7 @@ import (
"git.grassecon.net/urdt/ussd/internal/models"
"git.grassecon.net/urdt/ussd/internal/utils"
"github.com/alecthomas/assert/v2"
"github.com/grassrootseconomics/eth-custodial/pkg/api"
testdataloader "github.com/peteole/testdata-loader"
"github.com/stretchr/testify/require"
)
@ -74,6 +75,8 @@ func TestCreateAccount(t *testing.T) {
}
// Create required mocks
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 {
t.Logf(err.Error())
}
@ -85,12 +88,12 @@ func TestCreateAccount(t *testing.T) {
tests := []struct {
name string
serverResponse *server.OKResponse
serverResponse *api.OKResponse
expectedResult resource.Result
}{
{
name: "Test account creation success",
serverResponse: &server.OKResponse{
serverResponse: &api.OKResponse{
Ok: true,
Description: "Account creation successed",
Result: map[string]any{
@ -100,6 +103,7 @@ func TestCreateAccount(t *testing.T) {
},
expectedResult: resource.Result{
FlagSet: []uint32{flag_account_created},
FlagReset: []uint32{flag_api_call_error},
},
},
}
@ -1073,14 +1077,14 @@ func TestCheckAccountStatus(t *testing.T) {
tests := []struct {
name string
input []byte
serverResponse *server.OKResponse
serverResponse *api.OKResponse
response *models.TrackStatusResponse
expectedResult resource.Result
}{
{
name: "Test when account is on the Sarafu network",
input: []byte("TrackingId1234"),
serverResponse: &server.OKResponse{
serverResponse: &api.OKResponse{
Ok: true,
Description: "Account creation succeeded",
Result: map[string]any{
@ -1135,7 +1139,7 @@ func TestCheckAccountStatus(t *testing.T) {
},
},
},
serverResponse: &server.OKResponse{
serverResponse: &api.OKResponse{
Ok: true,
Description: "Account creation succeeded",
Result: map[string]any{