Skip to content

Commit

Permalink
[PR #2691] Simplified and refactored queue and switch logic throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
jdisho authored and cnoon committed Mar 26, 2019
1 parent 4dea903 commit cf34434
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 156 deletions.
96 changes: 24 additions & 72 deletions Source/AFError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,22 +294,14 @@ extension AFError {
extension AFError {
/// The `URLConvertible` associated with the error.
public var urlConvertible: URLConvertible? {
switch self {
case .invalidURL(let url):
return url
default:
return nil
}
guard case .invalidURL(let url) = self else { return nil }
return url
}

/// The `URL` associated with the error.
public var url: URL? {
switch self {
case .multipartEncodingFailed(let reason):
return reason.url
default:
return nil
}
guard case .multipartEncodingFailed(let reason) = self else { return nil }
return reason.url
}

/// The underlying `Error` responsible for generating the failure associated with `.sessionInvalidated`,
Expand Down Expand Up @@ -338,64 +330,40 @@ extension AFError {

/// The acceptable `Content-Type`s of a `.responseValidationFailed` error.
public var acceptableContentTypes: [String]? {
switch self {
case .responseValidationFailed(let reason):
return reason.acceptableContentTypes
default:
return nil
}
guard case .responseValidationFailed(let reason) = self else { return nil }
return reason.acceptableContentTypes
}

/// The response `Content-Type` of a `.responseValidationFailed` error.
public var responseContentType: String? {
switch self {
case .responseValidationFailed(let reason):
return reason.responseContentType
default:
return nil
}
guard case .responseValidationFailed(let reason) = self else { return nil }
return reason.responseContentType
}

/// The response code of a `.responseValidationFailed` error.
public var responseCode: Int? {
switch self {
case .responseValidationFailed(let reason):
return reason.responseCode
default:
return nil
}
guard case .responseValidationFailed(let reason) = self else { return nil }
return reason.responseCode
}

/// The `String.Encoding` associated with a failed `.stringResponse()` call.
public var failedStringEncoding: String.Encoding? {
switch self {
case .responseSerializationFailed(let reason):
return reason.failedStringEncoding
default:
return nil
}
guard case .responseSerializationFailed(let reason) = self else { return nil }
return reason.failedStringEncoding
}
}

extension AFError.ParameterEncodingFailureReason {
var underlyingError: Error? {
switch self {
case .jsonEncodingFailed(let error):
return error
default:
return nil
}
guard case .jsonEncodingFailed(let error) = self else { return nil }
return error
}
}

extension AFError.ParameterEncoderFailureReason {
var underlyingError: Error? {
switch self {
case .encoderFailed(let error):
return error
default:
return nil
}
guard case .encoderFailed(let error) = self else { return nil }
return error
}
}

Expand Down Expand Up @@ -435,41 +403,25 @@ extension AFError.ResponseValidationFailureReason {
}

var responseContentType: String? {
switch self {
case .unacceptableContentType(_, let responseType):
return responseType
default:
return nil
}
guard case .unacceptableContentType(_, let responseType) = self else { return nil }
return responseType
}

var responseCode: Int? {
switch self {
case .unacceptableStatusCode(let code):
return code
default:
return nil
}
guard case .unacceptableStatusCode(let code) = self else { return nil }
return code
}
}

extension AFError.ResponseSerializationFailureReason {
var failedStringEncoding: String.Encoding? {
switch self {
case .stringSerializationFailed(let encoding):
return encoding
default:
return nil
}
guard case .stringSerializationFailed(let encoding) = self else { return nil }
return encoding
}

var underlyingError: Error? {
switch self {
case .jsonSerializationFailed(let error):
return error
default:
return nil
}
guard case .jsonSerializationFailed(let error) = self else { return nil }
return error
}
}

Expand Down
33 changes: 10 additions & 23 deletions Source/ParameterEncoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ public struct URLEncoding: ParameterEncoding {
/// - httpBody: Sets encoded query string result as the HTTP body of the URL request.
public enum Destination {
case methodDependent, queryString, httpBody

func encodesParametersInURL(for method: HTTPMethod) -> Bool {
switch self {
case .methodDependent: return [.get, .head, .delete].contains(method)
case .queryString: return true
case .httpBody: return false
}
}
}

/// Configures how `Array` parameters are encoded.
Expand Down Expand Up @@ -108,12 +116,9 @@ public struct URLEncoding: ParameterEncoding {

// MARK: Properties

/// Returns a default `URLEncoding` instance.
/// Returns a default `URLEncoding` instance with a `.methodDependent` destination.
public static var `default`: URLEncoding { return URLEncoding() }

/// Returns a `URLEncoding` instance with a `.methodDependent` destination.
public static var methodDependent: URLEncoding { return URLEncoding() }

/// Returns a `URLEncoding` instance with a `.queryString` destination.
public static var queryString: URLEncoding { return URLEncoding(destination: .queryString) }

Expand Down Expand Up @@ -159,7 +164,7 @@ public struct URLEncoding: ParameterEncoding {

guard let parameters = parameters else { return urlRequest }

if let method = HTTPMethod(rawValue: urlRequest.httpMethod ?? "GET"), encodesParametersInURL(with: method) {
if let method = HTTPMethod(rawValue: urlRequest.httpMethod ?? "GET"), destination.encodesParametersInURL(for: method) {
guard let url = urlRequest.url else {
throw AFError.parameterEncodingFailed(reason: .missingURL)
}
Expand Down Expand Up @@ -230,24 +235,6 @@ public struct URLEncoding: ParameterEncoding {
}
return components.map { "\($0)=\($1)" }.joined(separator: "&")
}

private func encodesParametersInURL(with method: HTTPMethod) -> Bool {
switch destination {
case .queryString:
return true
case .httpBody:
return false
default:
break
}

switch method {
case .get, .head, .delete:
return true
default:
return false
}
}
}

// MARK: -
Expand Down
Loading

0 comments on commit cf34434

Please sign in to comment.