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
|
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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue