Skip to content

Commit b075bb6

Browse files
Load example credentials from local config
1 parent df2090f commit b075bb6

1 file changed

Lines changed: 126 additions & 11 deletions

File tree

Examples/GenerateAndScan/main.swift

Lines changed: 126 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,136 @@
11
import AsposeBarcodeCloud
22
import Foundation
33

4-
let environment = ProcessInfo.processInfo.environment
5-
6-
let client: AsposeBarcodeCloudClient
7-
if let accessToken = environment["TEST_CONFIGURATION_ACCESS_TOKEN"], !accessToken.isEmpty {
8-
client = AsposeBarcodeCloudClient(accessToken: accessToken)
9-
} else if let clientId = environment["TEST_CONFIGURATION_CLIENT_ID"],
10-
let clientSecret = environment["TEST_CONFIGURATION_CLIENT_SECRET"],
11-
!clientId.isEmpty, !clientSecret.isEmpty
12-
{
13-
client = AsposeBarcodeCloudClient(clientId: clientId, clientSecret: clientSecret)
14-
} else {
4+
private enum ExampleConfiguration {
5+
private static let defaultConfigPath = "Tests/configuration.json"
6+
7+
static func load() throws -> AsposeBarcodeCloudConfiguration? {
8+
if let configuration = loadFromEnvironment(ProcessInfo.processInfo.environment) {
9+
return configuration
10+
}
11+
12+
return try loadFromFile(defaultConfigPath)
13+
}
14+
15+
private static func loadFromEnvironment(_ environment: [String: String]) -> AsposeBarcodeCloudConfiguration? {
16+
let payload = Payload(
17+
accessToken: firstValue(in: environment, names: [
18+
"TEST_CONFIGURATION_ACCESS_TOKEN",
19+
]),
20+
clientId: firstValue(in: environment, names: [
21+
"TEST_CONFIGURATION_CLIENT_ID",
22+
"ASPOSE_CLIENT_ID",
23+
]),
24+
clientSecret: firstValue(in: environment, names: [
25+
"TEST_CONFIGURATION_CLIENT_SECRET",
26+
"ASPOSE_CLIENT_SECRET",
27+
]),
28+
host: firstValue(in: environment, names: [
29+
"TEST_CONFIGURATION_HOST",
30+
"TEST_CONFIGURATION_BASE_URL",
31+
]),
32+
tokenURL: firstValue(in: environment, names: [
33+
"TEST_CONFIGURATION_TOKEN_URL",
34+
])
35+
)
36+
37+
return payload.makeConfiguration()
38+
}
39+
40+
private static func loadFromFile(_ path: String) throws -> AsposeBarcodeCloudConfiguration? {
41+
guard FileManager.default.fileExists(atPath: path),
42+
let data = FileManager.default.contents(atPath: path)
43+
else {
44+
return nil
45+
}
46+
47+
let payload = try JSONDecoder().decode(Payload.self, from: data)
48+
return payload.makeConfiguration()
49+
}
50+
51+
private static func firstValue(in environment: [String: String], names: [String]) -> String? {
52+
for name in names {
53+
if let value = environment[name], !value.isEmpty {
54+
return value
55+
}
56+
}
57+
58+
return nil
59+
}
60+
61+
private struct Payload: Decodable {
62+
let accessToken: String?
63+
let clientId: String?
64+
let clientSecret: String?
65+
let host: String?
66+
let tokenURL: String?
67+
68+
enum CodingKeys: String, CodingKey {
69+
case accessToken
70+
case clientId
71+
case clientSecret
72+
case host
73+
case baseUrl
74+
case tokenURL
75+
case tokenUrl
76+
}
77+
78+
init(
79+
accessToken: String? = nil,
80+
clientId: String? = nil,
81+
clientSecret: String? = nil,
82+
host: String? = nil,
83+
tokenURL: String? = nil
84+
) {
85+
self.accessToken = accessToken
86+
self.clientId = clientId
87+
self.clientSecret = clientSecret
88+
self.host = host
89+
self.tokenURL = tokenURL
90+
}
91+
92+
init(from decoder: Decoder) throws {
93+
let container = try decoder.container(keyedBy: CodingKeys.self)
94+
accessToken = try container.decodeIfPresent(String.self, forKey: .accessToken)
95+
clientId = try container.decodeIfPresent(String.self, forKey: .clientId)
96+
clientSecret = try container.decodeIfPresent(String.self, forKey: .clientSecret)
97+
host = try container.decodeIfPresent(String.self, forKey: .host)
98+
?? container.decodeIfPresent(String.self, forKey: .baseUrl)
99+
tokenURL = try container.decodeIfPresent(String.self, forKey: .tokenURL)
100+
?? container.decodeIfPresent(String.self, forKey: .tokenUrl)
101+
}
102+
103+
func makeConfiguration() -> AsposeBarcodeCloudConfiguration? {
104+
if let accessToken, !accessToken.isEmpty {
105+
return AsposeBarcodeCloudConfiguration(
106+
accessToken: accessToken,
107+
host: host ?? AsposeBarcodeCloudConfiguration.defaultHost,
108+
tokenURL: tokenURL ?? AsposeBarcodeCloudConfiguration.defaultTokenURL
109+
)
110+
}
111+
112+
guard let clientId, !clientId.isEmpty,
113+
let clientSecret, !clientSecret.isEmpty
114+
else {
115+
return nil
116+
}
117+
118+
return AsposeBarcodeCloudConfiguration(
119+
clientId: clientId,
120+
clientSecret: clientSecret,
121+
host: host ?? AsposeBarcodeCloudConfiguration.defaultHost,
122+
tokenURL: tokenURL ?? AsposeBarcodeCloudConfiguration.defaultTokenURL
123+
)
124+
}
125+
}
126+
}
127+
128+
guard let configuration = try ExampleConfiguration.load() else {
15129
print("Set TEST_CONFIGURATION_ACCESS_TOKEN or TEST_CONFIGURATION_CLIENT_ID/TEST_CONFIGURATION_CLIENT_SECRET.")
16130
exit(1)
17131
}
18132

133+
let client = AsposeBarcodeCloudClient(configuration: configuration)
19134
let barcodeValue = CommandLine.arguments.dropFirst().first ?? "Aspose.BarCode Cloud Swift example"
20135

21136
print("Generating QR barcode...")

0 commit comments

Comments
 (0)