From c90514607c5f653c2dd9301a587cbceb238fadb2 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Thu, 23 Dec 2021 20:15:18 -0700 Subject: [PATCH] Fix issues with TwigsApiService --- Sources/TwigsCore/TwigsApiService.swift | 33 ++++++++++++++----------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/Sources/TwigsCore/TwigsApiService.swift b/Sources/TwigsCore/TwigsApiService.swift index 9ca92f0..89e1350 100644 --- a/Sources/TwigsCore/TwigsApiService.swift +++ b/Sources/TwigsCore/TwigsApiService.swift @@ -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) }