Fix issues with TwigsApiService

This commit is contained in:
William Brawner 2021-12-23 20:15:18 -07:00
parent 39468a3a36
commit c90514607c

View file

@ -7,17 +7,26 @@
import Foundation
public class TwigsApiService: BudgetRepository, CategoryRepository, RecurringTransactionsRepository, TransactionRepository, UserRepository {
open class TwigsApiService: BudgetRepository, CategoryRepository, RecurringTransactionsRepository, TransactionRepository, UserRepository {
let requestHelper: RequestHelper
public convenience init(_ serverUrl: String) {
self.init(RequestHelper(serverUrl))
public convenience init() {
self.init(RequestHelper())
}
init(_ requestHelper: RequestHelper) {
self.requestHelper = requestHelper
}
public var baseUrl: String? {
get {
return requestHelper.baseUrl
}
set {
requestHelper.baseUrl = newValue
}
}
public var token: String? {
get {
return requestHelper.token
@ -236,13 +245,15 @@ public class TwigsApiService: BudgetRepository, CategoryRepository, RecurringTra
class RequestHelper {
let decoder = JSONDecoder()
private var _baseUrl: String = ""
var baseUrl: String {
private var _baseUrl: String? = nil
var baseUrl: String? {
get {
self.baseUrl
}
set {
var correctServer = newValue.lowercased()
guard var correctServer = newValue?.lowercased() else {
return
}
if !correctServer.starts(with: "http://") && !correctServer.starts(with: "https://") {
correctServer = "http://\(correctServer)"
}
@ -251,8 +262,7 @@ class RequestHelper {
}
var token: String?
init(_ serverUrl: String) {
self.baseUrl = serverUrl
init() {
self.decoder.dateDecodingStrategy = .formatted(Date.iso8601DateFormatter)
}
@ -417,13 +427,6 @@ extension Date {
return dateFormatter
}()
static let localeDateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.dateFormat = DateFormatter.dateFormat(fromTemplate: "yyyyMMdd", options: 0, locale: Locale.current)
return dateFormatter
}()
func toISO8601String() -> String {
return Date.iso8601DateFormatter.string(from: self)
}