Skip to content

Commit

Permalink
feat(async): add support for async-await calls
Browse files Browse the repository at this point in the history
- swift 5.7 is required
- implements / fixes #1
- auto-generated with openapi-generator, extended with custom modifications
- added new improvements from the newest openapi-generator to underlying request classes
- deprecated txs api endpoints removed
  • Loading branch information
ph4r05 committed Jun 19, 2022
1 parent d5bf4a6 commit ee6dd92
Show file tree
Hide file tree
Showing 46 changed files with 2,690 additions and 1,070 deletions.
11 changes: 6 additions & 5 deletions BlockfrostSwiftSDK.podspec
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Pod::Spec.new do |s|
s.name = 'BlockfrostSwiftSDK'
s.ios.deployment_target = '13.0'
s.osx.deployment_target = '10.15'
s.tvos.deployment_target = '13.0'
s.watchos.deployment_target = '6.0'
s.version = '0.0.7'
s.ios.deployment_target = '15.0'
s.osx.deployment_target = '12.0'
s.tvos.deployment_target = '15.0'
s.watchos.deployment_target = '8.0'
s.swift_version = '5.7'
s.version = '1.0.0'
s.source = { :git => 'https://github.com/blockfrost/blockfrost-swift.git', :tag => s.version.to_s }
s.authors = 'Blockfrost Swift SDK'
s.license = 'MIT'
Expand Down
2 changes: 1 addition & 1 deletion BlockfrostSwiftSDK/Classes/BuildInfo.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Foundation
public class BuildInfo {
public static let VERSION="0.0.7"
public static let VERSION="1.0.0"
}
53 changes: 50 additions & 3 deletions BlockfrostSwiftSDK/Classes/OpenAPIs/APIs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import Foundation
import Alamofire

//@available(*, deprecated, renamed: "BlockfrostSDK")
//public typealias OpenAPIClientAPI = BlockfrostSDK

open class BlockfrostConfig {
public static let USER_AGENT = "BlockfrostSwiftSDK"
public static let URL_MAINNET = "https://cardano-mainnet.blockfrost.io/api/v0"
Expand Down Expand Up @@ -158,6 +155,7 @@ open class BlockfrostRetryPolicy : RetryPolicy {
}

open class APIRequest {
@discardableResult
open func cancel() -> Bool { false }
}

Expand Down Expand Up @@ -193,6 +191,7 @@ open class RequestBuilder<T> {
}
}

@discardableResult
open func execute(_ apiResponseQueue: DispatchQueue = BlockfrostConfig.shared().apiResponseQueue,
_ completion: @escaping (_ result: Swift.Result<Response<T>, Error>) -> Void) -> APIRequest {
fatalError()
Expand All @@ -216,12 +215,60 @@ public protocol RequestBuilderFactory {
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type
}

public typealias AsyncRequestBuilder<T> = (_ completion: @escaping (_ result: Swift.Result<Response<T>, Error>) -> Void) -> APIRequest

open class BaseService {
var config: BlockfrostConfig = BlockfrostConfig.shared()

public init(config: BlockfrostConfig? = nil) {
self.config = config ?? BlockfrostConfig.shared()
}

@discardableResult
open func completionWrapper<T>(
_ apiResponseQueue: DispatchQueue? = nil,
completion: @escaping (_ result: Swift.Result<T, Error>) -> Void,
requestBuilder: () -> RequestBuilder<T>
) -> APIRequest {
requestBuilder().execute(apiResponseQueue ?? config.apiResponseQueue) { result -> Void in
switch result {
case let .success(response):
completion(.success(response.body!))
case let .failure(error):
completion(.failure(error))
}
}
}

@discardableResult
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
open func asyncWrapper<T>(
_ requestCreator: AsyncRequestBuilder<T>
) async throws -> T {
var request: APIRequest?
return try await withTaskCancellationHandler {
try Task.checkCancellation()
return try await withCheckedThrowingContinuation { continuation in
guard !Task.isCancelled else {
continuation.resume(throwing: CancellationError())
return
}

let completion: (_ result: Swift.Result<Response<T>, Error>) -> Void = { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
case let .failure(error):
continuation.resume(throwing: error)
}
}

request = requestCreator(completion)
}
} onCancel: { [request] in
request?.cancel()
}
}
}

public enum SortOrder: String, CaseIterable {
Expand Down

0 comments on commit ee6dd92

Please sign in to comment.