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