Skip to content

Commit

Permalink
[swift6] general improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
4brunu committed Sep 27, 2024
1 parent 3d812bd commit f3dc42c
Show file tree
Hide file tree
Showing 12 changed files with 4,544 additions and 4,516 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,77 @@ import FoundationNetworking
#endif
import Alamofire

open class PetstoreClientAPI: @unchecked Sendable {
private init() {}
public static let shared = PetstoreClientAPI()
@available(*, deprecated, message: "Use `OpenAPIClient` instead")
public typealias PetstoreClientAPI = OpenAPIClient

public var basePath = "http://petstore.swagger.io:80/v2"
public var customHeaders: [String: String] = [:]
open class OpenAPIClient: @unchecked Sendable {
public var basePath: String
public var customHeaders: [String: String]
public var credential: URLCredential?
public var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
public var apiResponseQueue: DispatchQueue = .main
public var requestBuilderFactory: RequestBuilderFactory
public var apiResponseQueue: DispatchQueue
public var codableHelper: CodableHelper

/// 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 var successfulStatusCodeRange: Range = 200..<300
public var successfulStatusCodeRange: Range<Int>
/// ResponseSerializer that will be used by the generator for `Data` responses
///
/// If unchanged, Alamofires default `DataResponseSerializer` will be used.
public var dataResponseSerializer: AnyResponseSerializer<Data> = AnyResponseSerializer(DataResponseSerializer())
public var dataResponseSerializer: AnyResponseSerializer<Data>
/// ResponseSerializer that will be used by the generator for `String` responses
///
/// If unchanged, Alamofires default `StringResponseSerializer` will be used.
public var stringResponseSerializer: AnyResponseSerializer<String> = AnyResponseSerializer(StringResponseSerializer())
public var stringResponseSerializer: AnyResponseSerializer<String>

public init(
basePath: String = "http://petstore.swagger.io:80/v2",
customHeaders: [String: String] = [:],
credential: URLCredential? = nil,
requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory(),
apiResponseQueue: DispatchQueue = .main,
codableHelper: CodableHelper = CodableHelper(),
successfulStatusCodeRange: Range<Int> = 200..<300,
dataResponseSerializer: AnyResponseSerializer<Data> = AnyResponseSerializer(DataResponseSerializer()),
stringResponseSerializer: AnyResponseSerializer<String> = AnyResponseSerializer(StringResponseSerializer())
) {
self.basePath = basePath
self.customHeaders = customHeaders
self.credential = credential
self.requestBuilderFactory = requestBuilderFactory
self.apiResponseQueue = apiResponseQueue
self.codableHelper = codableHelper
self.successfulStatusCodeRange = successfulStatusCodeRange
self.dataResponseSerializer = dataResponseSerializer
self.stringResponseSerializer = stringResponseSerializer
}

public static let shared = OpenAPIClient()
}

open class RequestBuilder<T>: @unchecked Sendable {
var credential: URLCredential?
var headers: [String: String]
public var credential: URLCredential?
public var headers: [String: String]
public let parameters: [String: Any]?
public let method: String
public let URLString: String
public let requestTask: RequestTask = RequestTask()
public let requiresAuthentication: Bool
public let client: OpenAPIClient

/// Optional block to obtain a reference to the request's progress instance when available.
public var onProgressReady: ((Progress) -> Void)?

required public init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:], requiresAuthentication: Bool) {
required public init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:], requiresAuthentication: Bool, client: OpenAPIClient = OpenAPIClient.shared) {
self.method = method
self.URLString = URLString
self.parameters = parameters
self.headers = headers
self.requiresAuthentication = requiresAuthentication
self.client = client

addHeaders(PetstoreClientAPI.shared.customHeaders)
addHeaders(client.customHeaders)
}

