Skip to content

Commit 5f1f47d

Browse files
authored
[swift5] Abstract away URLSession (#11651) (#12110)
1 parent 6a77660 commit 5f1f47d

File tree

15 files changed

+120
-30
lines changed

15 files changed

+120
-30
lines changed

modules/openapi-generator/src/main/resources/swift5/libraries/urlsession/URLSessionImplementations.mustache

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import Foundation
99
import MobileCoreServices
1010
#endif
1111

12+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} protocol URLSessionProtocol {
13+
func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
14+
}
15+
16+
extension URLSession: URLSessionProtocol {}
17+
1218
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
1319
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
1420
return URLSessionRequestBuilder<T>.self
@@ -57,7 +63,7 @@ private var credentialStore = SynchronizedDictionary<Int, URLCredential>()
5763
May be overridden by a subclass if you want to control the URLSession
5864
configuration.
5965
*/
60-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func createURLSession() -> URLSession {
66+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func createURLSession() -> URLSessionProtocol {
6167
return defaultURLSession
6268
}
6369

@@ -76,7 +82,7 @@ private var credentialStore = SynchronizedDictionary<Int, URLCredential>()
7682
May be overridden by a subclass if you want to control the URLRequest
7783
configuration (e.g. to override the cache policy).
7884
*/
79-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func createURLRequest(urlSession: URLSession, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
85+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func createURLRequest(urlSession: URLSessionProtocol, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
8086
8187
guard let url = URL(string: URLString) else {
8288
throw DownloadException.requestMissingURL

samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import Foundation
99
import MobileCoreServices
1010
#endif
1111

12+
public protocol URLSessionProtocol {
13+
func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
14+
}
15+
16+
extension URLSession: URLSessionProtocol {}
17+
1218
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
1319
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
1420
return URLSessionRequestBuilder<T>.self
@@ -57,7 +63,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
5763
May be overridden by a subclass if you want to control the URLSession
5864
configuration.
5965
*/
60-
open func createURLSession() -> URLSession {
66+
open func createURLSession() -> URLSessionProtocol {
6167
return defaultURLSession
6268
}
6369

@@ -76,7 +82,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
7682
May be overridden by a subclass if you want to control the URLRequest
7783
configuration (e.g. to override the cache policy).
7884
*/
79-
open func createURLRequest(urlSession: URLSession, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
85+
open func createURLRequest(urlSession: URLSessionProtocol, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
8086

8187
guard let url = URL(string: URLString) else {
8288
throw DownloadException.requestMissingURL

samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import Foundation
99
import MobileCoreServices
1010
#endif
1111

12+
public protocol URLSessionProtocol {
13+
func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
14+
}
15+
16+
extension URLSession: URLSessionProtocol {}
17+
1218
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
1319
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
1420
return URLSessionRequestBuilder<T>.self
@@ -57,7 +63,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
5763
May be overridden by a subclass if you want to control the URLSession
5864
configuration.
5965
*/
60-
open func createURLSession() -> URLSession {
66+
open func createURLSession() -> URLSessionProtocol {
6167
return defaultURLSession
6268
}
6369

@@ -76,7 +82,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
7682
May be overridden by a subclass if you want to control the URLRequest
7783
configuration (e.g. to override the cache policy).
7884
*/
79-
open func createURLRequest(urlSession: URLSession, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
85+
open func createURLRequest(urlSession: URLSessionProtocol, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
8086

8187
guard let url = URL(string: URLString) else {
8288
throw DownloadException.requestMissingURL

samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import Foundation
99
import MobileCoreServices
1010
#endif
1111

12+
public protocol URLSessionProtocol {
13+
func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
14+
}
15+
16+
extension URLSession: URLSessionProtocol {}
17+
1218
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
1319
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
1420
return URLSessionRequestBuilder<T>.self
@@ -57,7 +63,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
5763
May be overridden by a subclass if you want to control the URLSession
5864
configuration.
5965
*/
60-
open func createURLSession() -> URLSession {
66+
open func createURLSession() -> URLSessionProtocol {
6167
return defaultURLSession
6268
}
6369

@@ -76,7 +82,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
7682
May be overridden by a subclass if you want to control the URLRequest
7783
configuration (e.g. to override the cache policy).
7884
*/
79-
open func createURLRequest(urlSession: URLSession, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
85+
open func createURLRequest(urlSession: URLSessionProtocol, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
8086

8187
guard let url = URL(string: URLString) else {
8288
throw DownloadException.requestMissingURL

samples/client/petstore/swift5/deprecated/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import Foundation
99
import MobileCoreServices
1010
#endif
1111

12+
public protocol URLSessionProtocol {
13+
func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
14+
}
15+
16+
extension URLSession: URLSessionProtocol {}
17+
1218
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
1319
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
1420
return URLSessionRequestBuilder<T>.self
@@ -57,7 +63,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
5763
May be overridden by a subclass if you want to control the URLSession
5864
configuration.
5965
*/
60-
open func createURLSession() -> URLSession {
66+
open func createURLSession() -> URLSessionProtocol {
6167
return defaultURLSession
6268
}
6369

@@ -76,7 +82,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
7682
May be overridden by a subclass if you want to control the URLRequest
7783
configuration (e.g. to override the cache policy).
7884
*/
79-
open func createURLRequest(urlSession: URLSession, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
85+
open func createURLRequest(urlSession: URLSessionProtocol, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
8086

8187
guard let url = URL(string: URLString) else {
8288
throw DownloadException.requestMissingURL

samples/client/petstore/swift5/frozenEnums/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import Foundation
99
import MobileCoreServices
1010
#endif
1111

12+
public protocol URLSessionProtocol {
13+
func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
14+
}
15+
16+
extension URLSession: URLSessionProtocol {}
17+
1218
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
1319
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
1420
return URLSessionRequestBuilder<T>.self
@@ -57,7 +63,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
5763
May be overridden by a subclass if you want to control the URLSession
5864
configuration.
5965
*/
60-
open func createURLSession() -> URLSession {
66+
open func createURLSession() -> URLSessionProtocol {
6167
return defaultURLSession
6268
}
6369

@@ -76,7 +82,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
7682
May be overridden by a subclass if you want to control the URLRequest
7783
configuration (e.g. to override the cache policy).
7884
*/
79-
open func createURLRequest(urlSession: URLSession, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
85+
open func createURLRequest(urlSession: URLSessionProtocol, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
8086

8187
guard let url = URL(string: URLString) else {
8288
throw DownloadException.requestMissingURL

samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import Foundation
99
import MobileCoreServices
1010
#endif
1111

12+
internal protocol URLSessionProtocol {
13+
func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
14+
}
15+
16+
extension URLSession: URLSessionProtocol {}
17+
1218
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
1319
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
1420
return URLSessionRequestBuilder<T>.self
@@ -57,7 +63,7 @@ internal class URLSessionRequestBuilder<T>: RequestBuilder<T> {
5763
May be overridden by a subclass if you want to control the URLSession
5864
configuration.
5965
*/
60-
internal func createURLSession() -> URLSession {
66+
internal func createURLSession() -> URLSessionProtocol {
6167
return defaultURLSession
6268
}
6369

@@ -76,7 +82,7 @@ internal class URLSessionRequestBuilder<T>: RequestBuilder<T> {
7682
May be overridden by a subclass if you want to control the URLRequest
7783
configuration (e.g. to override the cache policy).
7884
*/
79-
internal func createURLRequest(urlSession: URLSession, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
85+
internal func createURLRequest(urlSession: URLSessionProtocol, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
8086

8187
guard let url = URL(string: URLString) else {
8288
throw DownloadException.requestMissingURL

samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import Foundation
99
import MobileCoreServices
1010
#endif
1111

12+
public protocol URLSessionProtocol {
13+
func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
14+
}
15+
16+
extension URLSession: URLSessionProtocol {}
17+
1218
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
1319
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
1420
return URLSessionRequestBuilder<T>.self
@@ -57,7 +63,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
5763
May be overridden by a subclass if you want to control the URLSession
5864
configuration.
5965
*/
60-
open func createURLSession() -> URLSession {
66+
open func createURLSession() -> URLSessionProtocol {
6167
return defaultURLSession
6268
}
6369

@@ -76,7 +82,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
7682
May be overridden by a subclass if you want to control the URLRequest
7783
configuration (e.g. to override the cache policy).
7884
*/
79-
open func createURLRequest(urlSession: URLSession, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
85+
open func createURLRequest(urlSession: URLSessionProtocol, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
8086

8187
guard let url = URL(string: URLString) else {
8288
throw DownloadException.requestMissingURL

samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import Foundation
99
import MobileCoreServices
1010
#endif
1111

12+
public protocol URLSessionProtocol {
13+
func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
14+
}
15+
16+
extension URLSession: URLSessionProtocol {}
17+
1218
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
1319
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
1420
return URLSessionRequestBuilder<T>.self
@@ -57,7 +63,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
5763
May be overridden by a subclass if you want to control the URLSession
5864
configuration.
5965
*/
60-
open func createURLSession() -> URLSession {
66+
open func createURLSession() -> URLSessionProtocol {
6167
return defaultURLSession
6268
}
6369

@@ -76,7 +82,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
7682
May be overridden by a subclass if you want to control the URLRequest
7783
configuration (e.g. to override the cache policy).
7884
*/
79-
open func createURLRequest(urlSession: URLSession, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
85+
open func createURLRequest(urlSession: URLSessionProtocol, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
8086

8187
guard let url = URL(string: URLString) else {
8288
throw DownloadException.requestMissingURL

samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/URLSessionImplementations.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import Foundation
99
import MobileCoreServices
1010
#endif
1111

12+
public protocol URLSessionProtocol {
13+
func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask
14+
}
15+
16+
extension URLSession: URLSessionProtocol {}
17+
1218
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
1319
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
1420
return URLSessionRequestBuilder<T>.self
@@ -57,7 +63,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
5763
May be overridden by a subclass if you want to control the URLSession
5864
configuration.
5965
*/
60-
open func createURLSession() -> URLSession {
66+
open func createURLSession() -> URLSessionProtocol {
6167
return defaultURLSession
6268
}
6369

@@ -76,7 +82,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
7682
May be overridden by a subclass if you want to control the URLRequest
7783
configuration (e.g. to override the cache policy).
7884
*/
79-
open func createURLRequest(urlSession: URLSession, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
85+
open func createURLRequest(urlSession: URLSessionProtocol, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) throws -> URLRequest {
8086

8187
guard let url = URL(string: URLString) else {
8288
throw DownloadException.requestMissingURL

0 commit comments

Comments
 (0)