mirror of
https://github.com/RevenueCat/purchases-ios
synced 2026-11-10 22:45:55 +05:45
For [CSDK-478]. Follow up to #1929. ### Example: > DEBUG: ℹ️ PostReceiptDataOperation: Started INFO: ℹ️ Receipt parsed successfully DEBUG: ℹ️ PostReceiptDataOperation: Posting receipt: { "opaque_value" : "Rn\/39QYAAAA=", "sha1_hash" : "Ii8Z7ocZm524eJcWFOvVauNrNHs=", "bundle_id" : "com.revenuecat.StoreKitTestApp", "in_app_purchases" : [ { "quantity" : 1, "product_id" : "com.revenuecat.monthly_4.99.1_week_intro", "purchase_date" : "2022-09-27T19:09:47Z", "transaction_id" : "0", "is_in_intro_offer_period" : true, "expires_date" : "2022-09-27T19:09:57Z" } ], "application_version" : "1", "creation_date" : "2022-09-27T19:09:47Z", "expiration_date" : "4001-01-01T00:00:00Z" } DEBUG: ℹ️ There are no requests currently running, starting request POST receipts DEBUG: ℹ️ API request started: POST /v1/receipts DEBUG: ℹ️ API request completed: POST /v1/receipts 200 DEBUG: ℹ️ PostReceiptDataOperation: Finished DEBUG: ℹ️ Serial request done: POST receipts, 0 requests left in the queue DEBUG: ℹ️ Sending updated CustomerInfo to delegate. INFO: 😻💰 Purchased product - 'com.revenuecat.monthly_4.99.1_week_intro' INFO: 💰 Finishing transaction com.revenuecat.monthly_4.99.1_week_intro 0 () [CSDK-478]: https://revenuecats.atlassian.net/browse/CSDK-478?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
175 lines
5.5 KiB
Swift
175 lines
5.5 KiB
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
|
|
//
|
|
// JSONDecoder+Extensions.swift
|
|
//
|
|
// Created by Juanpe Catalán on 25/8/21.
|
|
|
|
import Foundation
|
|
|
|
enum CodableError: Error, CustomStringConvertible, LocalizedError {
|
|
|
|
case unexpectedValue(Any.Type, Any)
|
|
case valueNotFound(value: Any.Type, context: DecodingError.Context)
|
|
case invalidJSONObject(value: [String: Any])
|
|
|
|
var description: String {
|
|
switch self {
|
|
case let .unexpectedValue(type, value):
|
|
return Strings.codable.unexpectedValueError(type: type, value: value).description
|
|
case let .valueNotFound(value, context):
|
|
return Strings.codable.valueNotFoundError(value: value, context: context).description
|
|
case let .invalidJSONObject(value):
|
|
return Strings.codable.invalid_json_error(jsonData: value).description
|
|
}
|
|
}
|
|
|
|
var errorDescription: String? { return self.description }
|
|
|
|
}
|
|
|
|
extension JSONDecoder {
|
|
|
|
/// Decodes a top-level value of the given type from the given Data containing a JSON representation of `type`.
|
|
///
|
|
/// - Parameters:
|
|
/// - type: The type of the value to decode. The default is `T.self`.
|
|
/// - data: The data to decode from.
|
|
/// - Returns: A value of the requested type.
|
|
/// - throws: `CodableError` or `DecodableError` if the data is invalid or can't be deserialized.
|
|
func decode<T: Decodable>(
|
|
_ type: T.Type = T.self,
|
|
jsonData: Data,
|
|
logErrors: Bool = true
|
|
) throws -> T {
|
|
do {
|
|
return try self.decode(type, from: jsonData)
|
|
} catch {
|
|
if logErrors {
|
|
ErrorUtils.logDecodingError(error)
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
/// Decodes a top-level value of the given type from the given Dictionary.
|
|
///
|
|
/// - Parameters:
|
|
/// - type: The type of the value to decode. The default is `T.self`.
|
|
/// - dictionary: The dictionary to decode from.
|
|
/// - Returns: A value of the requested type.
|
|
/// - Throws: `CodableError` or `DecodableError` if the data is invalid or can't be deserialized.
|
|
/// - Note: this method logs the error before throwing, so it's "safe" to use with `try?`
|
|
func decode<T: Decodable>(
|
|
_ type: T.Type = T.self,
|
|
dictionary: [String: Any],
|
|
logErrors: Bool = true
|
|
) throws -> T {
|
|
if JSONSerialization.isValidJSONObject(dictionary) {
|
|
return try self.decode(type,
|
|
jsonData: try JSONSerialization.data(withJSONObject: dictionary),
|
|
logErrors: logErrors)
|
|
} else {
|
|
throw CodableError.invalidJSONObject(value: dictionary)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: Decoding Error handling
|
|
|
|
extension ErrorUtils {
|
|
|
|
static func logDecodingError(_ error: Error) {
|
|
guard let decodingError = error as? DecodingError else {
|
|
Logger.error(Strings.codable.decoding_error(error))
|
|
return
|
|
}
|
|
|
|
switch decodingError {
|
|
case let .dataCorrupted(context):
|
|
Logger.error(Strings.codable.corrupted_data_error(context: context))
|
|
case let .keyNotFound(key, context):
|
|
// This is expected to happen occasionally, the backend doesn't always populate all key/values.
|
|
Logger.debug(Strings.codable.keyNotFoundError(key: key, context: context))
|
|
case let .valueNotFound(value, context):
|
|
Logger.debug(Strings.codable.valueNotFoundError(value: value, context: context))
|
|
case let .typeMismatch(type, context):
|
|
Logger.error(Strings.codable.typeMismatch(type: type, context: context))
|
|
@unknown default:
|
|
Logger.error("Unhandled DecodingError: \(decodingError)\n\(Strings.codable.decoding_error(decodingError))")
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension Encodable {
|
|
|
|
func asDictionary() throws -> [String: Any] {
|
|
let data = try JSONEncoder.default.encode(self)
|
|
let result = try JSONSerialization.jsonObject(with: data, options: [])
|
|
|
|
guard let result = result as? [String: Any] else {
|
|
throw CodableError.unexpectedValue(type(of: result), result)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
/// - Throws: if encoding failed
|
|
/// - Returns: `nil` if the encoded `Data` can't be serialized into a `String`.
|
|
var prettyPrintedJSON: String? {
|
|
get throws {
|
|
return String(data: try self.prettyPrintedData, encoding: .utf8)
|
|
}
|
|
}
|
|
|
|
var prettyPrintedData: Data {
|
|
get throws {
|
|
return try JSONEncoder.prettyPrinted.encode(self)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: -
|
|
|
|
extension JSONEncoder {
|
|
|
|
static let `default`: JSONEncoder = {
|
|
let encoder = JSONEncoder()
|
|
encoder.keyEncodingStrategy = .convertToSnakeCase
|
|
encoder.dateEncodingStrategy = .iso8601
|
|
|
|
return encoder
|
|
}()
|
|
|
|
static let prettyPrinted: JSONEncoder = {
|
|
let encoder = JSONEncoder()
|
|
encoder.keyEncodingStrategy = .convertToSnakeCase
|
|
encoder.dateEncodingStrategy = .iso8601
|
|
encoder.outputFormatting = .prettyPrinted
|
|
|
|
return encoder
|
|
}()
|
|
|
|
}
|
|
|
|
extension JSONDecoder {
|
|
|
|
static let `default`: JSONDecoder = {
|
|
let decoder = JSONDecoder()
|
|
decoder.keyDecodingStrategy = .convertFromSnakeCase
|
|
decoder.dateDecodingStrategy = .iso8601
|
|
|
|
return decoder
|
|
}()
|
|
|
|
}
|