Files
purchases-ios/Sources/Networking/Responses/GetIntroEligibilityResponse.swift
T
NachoSoto 59d492df88 GetIntroEligibilityOperation: replaced response parsing with Decodable (#1576)
The new response is now represented by `GetIntroEligibilityResponse`, and has dedicated tests (`GetIntroEligibilityDecodingTests`) using a JSON fixture.
The operation becomes much simpler now, plus I've done a lot of code removing that was redundant.

The actual response handling is now 3 lines:
```swift
let result: [String: IntroEligibility] = Set(productIdentifiers)
  .dictionaryWithValues { eligibilities?[$0] ?? .unknown }
  .mapValues(IntroEligibility.init)
```

All the tests in `BackendGetIntroEligibilityTests` pass with no changes.
2022-05-26 13:54:45 -07:00

40 lines
1002 B
Swift

//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// GetIntroEligibilityResponse.swift
//
// Created by Nacho Soto on 5/12/22.
import Foundation
struct GetIntroEligibilityResponse {
var eligibilityByProductIdentifier: [String: IntroEligibilityStatus]
}
extension GetIntroEligibilityResponse: HTTPResponseBody {
static func create(with data: Data) throws -> Self {
let response = try [String: Bool?].create(with: data)
return .init(
eligibilityByProductIdentifier: response
.mapValues {
if let status = $0 {
return status ? .eligible : .ineligible
} else {
return .unknown
}
}
)
}
}