i cant seem to hit this endpoint /check-wallet-balance 'here is my route file"package routes
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/gin-gonic/gin"
)
const (
APIURL string = "https://payments.relworx.com/api/mobile-money"
ContentType string = "application/json"
AcceptType string = "application/vnd.relworx.v2"
)
type WalletBalanceResponse struct {
Success bool json:"success"
Balance string json:"balance"
}
// CheckBalanceRoute defines the route for checking wallet balance
func CheckBalanceRoute(r *gin.Engine) {
r.GET("/check-wallet-balance", CheckWalletBalanceHandler)
}
// CheckWalletBalance retrieves the wallet balance from the external API
func CheckWalletBalance(accountNo, currency, apiToken string) (*WalletBalanceResponse, error) {
url := fmt.Sprintf("%s/check-wallet-balance?account_no=%s¤cy=%s", APIURL, accountNo, currency)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err) // Use %w to unwrap error
}
req.Header.Set("Accept", AcceptType)
req.Header.Set("Content-Type", ContentType)
req.Header.Set("Authorization", "Bearer "+apiToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error making request: %w", err) // Use %w to unwrap error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %w", err) // Use %w to unwrap error
}
var balanceResp WalletBalanceResponse
err = json.Unmarshal(body, &balanceResp)
if err != nil {
return nil, fmt.Errorf("error decoding JSON response: %w", err) // Use %w to unwrap error
}
return &balanceResp, nil
}
// CheckWalletBalanceHandler handles requests to the check-wallet-balance endpoint
func CheckWalletBalanceHandler(c *gin.Context) {
apiToken := os.Getenv("API_TOKEN")
if apiToken == "" {
c.JSON(http.StatusInternalServerError, gin.H{"error": "API_TOKEN environment variable not set"})
return
}
accountNo := c.Query("account_no")
if accountNo == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Missing account_no query parameter"})
return
}
currency := c.Query("currency")
if currency == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Missing currency query parameter"})
return
}
// Call CheckWalletBalance and handle potential errors
balanceResp, err := CheckWalletBalance(accountNo, currency, apiToken)
if err != nil {
// Log the error for further investigation
fmt.Println("Error checking wallet balance:", err)
// Return a generic error message to the client, or consider providing more specific information depending on the error type.
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to check wallet balance"})
return
}
// Respond with the balance information if successful
c.JSON(http.StatusOK, gin.H{"success": balanceResp.Success, "balance": balanceResp.Balance})
}package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/yourmodule/relworx/works/routes"
)
func main() {
// Create the Gin engine
router := gin.Default()
routes.ValidateNumberRoute()
routes.SendPaymentRoute(router)
routes.RequestMobilePaymentRoute()
routes.CheckBalanceRoute(router)
if err := godotenv.Load(); err != nil {
fmt.Println("Error loading .env file")
return
}
// Start the server with the router
if err := router.Run(":8001"); err != nil {
panic(err)
}
}
" here is my models file for the same"package models
type CheckWalletBalanceRequest struct {
AccountNo string json:"account_no" binding:"required"
Currency string json:"currency" binding:"required"
}
type CheckWalletBalanceResponse struct {
Success bool json:"success"
Balance string json:"balance"
}
" some one help please
and here is my main.go file "
Top comments (0)