65 lines
3.0 KiB
Swift
65 lines
3.0 KiB
Swift
import Foundation
|
|
|
|
// Mirrors Kotlin BmlForeignLimitsClient exactly.
|
|
// Endpoint: GET https://app.bankofmaldives.com.mv/api/v2/foreign-limits
|
|
final class BmlForeignLimitsClient {
|
|
|
|
private let base = "https://app.bankofmaldives.com.mv/api/v2"
|
|
private let ua = "bml-mobile-banking/348 (Apple; iOS 17.0; iPhone)"
|
|
private let ver = "2.1.44.348"
|
|
|
|
private let urlSession: URLSession = {
|
|
let cfg = URLSessionConfiguration.ephemeral
|
|
cfg.timeoutIntervalForRequest = 30
|
|
return URLSession(configuration: cfg)
|
|
}()
|
|
|
|
func fetchForeignLimits(session: BmlSession) async throws -> [BmlForeignLimit] {
|
|
var req = URLRequest(url: URL(string: "\(base)/foreign-limits")!)
|
|
req.setValue("Bearer \(session.accessToken)", forHTTPHeaderField: "Authorization")
|
|
req.setValue(ua, "User-Agent")
|
|
req.setValue(ver, "x-app-version")
|
|
req.setValue("application/json","Accept")
|
|
|
|
let (data, response) = try await urlSession.data(for: req)
|
|
let code = (response as? HTTPURLResponse)?.statusCode ?? 0
|
|
if code == 401 || code == 419 { throw BmlError.sessionExpired }
|
|
if code >= 500 { throw BmlError.networkError("HTTP \(code)") }
|
|
|
|
guard let root = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
|
|
root["success"] as? Bool == true,
|
|
let payload = root["payload"] as? [[String: Any]] else {
|
|
return []
|
|
}
|
|
|
|
return payload.map { item in
|
|
let usage = item["usageByCategory"] as? [String: Any] ?? [:]
|
|
let atm = usage["ATM"] as? [String: Any] ?? [:]
|
|
let ecom = usage["ECOM"] as? [String: Any] ?? [:]
|
|
let pos = usage["POS"] as? [String: Any] ?? [:]
|
|
return BmlForeignLimit(
|
|
type: item["type"] as? String ?? "Debit",
|
|
used: item["used"] as? Double ?? 0,
|
|
totalLimit: item["totalLimit"] as? Double ?? 0,
|
|
generalCap: item["generalCap"] as? Double ?? 0,
|
|
generalRemaining: item["generalRemaining"] as? Double ?? 0,
|
|
medicalRemaining: item["medicalRemaining"] as? Double ?? 0,
|
|
isAtmEnabled: item["isAtmEnabled"] as? Bool ?? false,
|
|
isPosEnabled: item["isPosEnabled"] as? Bool ?? false,
|
|
atmRemaining: atm["remaining"] as? Double ?? 0,
|
|
atmLimit: atm["limit"] as? Double ?? 0,
|
|
ecomRemaining: ecom["remaining"] as? Double ?? 0,
|
|
ecomLimit: ecom["limit"] as? Double ?? 0,
|
|
posRemaining: pos["remaining"] as? Double ?? 0,
|
|
posLimit: pos["limit"] as? Double ?? 0
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
private extension URLRequest {
|
|
mutating func setValue(_ value: String, _ field: String) {
|
|
setValue(value, forHTTPHeaderField: field)
|
|
}
|
|
}
|