Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[swift5] adds configuration of response success ranges #13598

Merged
merged 2 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ extension {{projectName}}API {
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
// You must set it prior to encoding any dates, and it will only be read once.
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"{{/useVapor}}{{#useAlamofire}}
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
/// Configures the range of HTTP status codes that will result in a successful response
///
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var successfulStatusCodeRange: Range = 200..<300{{/useVapor}}{{#useAlamofire}}
/// ResponseSerializer that will be used by the generator for `Data` responses
///
/// If unchanged, Alamofires default `DataResponseSerializer` will be used.
Expand All @@ -33,7 +37,7 @@ extension {{projectName}}API {
}
{{#useAlamofire}}

/// Type-erased response serializer
/// Type-erased ResponseSerializer
///
/// This is needed in order to use `ResponseSerializer` as a Type in `Configuration`. Obsolete with `any` keyword in Swift >= 5.7
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct AnyResponseSerializer<T>: ResponseSerializer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ extension KeyedDecodingContainerProtocol {

extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}{{/useVapor}}{{#usePromiseKit}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.Session>()
managerStore[managerId] = nil
}

let validatedRequest = request.validate()
let validatedRequest = request.validate(statusCode: Configuration.successfulStatusCodeRange)

switch T.self {
case is Void.Type:
Expand Down Expand Up @@ -259,7 +259,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.Session>()
managerStore[managerId] = nil
}

let validatedRequest = request.validate()
let validatedRequest = request.validate(statusCode: Configuration.successfulStatusCodeRange)

switch T.self {
case is String.Type:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
managerStore[managerId] = nil
}

let validatedRequest = request.validate()
let validatedRequest = request.validate(statusCode: Configuration.successfulStatusCodeRange)

switch T.self {
case is Void.Type:
Expand Down Expand Up @@ -259,7 +259,7 @@ open class AlamofireDecodableRequestBuilder<T: Decodable>: AlamofireRequestBuild
managerStore[managerId] = nil
}

let validatedRequest = request.validate()
let validatedRequest = request.validate(statusCode: Configuration.successfulStatusCodeRange)

switch T.self {
case is String.Type:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ open class Configuration {
// You must set it prior to encoding any dates, and it will only be read once.
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
/// Configures the range of HTTP status codes that will result in a successful response
///
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
public static var successfulStatusCodeRange: Range = 200..<300
/// ResponseSerializer that will be used by the generator for `Data` responses
///
/// If unchanged, Alamofires default `DataResponseSerializer` will be used.
Expand All @@ -23,7 +27,7 @@ open class Configuration {
public static var stringResponseSerializer: AnyResponseSerializer<String> = AnyResponseSerializer(StringResponseSerializer())
}

/// Type-erased response serializer
/// Type-erased ResponseSerializer
///
/// This is needed in order to use `ResponseSerializer` as a Type in `Configuration`. Obsolete with `any` keyword in Swift >= 5.7
public struct AnyResponseSerializer<T>: ResponseSerializer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {

extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ open class Configuration {
// You must set it prior to encoding any dates, and it will only be read once.
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
/// Configures the range of HTTP status codes that will result in a successful response
///
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
public static var successfulStatusCodeRange: Range = 200..<300
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {

extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ open class Configuration {
// You must set it prior to encoding any dates, and it will only be read once.
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
/// Configures the range of HTTP status codes that will result in a successful response
///
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
public static var successfulStatusCodeRange: Range = 200..<300
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {

extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ open class Configuration {
// You must set it prior to encoding any dates, and it will only be read once.
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
/// Configures the range of HTTP status codes that will result in a successful response
///
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
public static var successfulStatusCodeRange: Range = 200..<300
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {

extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ open class Configuration {
// You must set it prior to encoding any dates, and it will only be read once.
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
/// Configures the range of HTTP status codes that will result in a successful response
///
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
public static var successfulStatusCodeRange: Range = 200..<300
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {

extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ open class Configuration {
// You must set it prior to encoding any dates, and it will only be read once.
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
/// Configures the range of HTTP status codes that will result in a successful response
///
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
public static var successfulStatusCodeRange: Range = 200..<300
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {

extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ internal class Configuration {
// You must set it prior to encoding any dates, and it will only be read once.
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
internal static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
/// Configures the range of HTTP status codes that will result in a successful response
///
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
internal static var successfulStatusCodeRange: Range = 200..<300
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {

extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ open class Configuration {
// You must set it prior to encoding any dates, and it will only be read once.
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
/// Configures the range of HTTP status codes that will result in a successful response
///
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
public static var successfulStatusCodeRange: Range = 200..<300
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {

extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ open class Configuration {
// You must set it prior to encoding any dates, and it will only be read once.
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
/// Configures the range of HTTP status codes that will result in a successful response
///
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
public static var successfulStatusCodeRange: Range = 200..<300
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {

extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ open class Configuration {
// You must set it prior to encoding any dates, and it will only be read once.
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
/// Configures the range of HTTP status codes that will result in a successful response
///
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
public static var successfulStatusCodeRange: Range = 200..<300
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ extension KeyedDecodingContainerProtocol {

extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ open class Configuration {
// You must set it prior to encoding any dates, and it will only be read once.
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
/// Configures the range of HTTP status codes that will result in a successful response
///
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
public static var successfulStatusCodeRange: Range = 200..<300
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {

extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ open class Configuration {
// You must set it prior to encoding any dates, and it will only be read once.
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
/// Configures the range of HTTP status codes that will result in a successful response
///
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
public static var successfulStatusCodeRange: Range = 200..<300
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {

extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ open class Configuration {
// You must set it prior to encoding any dates, and it will only be read once.
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
/// Configures the range of HTTP status codes that will result in a successful response
///
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
public static var successfulStatusCodeRange: Range = 200..<300
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ extension JSONEncodable where Self: Encodable {

extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ open class Configuration {
// You must set it prior to encoding any dates, and it will only be read once.
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
/// Configures the range of HTTP status codes that will result in a successful response
///
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
public static var successfulStatusCodeRange: Range = 200..<300
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {

extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ open class Configuration {
// You must set it prior to encoding any dates, and it will only be read once.
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
/// Configures the range of HTTP status codes that will result in a successful response
///
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
public static var successfulStatusCodeRange: Range = 200..<300
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {

extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}