diff --git a/TwoFAS/Common/Sources/Config/Config.swift b/TwoFAS/Common/Sources/Config/Config.swift index 28c326f3..50e30086 100644 --- a/TwoFAS/Common/Sources/Config/Config.swift +++ b/TwoFAS/Common/Sources/Config/Config.swift @@ -29,6 +29,7 @@ public enum Config { public enum API { public static let baseURL = URL(string: "https://api2.2fas.com")! + public static let notificationsURL = URL(string: "https://notifications.2fas.com")! } public enum TokenConsts { diff --git a/TwoFAS/Data/MainRepository/MainRepositoryImpl.swift b/TwoFAS/Data/MainRepository/MainRepositoryImpl.swift index 634c6f54..4c488d96 100644 --- a/TwoFAS/Data/MainRepository/MainRepositoryImpl.swift +++ b/TwoFAS/Data/MainRepository/MainRepositoryImpl.swift @@ -45,7 +45,10 @@ final class MainRepositoryImpl: MainRepository { let userDefaultsRepository: UserDefaultsRepository let storageRepository: StorageRepository let logDataChange: LogDataChange - let networkStack = NetworkStack(baseURL: Config.API.baseURL) + let networkStack = NetworkStack( + baseURL: Config.API.baseURL, + notificationsBaseURL: Config.API.notificationsURL + ) let iconDatabase: IconDescriptionDatabase = IconDescriptionDatabaseImpl() let serviceDefinitionDatabase: ServiceDefinitionDatabase = ServiceDefinitionDatabaseImpl() let iconDescriptionDatabase: IconDescriptionDatabase = IconDescriptionDatabaseImpl() diff --git a/TwoFAS/NetworkStack/NetworkCall.swift b/TwoFAS/NetworkStack/NetworkCall.swift index 3c78e3f5..3ca4b702 100644 --- a/TwoFAS/NetworkStack/NetworkCall.swift +++ b/TwoFAS/NetworkStack/NetworkCall.swift @@ -25,6 +25,7 @@ final class NetworkCall { var noError: (() -> Void)? private let baseURL: URL + private let notificationsBaseURL: URL private let jsonDecoder: JSONDecoder = { let decoder = JSONDecoder() decoder.keyDecodingStrategy = .convertFromSnakeCase @@ -60,8 +61,9 @@ final class NetworkCall { }() private let session: URLSession - init(baseURL: URL) { + init(baseURL: URL, notificationsBaseURL: URL) { self.baseURL = baseURL + self.notificationsBaseURL = notificationsBaseURL self.session = URLSession(configuration: configuration) } @@ -79,7 +81,22 @@ final class NetworkCall { dataTask.resume() } } - + + func handleNotificationsCall( + with request: NetworkRequestFormat, + completion: @escaping (Result) -> Void + ) { + queue.async { [weak self] in + guard let self else { return } + let dataTask = self.session.dataTask( + with: self.notificationsUrlRequest(for: request) + ) { [weak self] data, response, error in + self?.completionHandler(data, response as? HTTPURLResponse, error, completion: completion) + } + dataTask.resume() + } + } + func handleCall(with request: NetworkRequestFormat, completion: @escaping (Result) -> Void) { queue.async { [weak self] in guard let self else { return } @@ -246,7 +263,18 @@ private extension NetworkCall { return urlRequest } - + + func notificationsUrlRequest(for request: NetworkRequestFormat) -> URLRequest { + let path = notificationsBaseURL.absoluteString + "/" + request.path + guard + let url = URL(string: path) + else { fatalError("Network Stack: Can't create path for url: \(request.path)") } + var urlRequest = URLRequest(url: url) + urlRequest.httpMethod = request.method.rawValue + + return urlRequest + } + func urlRequest(for request: NetworkRequestFormat, data: P) -> URLRequest { let path = baseURL.absoluteString + "/" + request.path guard diff --git a/TwoFAS/NetworkStack/NetworkStack.swift b/TwoFAS/NetworkStack/NetworkStack.swift index 2311b60a..303aa13b 100644 --- a/TwoFAS/NetworkStack/NetworkStack.swift +++ b/TwoFAS/NetworkStack/NetworkStack.swift @@ -28,8 +28,11 @@ public final class NetworkStack { private let appVersionHandler = AppVersionHandler() private let networkHandler: NetworkStackRepositoryImpl - public init(baseURL: URL) { - networkHandler = NetworkStackRepositoryImpl(baseURL: baseURL) + public init(baseURL: URL, notificationsBaseURL: URL) { + networkHandler = NetworkStackRepositoryImpl( + baseURL: baseURL, + notificationsBaseURL: notificationsBaseURL + ) } } diff --git a/TwoFAS/NetworkStack/NetworkStackRepositoryImpl.swift b/TwoFAS/NetworkStack/NetworkStackRepositoryImpl.swift index 43e639e0..26c64273 100644 --- a/TwoFAS/NetworkStack/NetworkStackRepositoryImpl.swift +++ b/TwoFAS/NetworkStack/NetworkStackRepositoryImpl.swift @@ -22,8 +22,11 @@ import Foundation public final class NetworkStackRepositoryImpl { private let platform = "ios" private let networkCall: NetworkCall - init(baseURL: URL) { - self.networkCall = NetworkCall(baseURL: baseURL) + init(baseURL: URL, notificationsBaseURL: URL) { + self.networkCall = NetworkCall( + baseURL: baseURL, + notificationsBaseURL: notificationsBaseURL + ) networkCall.sslError = { NotificationCenter.default.post(name: .SSLNetworkErrorNotificationKey, object: nil) } @@ -128,13 +131,13 @@ extension NetworkStackRepositoryImpl: NetworkStackRepository { ) networkCall.handleCall(with: req, data: reqData, completion: completion) } - + public func listAllNews( publishedAfter: String, completion: @escaping (Result<[ListNews.NewsEntry], NetworkError>) -> Void ) { let req = ListNews.Request(platform: "ios", publishedAfter: publishedAfter) - networkCall.handleCall(with: req, completion: completion) + networkCall.handleNotificationsCall(with: req, completion: completion) } public func uploadLogs( diff --git a/TwoFAS/TwoFASAuth/MainRepository/MainRepositoryImpl.swift b/TwoFAS/TwoFASAuth/MainRepository/MainRepositoryImpl.swift index 9e05934c..5d77f8ff 100644 --- a/TwoFAS/TwoFASAuth/MainRepository/MainRepositoryImpl.swift +++ b/TwoFAS/TwoFASAuth/MainRepository/MainRepositoryImpl.swift @@ -26,7 +26,10 @@ import Protection import TimeVerification final class MainRepositoryImpl { - let network = NetworkStack(baseURL: Config.API.baseURL) + let network = NetworkStack( + baseURL: Config.API.baseURL, + notificationsBaseURL: Config.API.notificationsURL + ) let protectionModule = Protection() let storageRepository: StorageRepository