Skip to content

Commit

Permalink
Merge pull request #55 from baller784/from_date_decode_fix
Browse files Browse the repository at this point in the history
[SDK ver. 3.3.8] Fixed iso8601 with date time decoding
  • Loading branch information
ConstantinKV authored Oct 19, 2020
2 parents 723c0ce + 434da15 commit 5b0d059
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 9 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# Changelog

## [3.3.8] - 2020-10-16

### Fixed.

- Fixed dates decoding.

## [3.3.7] - 2020-10-07

### Added.

- Added missed attributes to `SEAttempt`.


## [3.3.6] - 2020-10-02

### Added.
Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PODS:
- Nimble (9.0.0)
- PKHUD (5.3.0)
- Quick (3.0.0)
- SaltEdge-iOS-Swift (3.3.6):
- SaltEdge-iOS-Swift (3.3.8):
- TrustKit
- SDWebImage (5.9.2):
- SDWebImage/Core (= 5.9.2)
Expand Down Expand Up @@ -45,7 +45,7 @@ SPEC CHECKSUMS:
Nimble: 3b4ec3fd40f1dc178058e0981107721c615643d8
PKHUD: 98f3e4bc904b9c916f1c5bb6d765365b5357291b
Quick: 6d9559f40647bc4d510103842ef2fdd882d753e2
SaltEdge-iOS-Swift: dd3c81d3f969eb4a7148e5f79e210a13d0628278
SaltEdge-iOS-Swift: 903862be01f04aee7f0d03a031d8795efe133f6e
SDWebImage: 0b42b8719ab0c5257177d5894306e8a336b21cbb
SDWebImageSVGKitPlugin: 06a811c05b9e839982baeb5251d15fe7a79abb82
SVGKit: 652cdf7bef8bec8564d04a8511d3ab50c7595fac
Expand Down
22 changes: 22 additions & 0 deletions Example/Tests/Extensions/DateUtilsSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ class DateUtilsSpec: QuickSpec {
}
}

describe("iso8601DateTime") {
context("when date string is given") {
it("should return date from string in iso8601DateTime format") {
let dateString = "2020-10-16T14:31:21Z"

var dateComponents = DateComponents()
dateComponents.year = 2020
dateComponents.month = 10
dateComponents.day = 16
dateComponents.hour = 14
dateComponents.minute = 31
dateComponents.second = 21
dateComponents.timeZone = TimeZone.utc

let calendar = Calendar(identifier: .iso8601)
let expectedDate = calendar.date(from: dateComponents)

expect(DateFormatter.iso8601DateTime.date(from: dateString)).to(equal(expectedDate))
}
}
}

describe("time") {
it("should return string from date in time format") {
var dateComponents = DateComponents()
Expand Down
2 changes: 1 addition & 1 deletion SaltEdge-iOS-Swift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'SaltEdge-iOS-Swift'
s.version = '3.3.7'
s.version = '3.3.8'
s.summary = "A handful of classes to help you interact with the Salt Edge API from your iOS or macOS app."
s.description = <<-DESC
SaltEdge-iOS is a library targeted at easing the interaction with the [Salt Edge API](https://docs.saltedge.com/).
Expand Down
30 changes: 27 additions & 3 deletions saltedge-ios-swift/Classes/API/HTTPService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,18 @@ struct HTTPPaginatedService<T: Decodable> {
}
}
}

struct HTTPService<T: Decodable> {
static func makeRequest(_ request: Routable, completion: SEHTTPResponse<T>) {
let urlRequest = request.asURLRequest()

makeRequest(urlRequest, completion: completion)
}

static func makeRequest(_ request: URLRequest, completion: SEHTTPResponse<T>) {
let task = SessionManager.shared.dataTask(with: request) { data, response, error in
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
decoder.dateDecodingStrategyFormatters = [DateFormatter.yyyyMMdd, DateFormatter.iso8601DateTime]

let (data, error) = handleResponse(from: data, error: error, decoder: decoder)

Expand Down Expand Up @@ -184,3 +184,27 @@ func handleResponse(from data: Data?, error: Error?, decoder: JSONDecoder) -> (D

return (jsonData, nil)
}

private extension JSONDecoder {
var dateDecodingStrategyFormatters: [DateFormatter]? {
get {
return nil
}
set {
guard let formatters = newValue else { return }

self.dateDecodingStrategy = .custom { decoder in
let container = try decoder.singleValueContainer()
let dateString = try container.decode(String.self)

for formatter in formatters {
if let date = formatter.date(from: dateString) {
return date
}
}

throw DecodingError.dataCorruptedError(in: container, debugDescription: "Cannot decode date string \(dateString)")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public class SEBaseConnectionParams: Encodable, ParametersEncodable {
public let fromDate: Date?
public let toDate: Date?

public init(fromDate: Date? = nil,
toDate: Date? = nil) {
public init(fromDate: Date? = nil, toDate: Date? = nil) {
self.fromDate = fromDate
self.toDate = toDate
}
Expand Down
8 changes: 8 additions & 0 deletions saltedge-ios-swift/Classes/Extensions/DateUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ extension DateFormatter {
return formatter
}()

static let iso8601DateTime: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
formatter.timeZone = TimeZone.utc
formatter.locale = Locale(identifier: "en_US_POSIX")
return formatter
}()

static let time: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "hh:mm:ss"
Expand Down

0 comments on commit 5b0d059

Please sign in to comment.