[TF-1592] add notificationsURL

This commit is contained in:
gmc 2024-07-23 19:30:21 +02:00
parent 41e3780f2c
commit 278b39f475
6 changed files with 52 additions and 11 deletions

View File

@ -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 {

View File

@ -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()

View File

@ -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<T: Decodable>(
with request: NetworkRequestFormat,
completion: @escaping (Result<T, NetworkError>) -> 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, NetworkError>) -> 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<P: Encodable>(for request: NetworkRequestFormat, data: P) -> URLRequest {
let path = baseURL.absoluteString + "/" + request.path
guard

View File

@ -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
)
}
}

View File

@ -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(

View File

@ -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