Skip to content

Commit 6d86e5a

Browse files
authored
Merge pull request #8 from adborbas/adborbas/extra_logging
Extra logging and not parsing timezone and date
2 parents a8eab70 + ad0695d commit 6d86e5a

File tree

6 files changed

+27
-78
lines changed

6 files changed

+27
-78
lines changed

Sources/AlphaSwiftage/Private/AlphaVantageResponseSerializer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ struct AlphaVantageResponseSerializer<T: Decodable>: ResponseSerializer {
1717
return .success(result)
1818
} catch {
1919
guard let data = data else {
20-
logger.error("Failed to serialise request and returned data is nil. \(error.localizedDescription)")
20+
logger.error("Failed to serialise response and returned data is nil. \(String(describing: error))")
2121
return .failure(.unknown(error))
2222
}
2323

24-
logger.debug("Failed to serialise request: \(error.localizedDescription).")
24+
logger.debug("Failed to serialise response: \(String(describing: error)).")
2525
do {
2626
let error = try JSONDecoder().decode(AlphaVantageAPIError.self, from: data)
27-
logger.info("API Error: \(error.localizedDescription)")
27+
logger.info("API Error: \(String(describing: error))")
2828
return .failure(.apiError(error))
2929
} catch {
3030
guard let response = String(data: data, encoding: .utf8) else {

Sources/AlphaSwiftage/Private/SwiftLogger.swift

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// SwiftLogger.swift
3+
//
4+
//
5+
// Created by Adam Borbas on 23/03/2024.
6+
//
7+
8+
import Foundation
9+
import Logging
10+
import Alamofire
11+
12+
var logger: Logger = Logger(label: "com.adborbas.alphaswiftage")

Sources/AlphaSwiftage/Public/Model/CurrencyExchangeRate.swift

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ public struct CurrencyExchangeRate: Codable, Equatable {
66
public let toCurrencyCode: String
77
public let toCurrencyName: String
88
public let exchangeRate: Decimal
9-
public let lastRefreshed: Date
10-
public let timeZone: TimeZone
9+
public let lastRefreshed: String
10+
public let timeZone: String
1111
public let bidPrice: Decimal
1212
public let askPrice: Decimal
1313

14-
init(fromCurrencyCode: String, fromCurrencyName: String, toCurrencyCode: String, toCurrencyName: String, exchangeRate: Decimal, lastRefreshed: Date, timeZone: TimeZone, bidPrice: Decimal, askPrice: Decimal) {
14+
init(fromCurrencyCode: String, fromCurrencyName: String, toCurrencyCode: String, toCurrencyName: String, exchangeRate: Decimal, lastRefreshed: String, timeZone: String, bidPrice: Decimal, askPrice: Decimal) {
1515
self.fromCurrencyCode = fromCurrencyCode
1616
self.fromCurrencyName = fromCurrencyName
1717
self.toCurrencyCode = toCurrencyCode
@@ -44,23 +44,7 @@ public struct CurrencyExchangeRate: Codable, Equatable {
4444
exchangeRate = try container.decodeUSDecimal(forKey: .exchangeRate)
4545
bidPrice = try container.decodeUSDecimal(forKey: .bidPrice)
4646
askPrice = try container.decodeUSDecimal(forKey: .askPrice)
47-
48-
49-
// Handling TimeZone
50-
let timeZoneString = try container.decode(String.self, forKey: .timeZone)
51-
guard let timeZone = TimeZone(identifier: timeZoneString) else {
52-
throw DecodingError.dataCorruptedError(forKey: .timeZone, in: container, debugDescription: "Time zone string is not a valid identifier.")
53-
}
54-
self.timeZone = timeZone
55-
56-
// Custom decoding for the Date
57-
let dateFormatter = DateFormatter()
58-
dateFormatter.timeZone = timeZone
59-
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
60-
let dateString = try container.decode(String.self, forKey: .lastRefreshed)
61-
guard let date = dateFormatter.date(from: dateString) else {
62-
throw DecodingError.dataCorruptedError(forKey: .lastRefreshed, in: container, debugDescription: "Date string does not match format expected by formatter.")
63-
}
64-
lastRefreshed = date
47+
timeZone = try container.decode(String.self, forKey: .timeZone)
48+
lastRefreshed = try container.decode(String.self, forKey: .lastRefreshed)
6549
}
6650
}

Sources/AlphaSwiftage/Public/Model/Symbol.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public struct Symbol: Codable, Equatable {
1414
public let region: String
1515
public let marketOpen: String
1616
public let marketClose: String
17-
public let timeZone: TimeZone
17+
public let timeZone: String
1818
public let currency: String
1919
public let matchScore: Float
2020

@@ -39,17 +39,11 @@ public struct Symbol: Codable, Equatable {
3939
marketOpen = try container.decode(String.self, forKey: .marketOpen)
4040
marketClose = try container.decode(String.self, forKey: .marketClose)
4141
currency = try container.decode(String.self, forKey: .currency)
42-
self.matchScore = try container.decodeUSFloat(forKey: .matchScore)
43-
44-
// Handling TimeZone
45-
let timeZoneString = try container.decode(String.self, forKey: .timeZone)
46-
guard let timeZone = TimeZone(identifier: timeZoneString) else {
47-
throw DecodingError.dataCorruptedError(forKey: .timeZone, in: container, debugDescription: "Time zone string is not a valid identifier.")
48-
}
49-
self.timeZone = timeZone
42+
matchScore = try container.decodeUSFloat(forKey: .matchScore)
43+
timeZone = try container.decode(String.self, forKey: .timeZone)
5044
}
5145

52-
init(symbol: String, name: String, type: String, region: String, marketOpen: String, marketClose: String, timeZone: TimeZone, currency: String, matchScore: Float) {
46+
init(symbol: String, name: String, type: String, region: String, marketOpen: String, marketClose: String, timeZone: String, currency: String, matchScore: Float) {
5347
self.symbol = symbol
5448
self.name = name
5549
self.type = type

Tests/AlphaVantageServiceTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ final class AlphaVantageServiceTests: XCTestCase {
4040
toCurrencyCode: target,
4141
toCurrencyName: "Hungarian Forint",
4242
exchangeRate: Decimal(floatLiteral: 393.5),
43-
lastRefreshed: Date(timeIntervalSince1970: 1710096503),
44-
timeZone: TimeZone.gmt,
43+
lastRefreshed: "2024-03-10 18:48:23",
44+
timeZone: "UTC",
4545
bidPrice: Decimal(floatLiteral: 393.49),
4646
askPrice: Decimal(floatLiteral: 393.51))
4747
given(response: .currencyExchangeRate, for: "https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=\(base)&to_currency=\(target)&apikey=\(apiKey)")
@@ -62,7 +62,7 @@ final class AlphaVantageServiceTests: XCTestCase {
6262
region: "XETRA",
6363
marketOpen: "08:00",
6464
marketClose: "20:00",
65-
timeZone: TimeZone(secondsFromGMT: 60*60*2)!,
65+
timeZone: "UTC+02",
6666
currency: "EUR",
6767
matchScore: 0.7273)
6868
let service = givenService()

0 commit comments

Comments
 (0)