Files
iOS/Sources/Shared/API/Webhook/Request&Response/WebhookResponseServiceCall.swift
Zac West 4d9a530637 Reorganize files in repo, pull out build settings from pbxproj (#1140)
This is somewhat in prep of being able to make the project file generated, but also just organizes things into more concrete directory structures.

This pulls out _all_ of the build settings from the root level, and most from the target level, into xcconfigs.

The new directory structure looks like:

- Sources
  - App
    - (everything from HomeAssistant/)
  - WatchApp
  - Shared
  - MacBridge
  - Extensions
    - Intents
    - NotificationContent
    - NotificationService
    - Share
    - Today
    - Watch
    - Widgets
- Tests
  - App
  - UI
  - Shared

Somewhat intentionally, the file structure under these is not yet standardized/organized.

The project targets are now:

- App
- WatchApp
- Shared-iOS
- Shared-watchOS
- MacBridge
- Tests-App
- Tests-UI
- Tests-Shared
- Extension-Intents
- Extension-NotificationContent
- Extension-NotificationService
- Extension-Share
- Extension-Today
- Extension-Widget
- WatchExtension-Watch

This does not yet clean up resources vs. sources, nor does it handle some of the "it's in Sources/App but it's part of Shared" crossover directory issues.
2020-10-03 00:15:04 -07:00

47 lines
1.4 KiB
Swift

import Foundation
import UserNotifications
import PromiseKit
extension WebhookResponseIdentifier {
static var serviceCall: Self { .init(rawValue: "serviceCall") }
}
struct WebhookResponseServiceCall: WebhookResponseHandler {
let api: HomeAssistantAPI
init(api: HomeAssistantAPI) {
self.api = api
}
static func shouldReplace(request current: WebhookRequest, with proposed: WebhookRequest) -> Bool {
// every service call is distinct
return false
}
func handle(
request: Promise<WebhookRequest>,
result: Promise<Any>
) -> Guarantee<WebhookResponseHandlerResult> {
firstly {
when(fulfilled: request, result)
}.get { request, _ in
let requestDictionary = try request.asDictionary()
let domain = requestDictionary["domain"] as? String ?? "(unknown)"
let service = requestDictionary["service"] as? String ?? "(unknown)"
let payload = requestDictionary["service_data"] as? [String: Any] ?? [:]
let event = ClientEvent(
text: "Called service: \(domain).\(service)",
type: .serviceCall,
payload: payload
)
Current.clientEventStore.addEvent(event)
}.then { _ in
Guarantee.value(WebhookResponseHandlerResult.default)
}.recover { _ in
Guarantee.value(WebhookResponseHandlerResult.default)
}
}
}