[swift5] adds configuration of response success ranges (#13598)

* add successfulCodeRange to configuration

* generate samples
This commit is contained in:
Jonas Reichert 2022-10-05 14:30:33 +02:00 committed by GitHub
parent 085e1e58e5
commit 4beee6c8c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 87 additions and 23 deletions

View File

@ -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.
@ -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 {

View File

@ -190,7 +190,7 @@ extension KeyedDecodingContainerProtocol {
extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}{{/useVapor}}{{#usePromiseKit}}

View File

@ -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:
@ -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:

View File

@ -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:
@ -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:

View File

@ -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.
@ -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 {

View File

@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {
extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}

View File

@ -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
}

View File

@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {
extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}

View File

@ -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
}

View File

@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {
extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}

View File

@ -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
}

View File

@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {
extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}

View File

@ -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
}

View File

@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {
extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}

View File

@ -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
}

View File

@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {
extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}

View File

@ -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
}

View File

@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {
extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}

View File

@ -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
}

View File

@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {
extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}

View File

@ -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
}

View File

@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {
extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}

View File

@ -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
}

View File

@ -189,7 +189,7 @@ extension KeyedDecodingContainerProtocol {
extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}

View File

@ -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
}

View File

@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {
extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}

View File

@ -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
}

View File

@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {
extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}

View File

@ -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
}

View File

@ -102,6 +102,6 @@ extension JSONEncodable where Self: Encodable {
extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}

View File

@ -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
}
}

View File

@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {
extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}

View File

@ -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
}

View File

@ -188,6 +188,6 @@ extension KeyedDecodingContainerProtocol {
extension HTTPURLResponse {
var isStatusCodeSuccessful: Bool {
return (200 ..< 300).contains(statusCode)
return Configuration.successfulStatusCodeRange.contains(statusCode)
}
}