mirror of
https://github.com/home-assistant/iOS.git
synced 2026-06-17 09:25:54 -05:00
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.
124 lines
5.8 KiB
Swift
124 lines
5.8 KiB
Swift
//
|
|
// HAAPI+RequestHelpers.swift
|
|
// HomeAssistant
|
|
//
|
|
// Created by Stephan Vanterpool on 8/12/18.
|
|
// Copyright © 2018 Robbie Trencheny. All rights reserved.
|
|
//
|
|
|
|
import Alamofire
|
|
import Foundation
|
|
import PromiseKit
|
|
import ObjectMapper
|
|
|
|
extension HomeAssistantAPI {
|
|
// MARK: - Helper methods for reducing boilerplate.
|
|
|
|
func handleResponse<T>(response: DataResponse<T>, seal: Resolver<T>, callingFunctionName: String) {
|
|
// Current.Log.verbose("\(callingFunctionName) response timeline: \(response.timeline)")
|
|
switch response.result {
|
|
case .success(let value):
|
|
seal.fulfill(value)
|
|
case .failure(let error):
|
|
Current.Log.error("Error on \(callingFunctionName) request: \(error)")
|
|
seal.reject(error)
|
|
}
|
|
}
|
|
|
|
func request(path: String, callingFunctionName: String, method: HTTPMethod = .get,
|
|
parameters: Parameters? = nil, encoding: ParameterEncoding = URLEncoding.default,
|
|
headers: HTTPHeaders? = nil) -> Promise<String> {
|
|
return Promise { seal in
|
|
let url = try connectionInfo().activeAPIURL.appendingPathComponent(path)
|
|
_ = manager.request(url, method: method, parameters: parameters, encoding: encoding,
|
|
headers: headers)
|
|
.validate()
|
|
.responseString { (response: DataResponse<String>) in
|
|
self.handleResponse(response: response, seal: seal,
|
|
callingFunctionName: callingFunctionName)
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
func request<T: BaseMappable>(path: String, callingFunctionName: String, method: HTTPMethod = .get,
|
|
parameters: Parameters? = nil,
|
|
encoding: ParameterEncoding = URLEncoding.default,
|
|
headers: HTTPHeaders? = nil) -> Promise<T> {
|
|
return Promise { seal in
|
|
let url = try connectionInfo().activeAPIURL.appendingPathComponent(path)
|
|
_ = manager.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers)
|
|
.validate()
|
|
.responseObject { (response: DataResponse<T>) in
|
|
self.handleResponse(response: response, seal: seal,
|
|
callingFunctionName: callingFunctionName)
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
func request<T: BaseMappable>(path: String, callingFunctionName: String, method: HTTPMethod = .get,
|
|
parameters: Parameters? = nil,
|
|
encoding: ParameterEncoding = URLEncoding.default,
|
|
headers: HTTPHeaders? = nil) -> Promise<[T]> {
|
|
return Promise { seal in
|
|
let url = try connectionInfo().activeAPIURL.appendingPathComponent(path)
|
|
_ = manager.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers)
|
|
.validate()
|
|
.responseArray { (response: DataResponse<[T]>) in
|
|
self.handleResponse(response: response, seal: seal,
|
|
callingFunctionName: callingFunctionName)
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
func request<T: ImmutableMappable>(path: String, callingFunctionName: String, method: HTTPMethod = .get,
|
|
parameters: Parameters? = nil,
|
|
encoding: ParameterEncoding = URLEncoding.default,
|
|
headers: HTTPHeaders? = nil) -> Promise<[T]> {
|
|
return Promise { seal in
|
|
let url = try connectionInfo().activeAPIURL.appendingPathComponent(path)
|
|
_ = manager.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers)
|
|
.validate()
|
|
.responseArray { (response: DataResponse<[T]>) in
|
|
self.handleResponse(response: response, seal: seal,
|
|
callingFunctionName: callingFunctionName)
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
func request<T: ImmutableMappable>(path: String, callingFunctionName: String, method: HTTPMethod = .get,
|
|
parameters: Parameters? = nil,
|
|
encoding: ParameterEncoding = URLEncoding.default,
|
|
headers: HTTPHeaders? = nil) -> Promise<T> {
|
|
return Promise { seal in
|
|
let url = try connectionInfo().activeAPIURL.appendingPathComponent(path)
|
|
_ = manager.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers)
|
|
.validate()
|
|
.responseObject { (response: DataResponse<T>) in
|
|
self.handleResponse(response: response, seal: seal,
|
|
callingFunctionName: callingFunctionName)
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
func requestImmutable<T: ImmutableMappable>(path: String, callingFunctionName: String, method: HTTPMethod = .get,
|
|
parameters: Parameters? = nil,
|
|
encoding: ParameterEncoding = URLEncoding.default,
|
|
headers: HTTPHeaders? = nil) -> Promise<T> {
|
|
return Promise { seal in
|
|
let url = try connectionInfo().activeAPIURL.appendingPathComponent(path)
|
|
_ = manager.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers)
|
|
.validate()
|
|
.responseObject { (response: DataResponse<T>) in
|
|
self.handleResponse(response: response, seal: seal,
|
|
callingFunctionName: callingFunctionName)
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|