-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move CoreHTTP contents to Sources folder
Resolves: none.
- Loading branch information
1 parent
3fdaeba
commit 4305a87
Showing
22 changed files
with
674 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// DefaultHTTPAPIError.swift | ||
// RxNetworkKit | ||
// | ||
// Created by Loay Ashraf on 19/02/2023. | ||
// | ||
|
||
/// Default type used for decoding internal api error bodies. | ||
public struct DefaultHTTPAPIError: HTTPAPIError { | ||
|
||
/// error message. | ||
let message: String | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// DefaultHTTPBodyError.swift | ||
// RxNetworkKit | ||
// | ||
// Created by Loay Ashraf on 21/08/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Default type used for decoding http error bodies. | ||
public struct DefaultHTTPBodyError: HTTPBodyError { | ||
|
||
/// response status code. | ||
let statusCode: Int? | ||
/// error message. | ||
let message: String? | ||
/// support identifier. | ||
let supportId: String? | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// | ||
// HTTPAPIError.swift | ||
// RxNetworkKit | ||
// | ||
// Created by Loay Ashraf on 20/03/2023. | ||
// | ||
|
||
/// Internal api error body type. | ||
public protocol HTTPAPIError: Error, Decodable { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// | ||
// HTTPErrorBody.swift | ||
// RxNetworkKit | ||
// | ||
// Created by Loay Ashraf on 21/08/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
/// HTTP body error type. | ||
public protocol HTTPBodyError: Decodable { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// HTTPClientError.swift | ||
// RxNetworkKit | ||
// | ||
// Created by Loay Ashraf on 19/02/2023. | ||
// | ||
|
||
/// Client-side (transport) http error. | ||
public enum HTTPClientError: Error { | ||
case response(HTTPStatusCode, HTTPBodyError?) | ||
case serialization(Error) | ||
case transport(Error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// HTTPError.swift | ||
// RxNetworkKit | ||
// | ||
// Created by Loay Ashraf on 19/02/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Generic http error (client, server or api). | ||
public enum HTTPError: Error { | ||
case client(HTTPClientError) | ||
case server(HTTPServerError) | ||
case api(HTTPAPIError) | ||
|
||
/// Creates `NetworkError` instance. | ||
/// | ||
/// - Parameter response: `HTTPURLResponse` used to get response status code. | ||
public init?<E: HTTPBodyError>(_ response: HTTPURLResponse?, data: Data?, errorType: E.Type) { | ||
if let response = response, | ||
let httpStatusCode = response.status { | ||
// Get Error body from response data if possible. | ||
var httpErrorBody: E? | ||
if let data = data { | ||
httpErrorBody = try? JSONDecoder().decode(errorType.self, from: data) | ||
} | ||
// Get Error from response status code | ||
switch httpStatusCode.responseType { | ||
case .clientError: | ||
self = .client(.response(httpStatusCode, httpErrorBody)) | ||
case .serverError: | ||
self = .server(.response(httpStatusCode, httpErrorBody)) | ||
default: | ||
return nil | ||
} | ||
} else { | ||
return nil | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// HTTPServerError.swift | ||
// RxNetworkKit | ||
// | ||
// Created by Loay Ashraf on 19/02/2023. | ||
// | ||
|
||
/// Server-side http error. | ||
public enum HTTPServerError: Error { | ||
case response(HTTPStatusCode, HTTPBodyError?) | ||
case generic(Error) | ||
} |
22 changes: 22 additions & 0 deletions
22
Source/Common/Request/Interceptor/HTTPRequestAdapter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// HTTPRequestAdapter.swift | ||
// RxNetworkKit | ||
// | ||
// Created by Loay Ashraf on 27/03/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Adapts outgoing requests. | ||
public protocol HTTPRequestAdapter: AnyObject { | ||
|
||
/// Adapts a given request for a given session. | ||
/// | ||
/// - Parameters: | ||
/// - request: current `URLRequest` | ||
/// - session: current `URLSession` | ||
/// | ||
/// - Returns: `URLRequest` adapted request for a given session. | ||
func adapt(_ request: URLRequest, for session: URLSession) -> URLRequest | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
Source/Common/Request/Interceptor/HTTPRequestInterceptor.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// | ||
// HTTPRequestInterceptor.swift | ||
// RxNetworkKit | ||
// | ||
// Created by Loay Ashraf on 27/03/2023. | ||
// | ||
|
||
/// Request adapter and retrier. | ||
public typealias HTTPRequestInterceptor = HTTPRequestAdapter & HTTPRequestRetrier |
41 changes: 41 additions & 0 deletions
41
Source/Common/Request/Interceptor/HTTPRequestRetrier.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// HTTPRequestRetrier.swift | ||
// RxNetworkKit | ||
// | ||
// Created by Loay Ashraf on 27/03/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Retries requests upon failure. | ||
public protocol HTTPRequestRetrier: AnyObject { | ||
|
||
/// Provides maximum retry attempts for a given request and session. | ||
/// | ||
/// - Parameters: | ||
/// - request: current `URLRequest` | ||
/// - session: current `URLSession` | ||
/// | ||
/// - Returns: `Int` maximum retry attempts for given request and session. | ||
func retryMaxAttempts(_ request: URLRequest, for session: URLSession) -> Int | ||
|
||
/// Provides retry policy for a given request and session. | ||
/// | ||
/// - Parameters: | ||
/// - request: current `URLRequest` | ||
/// - session: current `URLSession` | ||
/// | ||
/// - Returns: `NetworkRequestRetryPolicy` retry policy for given request and session. | ||
func retryPolicy(_ request: URLRequest, for session: URLSession) -> HTTPRequestRetryPolicy | ||
|
||
/// Decides if a given request should be reried for a given session and error. | ||
/// | ||
/// - Parameters: | ||
/// - request: current `URLRequest` | ||
/// - session: current `URLSession` | ||
/// - error: encountered `NetworkError` | ||
/// | ||
/// - Returns: `NetworkRequestRetryPolicy` retry policy for given request and session. | ||
func shouldRetry(_ request: URLRequest, for session: URLSession, dueTo error: HTTPError) -> Bool | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
Source/Common/Request/Interceptor/HTTPRequestRetryPolicy.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// HTTPRequestRetryPolicy.swift | ||
// RxNetworkKit | ||
// | ||
// Created by Loay Ashraf on 27/03/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
// This enum is inspired by Alex Grebenyuk excellent blog https://kean.blog/post/smart-retry | ||
// Here's Alex's twitter: https://twitter.com/a_grebenyuk | ||
|
||
/// Policy for retrying failed requests. | ||
public enum HTTPRequestRetryPolicy { | ||
case immediate | ||
case constant(time: Double) | ||
case exponential(initial: Double, multiplier: Double, maxDelay: Double) | ||
case custom(closure: (Int) -> Double) | ||
} | ||
|
||
public extension HTTPRequestRetryPolicy { | ||
/// Creates time interavel (`Double`) from current policy and given attempt count. | ||
/// | ||
/// - Parameter attempt: current attempt count. | ||
/// | ||
/// - Returns: Time interval `Double` for delay. | ||
func makeTimeInterval(_ attempt: Int) -> Double { | ||
switch self { | ||
case .immediate: return 0.0 | ||
case .constant(let time): return time | ||
case .exponential(let initial, let multiplier, let maxDelay): | ||
// if it's first attempt, simply use initial delay, otherwise calculate delay | ||
let delay = attempt == 1 ? initial : initial * pow(multiplier, Double(attempt - 1)) | ||
return min(maxDelay, delay) | ||
case .custom(let closure): return closure(attempt) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// HTTPMethod.swift | ||
// RxNetworkKit | ||
// | ||
// Created by Loay Ashraf on 20/03/2023. | ||
// | ||
|
||
/// An enumeration of the types of http methods. | ||
public enum HTTPMethod: String { | ||
case get = "GET" | ||
case put = "PUT" | ||
case post = "POST" | ||
case delete = "DELETE" | ||
case head = "HEAD" | ||
case options = "OPTIONS" | ||
case trace = "TRACE" | ||
case connect = "CONNECT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// HTTPScheme.swift | ||
// RxNetworkKit | ||
// | ||
// Created by Loay Ashraf on 20/03/2023. | ||
// | ||
|
||
/// An enumeration of the types of http schemes. | ||
public enum HTTPScheme: String { | ||
case http = "http://" | ||
case https = "https://" | ||
} |
Oops, something went wrong.