open func addHeaders(_ aHeaders: [String: String]) {
Expand All @@ -62,7 +90,7 @@ open class RequestBuilder<T>: @unchecked Sendable {
}

@discardableResult
open func execute(_ apiResponseQueue: DispatchQueue = PetstoreClientAPI.shared.apiResponseQueue, _ completion: @Sendable @escaping (_ result: Swift.Result<Response<T>, ErrorResponse>) -> Void) -> RequestTask {
open func execute(completion: @Sendable @escaping (_ result: Swift.Result<Response<T>, ErrorResponse>) -> Void) -> RequestTask {
return requestTask
}

Expand Down Expand Up @@ -109,7 +137,7 @@ open class RequestBuilder<T>: @unchecked Sendable {
}

open func addCredential() -> Self {
credential = PetstoreClientAPI.shared.credential
credential = client.credential
return self
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ open class AnotherFakeAPI {
- parameter body: (body) client model
- returns: Promise<Client>
*/
open func call123testSpecialTags( body: Client) -> Promise<Client> {
open func call123testSpecialTags(body: Client, client: OpenAPIClient = OpenAPIClient.shared) -> Promise<Client> {
let deferred = Promise<Client>.pending()
call123testSpecialTagsWithRequestBuilder(body: body).execute { result in
call123testSpecialTagsWithRequestBuilder(body: body, client: client).execute { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body)
Expand All @@ -44,9 +44,9 @@ open class AnotherFakeAPI {
- parameter apiResponseQueue: The queue on which api response is dispatched.
- returns: Observable<Client>
*/
open func call123testSpecialTags(body: Client, apiResponseQueue: DispatchQueue = PetstoreClientAPI.shared.apiResponseQueue) -> Observable<Client> {
open func call123testSpecialTags(body: Client, client: OpenAPIClient = OpenAPIClient.shared) -> Observable<Client> {
return Observable.create { observer -> Disposable in
let requestTask = self.call123testSpecialTagsWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
let requestTask = self.call123testSpecialTagsWithRequestBuilder(body: body, client: client).execute { result in
switch result {
case let .success(response):
observer.onNext(response.body)
Expand All @@ -70,8 +70,8 @@ open class AnotherFakeAPI {
*/
#if canImport(Combine)
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
open func call123testSpecialTags(body: Client) -> AnyPublisher<Client, Error> {
let requestBuilder = call123testSpecialTagsWithRequestBuilder(body: body)
open func call123testSpecialTags(body: Client, client: OpenAPIClient = OpenAPIClient.shared) -> AnyPublisher<Client, Error> {
let requestBuilder = call123testSpecialTagsWithRequestBuilder(body: body, client: client)
let requestTask = requestBuilder.requestTask
return Deferred { Future<Client, Error> { promise in
nonisolated(unsafe) let promise = promise
Expand Down Expand Up @@ -100,8 +100,8 @@ open class AnotherFakeAPI {
- returns: Client
*/
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
open func call123testSpecialTags(body: Client) async throws(ErrorResponse) -> Client {
return try await call123testSpecialTagsWithRequestBuilder(body: body).execute().body
open func call123testSpecialTags(body: Client, client: OpenAPIClient = OpenAPIClient.shared) async throws(ErrorResponse) -> Client {
return try await call123testSpecialTagsWithRequestBuilder(body: body, client: client).execute().body
}

/**
Expand All @@ -112,8 +112,8 @@ open class AnotherFakeAPI {
- parameter completion: completion handler to receive the result
*/
@discardableResult
open func call123testSpecialTags(body: Client, apiResponseQueue: DispatchQueue = PetstoreClientAPI.shared.apiResponseQueue, completion: @Sendable @escaping (_ result: Swift.Result<Client, ErrorResponse>) -> Void) -> RequestTask {
return call123testSpecialTagsWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
open func call123testSpecialTags(body: Client, client: OpenAPIClient = OpenAPIClient.shared, completion: @Sendable @escaping (_ result: Swift.Result<Client, ErrorResponse>) -> Void) -> RequestTask {
return call123testSpecialTagsWithRequestBuilder(body: body, client: client).execute { result in
switch result {
case let .success(response):
completion(.success(response.body))
Expand All @@ -130,9 +130,9 @@ open class AnotherFakeAPI {
- parameter body: (body) client model
- returns: RequestBuilder<Client>
*/
open func call123testSpecialTagsWithRequestBuilder(body: Client) -> RequestBuilder<Client> {
open func call123testSpecialTagsWithRequestBuilder(body: Client, client: OpenAPIClient = OpenAPIClient.shared) -> RequestBuilder<Client> {
let localVariablePath = "/another-fake/dummy"
let localVariableURLString = PetstoreClientAPI.shared.basePath + localVariablePath
let localVariableURLString = client.basePath + localVariablePath
let localVariableParameters = JSONEncodingHelper.encodingParameters(forEncodableObject: body)

let localVariableUrlComponents = URLComponents(string: localVariableURLString)
Expand All @@ -143,7 +143,7 @@ open class AnotherFakeAPI {

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)

let localVariableRequestBuilder: RequestBuilder<Client>.Type = PetstoreClientAPI.shared.requestBuilderFactory.getBuilder()
let localVariableRequestBuilder: RequestBuilder<Client>.Type = client.requestBuilderFactory.getBuilder()

return localVariableRequestBuilder.init(method: "PATCH", URLString: (localVariableUrlComponents?.string ?? localVariableURLString), parameters: localVariableParameters, headers: localVariableHeaderParameters, requiresAuthentication: false)
}
Expand Down
Loading

0 comments on commit f3dc42c

Please sign in to comment.