Skip to content

Commit 5f2a9fa

Browse files
Generate Swift SDK from templates
1 parent 2a551ab commit 5f2a9fa

37 files changed

Lines changed: 3589 additions & 128 deletions
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// APIHelper.swift
2+
//
3+
// Generated by openapi-generator
4+
// https://openapi-generator.tech
5+
//
6+
7+
import Foundation{{#useVapor}}
8+
import Vapor{{/useVapor}}
9+
10+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct APIHelper {
11+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
12+
let destination = source.reduce(into: [String: Any]()) { result, item in
13+
if let value = item.value {
14+
result[item.key] = value
15+
}
16+
}
17+
18+
if destination.isEmpty {
19+
return nil
20+
}
21+
return destination
22+
}
23+
24+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
25+
return source.reduce(into: [String: String]()) { result, item in
26+
if let collection = item.value as? [Any?] {
27+
result[item.key] = collection
28+
.compactMap { value in convertAnyToString(value) }
29+
.joined(separator: ",")
30+
} else if let value: Any = item.value {
31+
result[item.key] = convertAnyToString(value)
32+
}
33+
}
34+
}
35+
36+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func convertBoolToString(_ source: [String: Any]?) -> [String: Any]? {
37+
guard let source = source else {
38+
return nil
39+
}
40+
41+
return source.reduce(into: [String: Any]()) { result, item in
42+
switch item.value {
43+
case let x as Bool:
44+
result[item.key] = x.description
45+
default:
46+
result[item.key] = item.value
47+
}
48+
}
49+
}
50+
51+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func convertAnyToString(_ value: Any?) -> String? {
52+
guard let value = value else { return nil }
53+
if let value = value as? any RawRepresentable {
54+
return "\(value.rawValue)"
55+
} else {
56+
return "\(value)"
57+
}
58+
}
59+
60+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func mapValueToPathItem(_ source: Any) -> Any {
61+
if let collection = source as? [Any?] {
62+
return collection
63+
.compactMap { value in convertAnyToString(value) }
64+
.joined(separator: ",")
65+
} else if let value = source as? any RawRepresentable {
66+
return "\(value.rawValue)"
67+
}
68+
return source
69+
}
70+
71+
/// maps all values from source to query parameters
72+
///
73+
/// explode attribute is respected: collection values might be either joined or split up into separate key value pairs
74+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func mapValuesToQueryItems(_ source: [String: (wrappedValue: Any?, isExplode: Bool)]) -> [URLQueryItem]? {
75+
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
76+
if let collection = item.value.wrappedValue as? [Any?] {
77+
78+
let collectionValues: [String] = collection.compactMap { value in convertAnyToString(value) }
79+
80+
if !item.value.isExplode {
81+
result.append(URLQueryItem(name: item.key, value: collectionValues.joined(separator: ",")))
82+
} else {
83+
collectionValues
84+
.forEach { value in
85+
result.append(URLQueryItem(name: item.key, value: value))
86+
}
87+
}
88+
89+
} else if let value = item.value.wrappedValue {
90+
result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
91+
}
92+
}
93+
94+
if destination.isEmpty {
95+
return nil
96+
}
97+
return destination.sorted { $0.name < $1.name }
98+
}
99+
100+
/// maps all values from source to query parameters
101+
///
102+
/// collection values are always exploded
103+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
104+
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
105+
if let collection = item.value as? [Any?] {
106+
collection
107+
.compactMap { value in convertAnyToString(value) }
108+
.forEach { value in
109+
result.append(URLQueryItem(name: item.key, value: value))
110+
}
111+
112+
} else if let value = item.value {
113+
result.append(URLQueryItem(name: item.key, value: convertAnyToString(value)))
114+
}
115+
}
116+
117+
if destination.isEmpty {
118+
return nil
119+
}
120+
return destination.sorted { $0.name < $1.name }
121+
}
122+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// APIs.swift
2+
//
3+
// Generated by openapi-generator
4+
// https://openapi-generator.tech
5+
//
6+
7+
import Foundation
8+
#if canImport(FoundationNetworking)
9+
import FoundationNetworking
10+
#endif{{#useVapor}}
11+
import Vapor
12+
{{/useVapor}}
13+
14+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class {{projectName}}API {
15+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var basePath = "{{{basePath}}}"
16+
{{#useVapor}}
17+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var customHeaders: HTTPHeaders = [:]
18+
{{/useVapor}}
19+
{{^useVapor}}
20+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var customHeaders: [String: String] = [:]
21+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var credential: URLCredential?{{#useAlamofire}}
22+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory(){{/useAlamofire}}{{#useURLSession}}
23+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var requestBuilderFactory: RequestBuilderFactory = URLSessionRequestBuilderFactory(){{/useURLSession}}
24+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var apiResponseQueue: DispatchQueue = .main
25+
{{/useVapor}}
26+
}{{^useVapor}}
27+
28+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class RequestBuilder<T> {
29+
var credential: URLCredential?
30+
var headers: [String: String]
31+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let parameters: [String: Any]?
32+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let method: String
33+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let URLString: String
34+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let requestTask: RequestTask = RequestTask()
35+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let requiresAuthentication: Bool
36+
37+
/// Optional block to obtain a reference to the request's progress instance when available.
38+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var onProgressReady: ((Progress) -> Void)?
39+
40+
required {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:], requiresAuthentication: Bool) {
41+
self.method = method
42+
self.URLString = URLString
43+
self.parameters = parameters
44+
self.headers = headers
45+
self.requiresAuthentication = requiresAuthentication
46+
47+
addHeaders({{projectName}}API.customHeaders)
48+
}
49+
50+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func addHeaders(_ aHeaders: [String: String]) {
51+
for (header, value) in aHeaders {
52+
headers[header] = value
53+
}
54+
}
55+
56+
@discardableResult
57+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func execute(_ apiResponseQueue: DispatchQueue = {{projectName}}API.apiResponseQueue, _ completion: @escaping (_ result: Swift.Result<Response<T>, ErrorResponse>) -> Void) -> RequestTask {
58+
return requestTask
59+
}
60+
61+
{{#useAsyncAwait}}
62+
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
63+
@discardableResult
64+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func execute() async throws -> Response<T> {
65+
return try await withTaskCancellationHandler {
66+
try Task.checkCancellation()
67+
return try await withCheckedThrowingContinuation { continuation in
68+
guard !Task.isCancelled else {
69+
continuation.resume(throwing: CancellationError())
70+
return
71+
}
72+
73+
self.execute { result in
74+
switch result {
75+
case let .success(response):
76+
continuation.resume(returning: response)
77+
case let .failure(error):
78+
continuation.resume(throwing: error)
79+
}
80+
}
81+
}
82+
} onCancel: {
83+
self.requestTask.cancel()
84+
}
85+
}
86+
87+
{{/useAsyncAwait}}
88+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func addHeader(name: String, value: String) -> Self {
89+
if !value.isEmpty {
90+
headers[name] = value
91+
}
92+
return self
93+
}
94+
95+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func addCredential() -> Self {
96+
credential = {{projectName}}API.credential
97+
return self
98+
}
99+
}
100+
101+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} protocol RequestBuilderFactory {
102+
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type
103+
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type
104+
}{{/useVapor}}

codegen/Support/swift/AsposeBarcodeCloudClient.swift renamed to codegen/Templates/swift/AsposeBarcodeCloudClient.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public final class AsposeBarcodeCloudConfiguration {
3333
public static let defaultHost = "https://api.aspose.cloud/v4.0"
3434
public static let defaultTokenURL = "https://id.aspose.cloud/connect/token"
3535
public static let defaultSdkName = "swift sdk"
36-
public static let defaultSdkVersion = "26.4.0"
36+
public static let defaultSdkVersion = "{{packageVersion}}"
3737
3838
public var host: String
3939
public var tokenURL: String
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github "Flight-School/AnyCodable" ~> 0.6{{#useAlamofire}}
2+
github "Alamofire/Alamofire" ~> 5.7{{/useAlamofire}}{{#usePromiseKit}}
3+
github "mxcl/PromiseKit" ~> 6.15{{/usePromiseKit}}{{#useRxSwift}}
4+
github "ReactiveX/RxSwift" ~> 6.2{{/useRxSwift}}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// CodableHelper.swift
3+
//
4+
// Generated by openapi-generator
5+
// https://openapi-generator.tech
6+
//
7+
8+
import Foundation
9+
10+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class CodableHelper {
11+
private static var customDateFormatter: DateFormatter?
12+
private static var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter()
13+
14+
private static var customJSONDecoder: JSONDecoder?
15+
private static var defaultJSONDecoder: JSONDecoder = {
16+
let decoder = JSONDecoder()
17+
decoder.dateDecodingStrategy = .formatted(CodableHelper.dateFormatter)
18+
return decoder
19+
}()
20+
21+
private static var customJSONEncoder: JSONEncoder?
22+
private static var defaultJSONEncoder: JSONEncoder = {
23+
let encoder = JSONEncoder()
24+
encoder.dateEncodingStrategy = .formatted(CodableHelper.dateFormatter)
25+
encoder.outputFormatting = .prettyPrinted
26+
return encoder
27+
}()
28+
29+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var dateFormatter: DateFormatter {
30+
get { return customDateFormatter ?? defaultDateFormatter }
31+
set { customDateFormatter = newValue }
32+
}
33+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var jsonDecoder: JSONDecoder {
34+
get { return customJSONDecoder ?? defaultJSONDecoder }
35+
set { customJSONDecoder = newValue }
36+
}
37+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var jsonEncoder: JSONEncoder {
38+
get { return customJSONEncoder ?? defaultJSONEncoder }
39+
set { customJSONEncoder = newValue }
40+
}
41+
42+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable {
43+
return Swift.Result { try jsonDecoder.decode(type, from: data) }
44+
}
45+
46+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable {
47+
return Swift.Result { try jsonEncoder.encode(value) }
48+
}
49+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Configuration.swift
2+
//
3+
// Generated by openapi-generator
4+
// https://openapi-generator.tech
5+
//
6+
7+
import Foundation
8+
#if canImport(FoundationNetworking)
9+
import FoundationNetworking
10+
#endif{{#useVapor}}
11+
import Vapor{{/useVapor}}{{#useAlamofire}}
12+
import Alamofire{{/useAlamofire}}
13+
14+
{{#swiftUseApiNamespace}}
15+
@available(*, deprecated, renamed: "{{projectName}}API.Configuration")
16+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} typealias Configuration = {{projectName}}API.Configuration
17+
18+
extension {{projectName}}API {
19+
{{/swiftUseApiNamespace}}
20+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class Configuration {
21+
{{#useVapor}}{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var apiClient: Vapor.Client? = nil
22+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var apiWrapper: (inout Vapor.ClientRequest) throws -> () = { _ in }
23+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var contentConfiguration = ContentConfiguration.default(){{/useVapor}}{{^useVapor}}
24+
/// Configures the range of HTTP status codes that will result in a successful response
25+
///
26+
/// If a HTTP status code is outside of this range the response will be interpreted as failed.
27+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var successfulStatusCodeRange: Range = 200..<300{{/useVapor}}{{#useAlamofire}}
28+
/// ResponseSerializer that will be used by the generator for `Data` responses
29+
///
30+
/// If unchanged, Alamofires default `DataResponseSerializer` will be used.
31+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var dataResponseSerializer: AnyResponseSerializer<Data> = AnyResponseSerializer(DataResponseSerializer())
32+
/// ResponseSerializer that will be used by the generator for `String` responses
33+
///
34+
/// If unchanged, Alamofires default `StringResponseSerializer` will be used.
35+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var stringResponseSerializer: AnyResponseSerializer<String> = AnyResponseSerializer(StringResponseSerializer()){{/useAlamofire}}
36+
}
37+
{{#useAlamofire}}
38+
39+
/// Type-erased ResponseSerializer
40+
///
41+
/// This is needed in order to use `ResponseSerializer` as a Type in `Configuration`. Obsolete with `any` keyword in Swift >= 5.7
42+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct AnyResponseSerializer<T>: ResponseSerializer {
43+
44+
let _serialize: (URLRequest?, HTTPURLResponse?, Data?, Error?) throws -> T
45+
46+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init<V: ResponseSerializer>(_ delegatee: V) where V.SerializedObject == T {
47+
_serialize = delegatee.serialize
48+
}
49+
50+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> T {
51+
try _serialize(request, response, data, error)
52+
}
53+
}{{/useAlamofire}}{{#swiftUseApiNamespace}}}
54+
55+
{{/swiftUseApiNamespace}}

0 commit comments

Comments
 (0)