iOS/Sources/App/Settings/Notifications/NotificationRateLimitsAPI.swift
Michal Šrůtek 08781c19f0
Remove default options parameter (#2621)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->
This PR removes the `options` parameters which use the default value.

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

<img width="1014" alt="Screenshot 2024-03-03 at 21 17 55"
src="https://github.com/home-assistant/iOS/assets/35694712/85324caf-c36d-4366-a9f8-9c349192a361">
<img width="1049" alt="Screenshot 2024-03-03 at 21 17 17"
src="https://github.com/home-assistant/iOS/assets/35694712/cebddbe6-8ea4-415e-b12a-0e35f86bf5a3">
<img width="1022" alt="Screenshot 2024-03-03 at 21 16 53"
src="https://github.com/home-assistant/iOS/assets/35694712/ccccb902-9791-42f6-8868-5900093c40ae">
<img width="991" alt="Screenshot 2024-03-03 at 21 16 28"
src="https://github.com/home-assistant/iOS/assets/35694712/f2f33685-a55c-47ed-a555-8cc0fc1561ce">
<img width="1329" alt="Screenshot 2024-03-03 at 21 16 00"
src="https://github.com/home-assistant/iOS/assets/35694712/2dc156da-29d6-44e4-a064-4fbdb79139bd">
<img width="1130" alt="Screenshot 2024-03-03 at 21 14 53"
src="https://github.com/home-assistant/iOS/assets/35694712/628dd2ff-5bba-4ee7-912b-cdbdf73f18b2">
2024-03-04 09:28:53 +01:00

97 lines
3.2 KiB
Swift

import Eureka
import Foundation
import PromiseKit
import Shared
struct RateLimitResponse: Decodable {
var target: String
struct RateLimits: Decodable {
var attempts: Int
var successful: Int
var errors: Int
var total: Int
var maximum: Int
var remaining: Int
var resetsAt: Date
}
var rateLimits: RateLimits
}
class NotificationRateLimitsAPI {
class func rateLimits(pushID: String) -> Promise<RateLimitResponse> {
firstly { () -> Promise<URLRequest> in
do {
var urlRequest = URLRequest(url: URL(
string: "https://mobile-apps.home-assistant.io/api/checkRateLimits"
)!)
urlRequest.httpMethod = "POST"
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.httpBody = try JSONSerialization.data(withJSONObject: [
"push_token": pushID,
])
return .value(urlRequest)
} catch {
return .init(error: error)
}
}.then {
URLSession.shared.dataTask(.promise, with: $0)
}.map { data, _ throws -> RateLimitResponse in
let decoder = with(JSONDecoder()) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.sss'Z'"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(identifier: "UTC")
$0.dateDecodingStrategy = .formatted(dateFormatter)
}
return try decoder.decode(RateLimitResponse.self, from: data)
}
}
}
extension RateLimitResponse.RateLimits {
func row(for keyPath: KeyPath<Self, Int>) -> BaseRow {
LabelRow {
$0.value = NumberFormatter.localizedString(
from: NSNumber(value: self[keyPath: keyPath]),
number: .none
)
$0.title = { () -> String in
switch keyPath {
case \.attempts:
return L10n.SettingsDetails.Notifications.RateLimits.attempts
case \.successful:
return L10n.SettingsDetails.Notifications.RateLimits.delivered
case \.errors:
return L10n.SettingsDetails.Notifications.RateLimits.errors
case \.total:
return L10n.SettingsDetails.Notifications.RateLimits.total
case \.maximum:
return ""
default:
fatalError("missing key: \(keyPath)")
}
}()
}
}
func row(for keyPath: KeyPath<Self, Date>) -> BaseRow {
LabelRow { row in
row.value = DateFormatter.localizedString(
from: self[keyPath: keyPath],
dateStyle: .none,
timeStyle: .medium
)
switch keyPath {
case \.resetsAt:
row.title = L10n.SettingsDetails.Notifications.RateLimits.resetsIn
row.tag = "resetsIn"
default:
fatalError("missing key: \(keyPath)")
}
}
}
}