-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
138 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 0 additions & 96 deletions
96
Sources/App/Settings/Notifications/NotificationRateLimitsAPI.swift
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
Sources/Shared/API/Webhook/Sensors/NotificationRateLimitSensor.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import Combine | ||
import Foundation | ||
import HAKit | ||
import PromiseKit | ||
|
||
final class NotificationRateLimitSensor: SensorProvider { | ||
let request: SensorProviderRequest | ||
init(request: SensorProviderRequest) { | ||
self.request = request | ||
} | ||
|
||
func sensors() -> Promise<[WebhookSensor]> { | ||
#if !os(watchOS) | ||
return .init { resolver in | ||
if let pushID = Current.settingsStore.pushID { | ||
NotificationRateLimitsAPI.rateLimits(pushID: pushID).done { response in | ||
resolver.fulfill([.init( | ||
name: "Notification Rate Limit", | ||
uniqueID: "notification-rate-limit", | ||
icon: "mdi:message-badge-outline", | ||
state: response.rateLimits.remaining | ||
)]) | ||
}.cauterize() | ||
} else { | ||
resolver.fulfill([]) | ||
} | ||
} | ||
#else | ||
return .value([]) | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Foundation | ||
import PromiseKit | ||
|
||
public struct RateLimitResponse: Decodable { | ||
public var target: String | ||
|
||
public struct RateLimits: Decodable { | ||
public var attempts: Int | ||
public var successful: Int | ||
public var errors: Int | ||
public var total: Int | ||
public var maximum: Int | ||
public var remaining: Int | ||
public var resetsAt: Date | ||
} | ||
|
||
public var rateLimits: RateLimits | ||
} | ||
|
||
public class NotificationRateLimitsAPI { | ||
public 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) | ||
} | ||
} | ||
} |