Skip to content

Commit 39a7df5

Browse files
refactor: use env enum
1 parent 2990a63 commit 39a7df5

File tree

13 files changed

+72
-157
lines changed

13 files changed

+72
-157
lines changed

BluxClient.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'BluxClient'
11-
s.version = '0.2.12'
11+
s.version = '0.3.0'
1212
s.summary = 'Blux iOS SDK.'
1313

1414
s.homepage = 'https://github.com/zaikorea/Blux-iOS-SDK.git'

BluxClient/Classes/BluxClient.swift

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// Copyright © 2024 Blux. All rights reserved.
66
//
77

8-
import UIKit
9-
108
// Used in setUserProperties, setCustomUserProperties
119
struct PropertiesWrapper<T: Codable>: Codable {
1210
var properties: T
@@ -89,15 +87,11 @@ struct PropertiesWrapper<T: Codable>: Codable {
8987

9088
/// Set userId of the device
9189
@objc public static func signIn(userId: String) {
92-
guard let clientId = SdkConfig.clientIdInUserDefaults else {
93-
return
94-
}
95-
guard let bluxId = SdkConfig.bluxIdInUserDefaults else {
96-
return
97-
}
98-
guard let deviceId = SdkConfig.deviceIdInUserDefaults else {
99-
return
100-
}
90+
guard
91+
let clientId = SdkConfig.clientIdInUserDefaults,
92+
let bluxId = SdkConfig.bluxIdInUserDefaults,
93+
let deviceId = SdkConfig.deviceIdInUserDefaults
94+
else { return }
10195

10296
let body = DeviceService.getBluxDeviceInfo()
10397
body.userId = userId
@@ -123,15 +117,11 @@ struct PropertiesWrapper<T: Codable>: Codable {
123117

124118
/// Signout from the device
125119
@objc public static func signOut() {
126-
guard let clientId = SdkConfig.clientIdInUserDefaults else {
127-
return
128-
}
129-
guard let bluxId = SdkConfig.bluxIdInUserDefaults else {
130-
return
131-
}
132-
guard let deviceId = SdkConfig.deviceIdInUserDefaults else {
133-
return
134-
}
120+
guard
121+
let clientId = SdkConfig.clientIdInUserDefaults,
122+
let bluxId = SdkConfig.bluxIdInUserDefaults,
123+
let deviceId = SdkConfig.deviceIdInUserDefaults
124+
else { return }
135125

136126
let body = DeviceService.getBluxDeviceInfo()
137127
body.deviceId = deviceId
@@ -155,12 +145,10 @@ struct PropertiesWrapper<T: Codable>: Codable {
155145
}
156146

157147
public static func setUserProperties(userProperties: UserProperties) {
158-
guard let clientId = SdkConfig.clientIdInUserDefaults else {
159-
return
160-
}
161-
guard let bluxId = SdkConfig.bluxIdInUserDefaults else {
162-
return
163-
}
148+
guard
149+
let clientId = SdkConfig.clientIdInUserDefaults,
150+
let bluxId = SdkConfig.bluxIdInUserDefaults
151+
else { return }
164152

165153
// Input으로 받은 userProperties 객체를 한번 더 감싼 형태
166154

@@ -236,12 +224,10 @@ struct PropertiesWrapper<T: Codable>: Codable {
236224
public static func setCustomUserProperties(
237225
customUserProperties: [String: Any?]
238226
) throws {
239-
guard let clientId = SdkConfig.clientIdInUserDefaults else {
240-
return
241-
}
242-
guard let bluxId = SdkConfig.bluxIdInUserDefaults else {
243-
return
244-
}
227+
guard
228+
let clientId = SdkConfig.clientIdInUserDefaults,
229+
let bluxId = SdkConfig.bluxIdInUserDefaults
230+
else { return }
245231

246232
var processedCustomProperties: [String: CustomValue] = [:]
247233

BluxClient/Classes/HTTPClient.swift

Lines changed: 28 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,37 @@
77

88
import Foundation
99

10-
enum Environment: String {
11-
case local = "http://localhost:9000/local"
12-
case dev = "https://api.blux.ai/dev"
13-
case stg = "https://api.blux.ai/stg"
14-
case prod = "https://api.blux.ai/prod"
15-
}
16-
17-
enum Configuration {
18-
static let apiBaseUrl: String = {
19-
guard let bundle = Bundle(identifier: "org.cocoapods.BluxClient"),
20-
let url = bundle.object(forInfoDictionaryKey: "API_BASE_URL")
21-
as? String
22-
else {
23-
return Environment.local.rawValue
24-
}
25-
return url
26-
}()
27-
}
28-
2910
final class HTTPClient {
3011
static let shared: HTTPClient = .init()
3112

32-
private static let API_BASE_URL: URL = .init(
33-
string: Configuration.apiBaseUrl)!
34-
35-
init() {
36-
Logger.verbose(HTTPClient.API_BASE_URL)
37-
}
38-
39-
enum HTTPMethodWithBody: String {
13+
private enum HTTPMethodWithBody: String {
4014
case POST
4115
case PUT
4216
case PATCH
4317
case DELETE
4418
}
4519

46-
enum HTTPError: Error {
20+
private enum HTTPError: Error {
4721
case transportError(Error)
4822
case serverSideError(Int)
4923
}
5024

51-
func createRequest(path: String) -> URLRequest? {
25+
private enum Environment: String {
26+
case local = "http://localhost:9000/local"
27+
case dev = "https://api.blux.ai/dev"
28+
case stg = "https://api.blux.ai/stg"
29+
case prod = "https://api.blux.ai/prod"
30+
}
31+
32+
private var API_BASE_URL: String = Environment.prod.rawValue
33+
34+
// MARK: - Private Methods
35+
36+
private func createRequest(path: String) -> URLRequest? {
5237
guard let clientId = SdkConfig.clientIdInUserDefaults else {
5338
return nil
5439
}
55-
guard let url = URL(string: "\(HTTPClient.API_BASE_URL)\(path)")
40+
guard let url = URL(string: "\(API_BASE_URL)\(path)")
5641
else {
5742
return nil
5843
}
@@ -75,15 +60,15 @@ final class HTTPClient {
7560
return request
7661
}
7762

78-
func createRequestWithBody<T: Codable>(
63+
private func createRequestWithBody<T: Codable>(
7964
method: HTTPMethodWithBody, path: String, body: T
8065
) -> URLRequest? {
8166
guard let clientId = SdkConfig.clientIdInUserDefaults else {
8267
Logger.error("No Client ID.")
8368
return nil
8469
}
8570

86-
guard let url = URL(string: "\(HTTPClient.API_BASE_URL)\(path)")
71+
guard let url = URL(string: "\(API_BASE_URL)\(path)")
8772
else {
8873
return nil
8974
}
@@ -110,7 +95,7 @@ final class HTTPClient {
11095
return request
11196
}
11297

113-
func createAsyncTask<V: Codable>(
98+
private func createAsyncTask<V: Codable>(
11499
request: URLRequest, completion: @escaping (V?, Error?) -> Void
115100
) -> URLSessionDataTask {
116101
let task = URLSession.shared.dataTask(with: request) {
@@ -153,71 +138,49 @@ final class HTTPClient {
153138
return task
154139
}
155140

156-
// MARK: - Methods
141+
// MARK: - Public Methods
157142

158143
func get<T: Codable>(
159144
path: String, completion: @escaping (T?, Error?) -> Void
160145
) {
161-
guard let request = createRequest(path: path) else {
162-
return
163-
}
146+
guard let request = createRequest(path: path)
147+
else { return }
164148

165149
Logger.verbose("GET Request - path:\(path)")
166-
167-
let task = createAsyncTask(
168-
request: request, completion: completion)
169-
150+
let task = createAsyncTask(request: request, completion: completion)
170151
task.resume()
171152
}
172153

173154
func post<T: Codable, V: Codable>(
174155
path: String, body: T, completion: @escaping (V?, Error?) -> Void
175156
) {
176-
guard
177-
let request = createRequestWithBody(
178-
method: HTTPMethodWithBody.POST, path: path, body: body)
179-
else {
180-
return
181-
}
157+
guard let request = createRequestWithBody(method: HTTPMethodWithBody.POST, path: path, body: body)
158+
else { return }
182159

183160
Logger.verbose("POST Request - path:\(path) body:\(body)")
184-
185161
let task = createAsyncTask(request: request, completion: completion)
186-
187162
task.resume()
188163
}
189164

190165
func put<T: Codable, V: Codable>(
191166
path: String, body: T, completion: @escaping (V?, Error?) -> Void
192167
) {
193-
guard
194-
let request = createRequestWithBody(
195-
method: HTTPMethodWithBody.PUT, path: path, body: body)
196-
else {
197-
return
198-
}
168+
guard let request = createRequestWithBody(method: HTTPMethodWithBody.PUT, path: path, body: body)
169+
else { return }
199170

200171
Logger.verbose("PUT Request - path:\(path) body:\(body)")
201-
202172
let task = createAsyncTask(request: request, completion: completion)
203-
204173
task.resume()
205174
}
206175

207176
func patch<T: Codable, V: Codable>(
208177
path: String, body: T, completion: @escaping (V?, Error?) -> Void
209178
) {
210-
guard
211-
let request = createRequestWithBody(
212-
method: HTTPMethodWithBody.PATCH, path: path, body: body)
213-
else {
214-
return
215-
}
179+
guard let request = createRequestWithBody(method: HTTPMethodWithBody.PATCH, path: path, body: body)
180+
else { return }
216181

217182
Logger.verbose("PATCH Request - path:\(path) body:\(body)")
218-
219183
let task = createAsyncTask(request: request, completion: completion)
220-
221184
task.resume()
222185
}
223186
}

BluxClient/Classes/InappService.swift

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,7 @@ class InappService {
156156
{
157157
switch scheme {
158158
case "http", "https":
159-
guard
160-
let topViewController =
161-
UIViewController.getTopViewController()
162-
else {
163-
Logger.verbose(
164-
"INAPP: Failed to get top view controller.")
165-
dismissWebView(webviewController)
166-
return
167-
}
168-
169159
EventService.createInappOpened(notificationId)
170-
171160
// 기존 웹뷰 닫기
172161
dismissWebView(webviewController) {
173162
DispatchQueue.main.async {
@@ -219,27 +208,19 @@ class InappService {
219208
@available(iOSApplicationExtension, unavailable)
220209
extension UIViewController {
221210
static func getTopViewController(
222-
_ baseViewController: UIViewController? = UIApplication.shared
223-
.keyWindow?.rootViewController
211+
_ baseViewController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController
224212
) -> UIViewController? {
225-
if let navigationController = baseViewController
226-
as? UINavigationController
227-
{
228-
return getTopViewController(
229-
navigationController.visibleViewController)
213+
if let navigationController = baseViewController as? UINavigationController {
214+
return getTopViewController(navigationController.visibleViewController)
230215
}
231216

232-
if let tabBarController = baseViewController as? UITabBarController {
233-
if let selectedViewController = tabBarController
234-
.selectedViewController
235-
{
236-
return getTopViewController(selectedViewController)
237-
}
217+
if let tabBarController = baseViewController as? UITabBarController,
218+
let selectedViewController = tabBarController.selectedViewController
219+
{
220+
return getTopViewController(selectedViewController)
238221
}
239222

240-
if let presentedViewController = baseViewController?
241-
.presentedViewController
242-
{
223+
if let presentedViewController = baseViewController?.presentedViewController {
243224
return getTopViewController(presentedViewController)
244225
}
245226

BluxClient/Classes/SdkConfig.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public enum SdkType: String {
1414
}
1515

1616
final class SdkConfig {
17-
static var sdkVersion = "0.2.12"
17+
static var sdkVersion = "0.3.0"
1818
static var sdkType: SdkType = .native
1919

2020
static var bluxAppGroupNameKey = "BluxAppGroupName"

BluxClient/Classes/request/events/Event.swift

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,50 +46,35 @@ public class Event: Codable {
4646

4747
@discardableResult
4848
public func setItemId(_ itemId: String) throws -> Event {
49-
var validatedItemId = try Validator.validateString(
50-
itemId, min: 1, max: 500, varName: "itemId")
51-
49+
let validatedItemId = try Validator.validateString(itemId, min: 1, max: 500, varName: "itemId")
5250
eventProperties.itemId = validatedItemId
5351
return self
5452
}
5553

5654
@discardableResult
5755
public func setPage(_ page: String) throws -> Event {
58-
var validatedPage = try Validator.validateString(
59-
page, min: 1, max: 500, varName: "page")
56+
let validatedPage = try Validator.validateString(page, min: 1, max: 500, varName: "page")
6057

6158
eventProperties.page = validatedPage
6259
return self
6360
}
6461

6562
@discardableResult
6663
public func setPrice(_ price: Double) throws -> Event {
67-
var validatedPrice = try Validator.validateNumber(
68-
price,
69-
min: 0,
70-
varName: "price")
71-
64+
let validatedPrice = try Validator.validateNumber(price, min: 0, varName: "price")
7265
eventProperties.price = validatedPrice
7366
return self
7467
}
7568

7669
@discardableResult
7770
public func setRating(_ rating: Double) throws -> Event {
78-
var validatedRating = try Validator.validateNumber(
79-
rating,
80-
min: 0,
81-
varName: "rating")
82-
71+
let validatedRating = try Validator.validateNumber(rating, min: 0, varName: "rating")
8372
eventProperties.rating = validatedRating
8473
return self
8574
}
8675

8776
@discardableResult
88-
public func setCustomEventProperties(
89-
_ customEventProperties: [String: String]?
90-
)
91-
-> Event
92-
{
77+
public func setCustomEventProperties(_ customEventProperties: [String: String]?) -> Event {
9378
self.customEventProperties = customEventProperties
9479
return self
9580
}

0 commit comments

Comments
 (0)