Result+Alamofire.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // Result+Alamofire.swift
  3. //
  4. // Copyright (c) 2019 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import Foundation
  25. /// Default type of `Result` returned by Alamofire, with an `AFError` `Failure` type.
  26. public typealias AFResult<Success> = Result<Success, AFError>
  27. // MARK: - Internal APIs
  28. extension Result {
  29. /// Returns the associated value if the result is a success, `nil` otherwise.
  30. var success: Success? {
  31. guard case let .success(value) = self else { return nil }
  32. return value
  33. }
  34. /// Returns the associated error value if the result is a failure, `nil` otherwise.
  35. var failure: Failure? {
  36. guard case let .failure(error) = self else { return nil }
  37. return error
  38. }
  39. /// Initializes a `Result` from value or error. Returns `.failure` if the error is non-nil, `.success` otherwise.
  40. ///
  41. /// - Parameters:
  42. /// - value: A value.
  43. /// - error: An `Error`.
  44. init(value: Success, error: Failure?) {
  45. if let error = error {
  46. self = .failure(error)
  47. } else {
  48. self = .success(value)
  49. }
  50. }
  51. /// Evaluates the specified closure when the `Result` is a success, passing the unwrapped value as a parameter.
  52. ///
  53. /// Use the `tryMap` method with a closure that may throw an error. For example:
  54. ///
  55. /// let possibleData: Result<Data, Error> = .success(Data(...))
  56. /// let possibleObject = possibleData.tryMap {
  57. /// try JSONSerialization.jsonObject(with: $0)
  58. /// }
  59. ///
  60. /// - parameter transform: A closure that takes the success value of the instance.
  61. ///
  62. /// - returns: A `Result` containing the result of the given closure. If this instance is a failure, returns the
  63. /// same failure.
  64. func tryMap<NewSuccess>(_ transform: (Success) throws -> NewSuccess) -> Result<NewSuccess, Error> {
  65. switch self {
  66. case let .success(value):
  67. do {
  68. return try .success(transform(value))
  69. } catch {
  70. return .failure(error)
  71. }
  72. case let .failure(error):
  73. return .failure(error)
  74. }
  75. }
  76. /// Evaluates the specified closure when the `Result` is a failure, passing the unwrapped error as a parameter.
  77. ///
  78. /// Use the `tryMapError` function with a closure that may throw an error. For example:
  79. ///
  80. /// let possibleData: Result<Data, Error> = .success(Data(...))
  81. /// let possibleObject = possibleData.tryMapError {
  82. /// try someFailableFunction(taking: $0)
  83. /// }
  84. ///
  85. /// - Parameter transform: A throwing closure that takes the error of the instance.
  86. ///
  87. /// - Returns: A `Result` instance containing the result of the transform. If this instance is a success, returns
  88. /// the same success.
  89. func tryMapError<NewFailure: Error>(_ transform: (Failure) throws -> NewFailure) -> Result<Success, Error> {
  90. switch self {
  91. case let .failure(error):
  92. do {
  93. return try .failure(transform(error))
  94. } catch {
  95. return .failure(error)
  96. }
  97. case let .success(value):
  98. return .success(value)
  99. }
  100. }
  101. }