-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.swift
More file actions
163 lines (142 loc) · 5.65 KB
/
Copy pathmain.swift
File metadata and controls
163 lines (142 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import AsposeBarcodeCloud
import Foundation
private enum ExampleConfiguration {
private static let defaultConfigPath = "Tests/configuration.json"
static func load() throws -> AsposeBarcodeCloudConfiguration? {
if let configuration = loadFromEnvironment(ProcessInfo.processInfo.environment) {
return configuration
}
return try loadFromFile(defaultConfigPath)
}
private static func loadFromEnvironment(_ environment: [String: String]) -> AsposeBarcodeCloudConfiguration? {
let payload = Payload(
accessToken: firstValue(in: environment, names: [
"TEST_CONFIGURATION_ACCESS_TOKEN",
]),
clientId: firstValue(in: environment, names: [
"TEST_CONFIGURATION_CLIENT_ID",
"ASPOSE_CLIENT_ID",
]),
clientSecret: firstValue(in: environment, names: [
"TEST_CONFIGURATION_CLIENT_SECRET",
"ASPOSE_CLIENT_SECRET",
]),
host: firstValue(in: environment, names: [
"TEST_CONFIGURATION_HOST",
"TEST_CONFIGURATION_BASE_URL",
]),
tokenURL: firstValue(in: environment, names: [
"TEST_CONFIGURATION_TOKEN_URL",
])
)
return payload.makeConfiguration()
}
private static func loadFromFile(_ path: String) throws -> AsposeBarcodeCloudConfiguration? {
guard FileManager.default.fileExists(atPath: path),
let data = FileManager.default.contents(atPath: path)
else {
return nil
}
let payload = try JSONDecoder().decode(Payload.self, from: data)
return payload.makeConfiguration()
}
private static func firstValue(in environment: [String: String], names: [String]) -> String? {
for name in names {
if let value = environment[name], !value.isEmpty {
return value
}
}
return nil
}
private struct Payload: Decodable {
let accessToken: String?
let clientId: String?
let clientSecret: String?
let host: String?
let tokenURL: String?
enum CodingKeys: String, CodingKey {
case accessToken
case clientId
case clientSecret
case host
case baseUrl
case tokenURL
case tokenUrl
}
init(
accessToken: String? = nil,
clientId: String? = nil,
clientSecret: String? = nil,
host: String? = nil,
tokenURL: String? = nil
) {
self.accessToken = accessToken
self.clientId = clientId
self.clientSecret = clientSecret
self.host = host
self.tokenURL = tokenURL
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
accessToken = try container.decodeIfPresent(String.self, forKey: .accessToken)
clientId = try container.decodeIfPresent(String.self, forKey: .clientId)
clientSecret = try container.decodeIfPresent(String.self, forKey: .clientSecret)
host = try container.decodeIfPresent(String.self, forKey: .host)
?? container.decodeIfPresent(String.self, forKey: .baseUrl)
tokenURL = try container.decodeIfPresent(String.self, forKey: .tokenURL)
?? container.decodeIfPresent(String.self, forKey: .tokenUrl)
}
func makeConfiguration() -> AsposeBarcodeCloudConfiguration? {
if let accessToken, !accessToken.isEmpty {
return AsposeBarcodeCloudConfiguration(
accessToken: accessToken,
host: host ?? AsposeBarcodeCloudConfiguration.defaultHost,
tokenURL: tokenURL ?? AsposeBarcodeCloudConfiguration.defaultTokenURL
)
}
guard let clientId, !clientId.isEmpty,
let clientSecret, !clientSecret.isEmpty
else {
return nil
}
return AsposeBarcodeCloudConfiguration(
clientId: clientId,
clientSecret: clientSecret,
host: host ?? AsposeBarcodeCloudConfiguration.defaultHost,
tokenURL: tokenURL ?? AsposeBarcodeCloudConfiguration.defaultTokenURL
)
}
}
}
guard let configuration = try ExampleConfiguration.load() else {
print("Set TEST_CONFIGURATION_ACCESS_TOKEN or TEST_CONFIGURATION_CLIENT_ID/TEST_CONFIGURATION_CLIENT_SECRET.")
exit(1)
}
let client = AsposeBarcodeCloudClient(configuration: configuration)
let barcodeValue = CommandLine.arguments.dropFirst().first ?? "Aspose.BarCode Cloud Swift example"
print("Generating QR barcode...")
let imageData = try await GenerateAPI.generate(
barcodeType: .qr,
data: barcodeValue,
barcodeImageParams: BarcodeImageParams(imageFormat: .png),
qrParams: QrParams(
qrEncodeMode: .auto,
qrErrorLevel: .levelM,
qrVersion: .auto,
qrAspectRatio: 0.75
),
apiConfiguration: client.apiConfiguration
)
try imageData.write(to: URL(fileURLWithPath: "QR.png"))
print("Generated image saved to QR.png")
print("Scanning generated barcode...")
let response = try await ScanAPI.scanBase64(
scanBase64Request: ScanBase64Request(fileBase64: imageData.base64EncodedString()),
apiConfiguration: client.apiConfiguration
)
if let first = response.barcodes?.first {
print("Recognized type: \(first.type ?? "")")
print("Recognized value: \(first.barcodeValue ?? "")")
} else {
print("No barcodes recognized.")
}