Fix issues with TwigsApiService
This commit is contained in:
parent
39468a3a36
commit
c90514607c
1 changed files with 18 additions and 15 deletions
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue