diff --git a/internal/urls/common.go b/internal/urls/common.go index cc4d256..1811103 100644 --- a/internal/urls/common.go +++ b/internal/urls/common.go @@ -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" ) diff --git a/main.go b/main.go index 2fe9acc..2675420 100644 --- a/main.go +++ b/main.go @@ -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) + + } diff --git a/pkg/endpoints/data.go b/pkg/endpoints/data.go new file mode 100644 index 0000000..689b25c --- /dev/null +++ b/pkg/endpoints/data.go @@ -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, + ) +} + diff --git a/pkg/endpoints/login.go b/pkg/endpoints/login.go index 9c78142..4beda9a 100644 --- a/pkg/endpoints/login.go +++ b/pkg/endpoints/login.go @@ -121,3 +121,4 @@ func VerifyLoginOTP( return loginVerResp, err } + diff --git a/pkg/models/data.go b/pkg/models/data.go new file mode 100644 index 0000000..a08f1c8 --- /dev/null +++ b/pkg/models/data.go @@ -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"` +} +