Skip to content

Commit

Permalink
Merge pull request #9 from AktuBuct/task/blue/IOS-3361
Browse files Browse the repository at this point in the history
[IOS-3361] Append coding strategy for Response and Method
  • Loading branch information
MetalheadSanya committed Dec 24, 2018
2 parents c6bd7da + cfe6c87 commit b69a223
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: objective-c
osx_image: xcode9.2
osx_image: xcode10
xcode_workspace: YandexMoneyCoreApi.xcworkspace
xcode_scheme: YandexMoneyCoreApiExamplePods
cache:
Expand Down
2 changes: 1 addition & 1 deletion YandexMoneyCoreApi.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'YandexMoneyCoreApi'
spec.version = '1.2.0'
spec.version = '1.3.0'
spec.homepage = 'https://github.com/yandex-money/core-api-swift'
spec.license = {
:type => "MIT",
Expand Down
8 changes: 7 additions & 1 deletion YandexMoneyCoreApi/Core/ApiMethod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* THE SOFTWARE.
*/

import struct Foundation.URL
import Foundation

/// HTTP method definitions.
///
Expand Down Expand Up @@ -52,6 +52,12 @@ public protocol ParametersEncoding {

// TODO: Use Encoder protocol

/// Date encoding strategy
var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy { get set }

/// Data encoding strategy
var dataEncodingStrategy: JSONEncoder.DataEncodingStrategy { get set }

/// Encodes the given top-level value.
///
/// - Parameters:
Expand Down
2 changes: 1 addition & 1 deletion YandexMoneyCoreApi/Core/ApiSessionDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extension ApiSessionDelegate: URLSessionDelegate {
/// - challenge: An object that contains the request for authentication.
/// - completionHandler: A handler that your delegate method must call providing the disposition
/// and credential.
open func urlSession(
public func urlSession(
_ session: URLSession,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
Expand Down
13 changes: 13 additions & 0 deletions YandexMoneyCoreApi/Core/Response/ApiResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ import typealias FunctionalSwift.Result
/// The response from the server.
public protocol ApiResponse {

/// Date decoding strategy
static var dateDecodingStrategy: JSONDecoder.DateDecodingStrategy { get }

/// Data decoding strategy
static var dataDecodingStrategy: JSONDecoder.DataDecodingStrategy { get }

/// Creates response.
///
/// - Parameters:
Expand Down Expand Up @@ -61,4 +67,11 @@ extension ApiResponse {
public static func makeSpecificError(response: HTTPURLResponse, data: Data) -> Error? {
return nil
}

public static var dateDecodingStrategy: JSONDecoder.DateDecodingStrategy {
return .deferredToDate
}
public static var dataDecodingStrategy: JSONDecoder.DataDecodingStrategy {
return .base64
}
}
5 changes: 4 additions & 1 deletion YandexMoneyCoreApi/Core/Response/JsonApiResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public protocol JsonApiResponse: ApiResponse, Decodable { }
extension JsonApiResponse {

public static func makeResponse(response: HTTPURLResponse, data: Data) -> Self? {
guard let object = try? JSONDecoder().decode(Self.self, from: data) else { return nil }
let decoder = JSONDecoder()
decoder.dataDecodingStrategy = dataDecodingStrategy
decoder.dateDecodingStrategy = dateDecodingStrategy
guard let object = try? decoder.decode(Self.self, from: data) else { return nil }
return object
}
}
8 changes: 8 additions & 0 deletions YandexMoneyCoreApi/Encoding/JsonParametersEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public final class JsonParametersEncoder: ParametersEncoding {
/// The options for writing the parameters as JSON data.
public let encoder: JSONEncoder

/// Date encoding strategy
public var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy = .deferredToDate

/// Data encoding strategy
public var dataEncodingStrategy: JSONEncoder.DataEncodingStrategy = .base64

private var body = Data()

// MARK: Initialization
Expand All @@ -50,6 +56,8 @@ public final class JsonParametersEncoder: ParametersEncoding {
/// - Returns: The new `JSONEncoding` instance.
public init(options: JSONEncoder.OutputFormatting = []) {
encoder = JSONEncoder()
encoder.dateEncodingStrategy = dateEncodingStrategy
encoder.dataEncodingStrategy = dataEncodingStrategy
encoder.outputFormatting = options
}

Expand Down
13 changes: 11 additions & 2 deletions YandexMoneyCoreApi/Encoding/QueryParametersEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ private typealias QueryParameters = [String: Any]
/// the HTTP body depends on the destination of the encoding.
public final class QueryParametersEncoder: ParametersEncoding {

/// Date encoding strategy
public var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy = .deferredToDate

/// Data encoding strategy
public var dataEncodingStrategy: JSONEncoder.DataEncodingStrategy = .base64

/// Makes new query parameters encoder
public init() { }

Expand All @@ -40,11 +46,14 @@ public final class QueryParametersEncoder: ParametersEncoding {
/// - Parameters:
/// - value: The value to encode.
///
/// - throws: `EncodingError.invalidValue` if a non-comforming floating-point value is encountered during encoding,
/// - throws: `EncodingError.invalidValue` if a non-conforming floating-point value is encountered during encoding,
/// and the encoding strategy is `.throw`.
/// An error if any value throws an error during encoding.
public func encode<T>(_ value: T) throws where T: Encodable {
let data = try JSONEncoder().encode(value)
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = dateEncodingStrategy
encoder.dataEncodingStrategy = dataEncodingStrategy
let data = try encoder.encode(value)
let parameters = try JSONSerialization.jsonObject(with: data)
self.parameters = parameters as? [String: Any] ?? [:]
}
Expand Down

0 comments on commit b69a223

Please sign in to comment.