feat: get number balance

This commit is contained in:
HeshamTB 2024-04-29 19:02:24 +03:00
parent 9c1014ac72
commit 6e955fa4f1
5 changed files with 76 additions and 4 deletions

View File

@ -1,10 +1,13 @@
package urls
const (
URL_MYSTC_API_BASE = "https://mystc.stc.com.sa"
PATH_API_AUTH_BASE = URL_MYSTC_API_BASE + "/api/mystc-api-authentication"
URL_MYSTC_API_BASE = "https://mystc.stc.com.sa/api"
PATH_API_AUTH_BASE = URL_MYSTC_API_BASE + "/mystc-api-authentication"
PATH_PHONES_LIST = PATH_API_AUTH_BASE + "/phones-list"
PATH_LOGIN = PATH_API_AUTH_BASE + "/login"
PATH_LOGIN_VERIFICATION = PATH_API_AUTH_BASE + "/login-verification"
PATH_API_NUM_MGMT_BASE = URL_MYSTC_API_BASE + "/mystc-api-user-number-management/api"
PATH_PREPAID_BALANCE_F = PATH_API_NUM_MGMT_BASE + "/phone-number/%s/prepaid-balance"
)

11
main.go
View File

@ -57,7 +57,6 @@ func main() {
}
fmt.Println("OTP Sent!")
fmt.Printf("loginOTP: %v\n", loginOTP)
fmt.Print("Enter OTP: ")
otp, err := reader.ReadString('\n')
otp = strings.Trim(otp, "\n")
@ -69,7 +68,15 @@ func main() {
}
fmt.Println("Logged in!")
fmt.Printf("login: %v\n", login)
balance, err := endpoints.GetBalance(login, selectedNumber.Number)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
fmt.Printf("Balance %f\n", balance.Balance)
}

47
pkg/endpoints/data.go Normal file
View File

@ -0,0 +1,47 @@
package endpoints
import (
"encoding/json"
"fmt"
"io"
"mystcapi/internal/urls"
"mystcapi/pkg/models"
"net/http"
)
func GetBalance(
login models.LoginVerificationResponse,
phoneNumber string) (models.PrePaidBalanceResponse, error) {
prepaidBalanceReq := models.PrePaidBalanceResponse{}
req, err := makeGetPrepaidBalanceReq(phoneNumber)
if err != nil {
return prepaidBalanceReq, err
}
req.Header.Set("Authorization", fmt.Sprintf("%s %s", login.TokenType, login.AccessToken))
resp, err := client.Do(req)
if err != nil {
return prepaidBalanceReq, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return prepaidBalanceReq, err
}
err = json.Unmarshal(body, &prepaidBalanceReq)
return prepaidBalanceReq, err
}
func makeGetPrepaidBalanceReq(phoneNumber string) (*http.Request, error) {
return http.NewRequest(
http.MethodGet,
fmt.Sprintf(urls.PATH_PREPAID_BALANCE_F, phoneNumber),
nil,
)
}

View File

@ -121,3 +121,4 @@ func VerifyLoginOTP(
return loginVerResp, err
}

14
pkg/models/data.go Normal file
View File

@ -0,0 +1,14 @@
package models
type PrePaidBalanceResponse struct {
PhoneNumber string `json:"phoneNumber"`
Balance float64 `json:"balance"`
CurrencyCode string `json:"currencyCode"`
AirTimeExpiryDate string `json:"airtimeExpiryDate"`
AccountExpiryDate string `json:"accountExpiryDate"`
CustomerId string `json:"customerId"`
ValidationType string `json:"validationType"`
TotalOtherBalances float64 `json:"totalOtherBalances"`
TotalFrozenBalances float64 `json:"totalFrozenBalances"`
}