Skip to content

Commit

Permalink
[swift5] adds configuration of response success ranges (#13598)
Browse files Browse the repository at this point in the history
* add successfulCodeRange to configuration

* generate samples
  • Loading branch information
Jonas1893 authored Oct 5, 2022
1 parent 085e1e5 commit 4beee6c
Show file tree
Hide file tree
Showing 34 changed files with 87 additions and 23 deletions.
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)
}
}

0 comments on commit 4beee6c

Please sign in to comment.