From 2228a4deeeacb2dee6486624dc06c7120c608017 Mon Sep 17 00:00:00 2001 From: Chung Tran Date: Fri, 26 Jan 2024 12:04:25 +0700 Subject: [PATCH 1/2] feat: remove old Networking --- .../Networking/Codable+SnakeCase.swift | 11 ---- .../Networking/Dto/JsonRpcRequestDto.swift | 8 --- .../Networking/Dto/JsonRpcResponseDto.swift | 15 ----- .../Common/Services/Networking/Endpoint.swift | 23 -------- .../Services/Networking/ErrorModel.swift | 29 --------- .../Services/Networking/HttpClient.swift | 42 ------------- .../Services/Networking/RequestMethod.swift | 9 --- .../Repository/NotificationRepository.swift | 59 +++++++++++-------- .../Repository/NotifierEndpoint.swift | 25 -------- 9 files changed, 35 insertions(+), 186 deletions(-) delete mode 100644 p2p_wallet/Common/Services/Networking/Codable+SnakeCase.swift delete mode 100644 p2p_wallet/Common/Services/Networking/Dto/JsonRpcRequestDto.swift delete mode 100644 p2p_wallet/Common/Services/Networking/Dto/JsonRpcResponseDto.swift delete mode 100644 p2p_wallet/Common/Services/Networking/Endpoint.swift delete mode 100644 p2p_wallet/Common/Services/Networking/ErrorModel.swift delete mode 100644 p2p_wallet/Common/Services/Networking/HttpClient.swift delete mode 100644 p2p_wallet/Common/Services/Networking/RequestMethod.swift delete mode 100644 p2p_wallet/Common/Services/NotificationsService/Repository/NotifierEndpoint.swift diff --git a/p2p_wallet/Common/Services/Networking/Codable+SnakeCase.swift b/p2p_wallet/Common/Services/Networking/Codable+SnakeCase.swift deleted file mode 100644 index 0091c446b7..0000000000 --- a/p2p_wallet/Common/Services/Networking/Codable+SnakeCase.swift +++ /dev/null @@ -1,11 +0,0 @@ -import Foundation - -extension Encodable { - var snakeCaseEncoded: String? { - let encoder = JSONEncoder() - encoder.outputFormatting = [.prettyPrinted, .sortedKeys] - encoder.keyEncodingStrategy = .convertToSnakeCase - guard let data = try? encoder.encode(self) else { return nil } - return String(data: data, encoding: .utf8) - } -} diff --git a/p2p_wallet/Common/Services/Networking/Dto/JsonRpcRequestDto.swift b/p2p_wallet/Common/Services/Networking/Dto/JsonRpcRequestDto.swift deleted file mode 100644 index 2cef91b742..0000000000 --- a/p2p_wallet/Common/Services/Networking/Dto/JsonRpcRequestDto.swift +++ /dev/null @@ -1,8 +0,0 @@ -import Foundation - -struct JsonRpcRequestDto: Encodable { - let jsonrpc = "2.0" - let id = UUID().uuidString - let method: String - let params: [T] -} diff --git a/p2p_wallet/Common/Services/Networking/Dto/JsonRpcResponseDto.swift b/p2p_wallet/Common/Services/Networking/Dto/JsonRpcResponseDto.swift deleted file mode 100644 index bbc25ac5ce..0000000000 --- a/p2p_wallet/Common/Services/Networking/Dto/JsonRpcResponseDto.swift +++ /dev/null @@ -1,15 +0,0 @@ -import Foundation - -struct JsonRpcResponseDto: Decodable { - let id: String - let result: T -} - -struct JsonRpcResponseErrorDto: Decodable { - let id: String - let error: JsonRpcError -} - -struct JsonRpcError: Decodable, Error { - let code: Int -} diff --git a/p2p_wallet/Common/Services/Networking/Endpoint.swift b/p2p_wallet/Common/Services/Networking/Endpoint.swift deleted file mode 100644 index b3e4364813..0000000000 --- a/p2p_wallet/Common/Services/Networking/Endpoint.swift +++ /dev/null @@ -1,23 +0,0 @@ -import Foundation - -protocol Endpoint { - var baseURL: String { get } - var path: String { get } - var method: RequestMethod { get } - var header: [String: String] { get } - var body: String? { get } -} - -extension Endpoint { - var header: [String: String] { - [ - "Content-Type": "application/json", - "Accept": "application/json", - "CHANNEL_ID": "P2PWALLET_MOBILE", - ] - } - - var baseURL: String { - GlobalAppState.shared.pushServiceEndpoint - } -} diff --git a/p2p_wallet/Common/Services/Networking/ErrorModel.swift b/p2p_wallet/Common/Services/Networking/ErrorModel.swift deleted file mode 100644 index 3f75f57549..0000000000 --- a/p2p_wallet/Common/Services/Networking/ErrorModel.swift +++ /dev/null @@ -1,29 +0,0 @@ -import Foundation - -enum ErrorModel: Error, LocalizedError { - case decode - case invalidURL - case noResponse - case unauthorized - case unexpectedStatusCode - case unknown - case api(model: ApiErrorModel) - - var errorDescription: String? { - switch self { - case let .api(model): - return model.message - case .decode: - return "Decode error" - case .unauthorized: - return "Session expired" - default: - return "Unknown error" - } - } -} - -struct ApiErrorModel: Decodable { - let code: Int - let message: String -} diff --git a/p2p_wallet/Common/Services/Networking/HttpClient.swift b/p2p_wallet/Common/Services/Networking/HttpClient.swift deleted file mode 100644 index 535ea473be..0000000000 --- a/p2p_wallet/Common/Services/Networking/HttpClient.swift +++ /dev/null @@ -1,42 +0,0 @@ -import Foundation - -@available(*, deprecated, message: "Use KeyAppNetworking.HTTPClient instead") -protocol HttpClient { - func sendRequest(endpoint: Endpoint, responseModel: T.Type) async throws -> T -} - -@available(*, deprecated, message: "Use KeyAppNetworking.HTTPClient instead") -final class HttpClientImpl: HttpClient { - func sendRequest(endpoint: Endpoint, responseModel: T.Type) async throws -> T { - guard let url = URL(string: endpoint.baseURL + endpoint.path) else { throw ErrorModel.invalidURL } - - var request = URLRequest(url: url) - request.httpMethod = endpoint.method.rawValue - request.allHTTPHeaderFields = endpoint.header - - if let body = endpoint.body { - request.httpBody = body.data(using: .utf8) - } - - print(request.cURL()) - - let (data, response) = try await URLSession.shared.data(for: request) - guard let response = response as? HTTPURLResponse else { throw ErrorModel.noResponse } - switch response.statusCode { - case 200 ... 299: - let decoder = JSONDecoder() - decoder.keyDecodingStrategy = .convertFromSnakeCase - guard let decodedResponse = try? decoder.decode(responseModel, from: data) else { - if let decodedErrorResponse = try? decoder.decode(JsonRpcResponseErrorDto.self, from: data) { - throw decodedErrorResponse.error - } - throw ErrorModel.decode - } - return decodedResponse - case 401: - throw ErrorModel.unauthorized - default: - throw ErrorModel.unexpectedStatusCode - } - } -} diff --git a/p2p_wallet/Common/Services/Networking/RequestMethod.swift b/p2p_wallet/Common/Services/Networking/RequestMethod.swift deleted file mode 100644 index 23a48d195c..0000000000 --- a/p2p_wallet/Common/Services/Networking/RequestMethod.swift +++ /dev/null @@ -1,9 +0,0 @@ -import Foundation - -enum RequestMethod: String { - case delete = "DELETE" - case get = "GET" - case patch = "PATCH" - case post = "POST" - case put = "PUT" -} diff --git a/p2p_wallet/Common/Services/NotificationsService/Repository/NotificationRepository.swift b/p2p_wallet/Common/Services/NotificationsService/Repository/NotificationRepository.swift index cb90f1e365..9c9730a175 100644 --- a/p2p_wallet/Common/Services/NotificationsService/Repository/NotificationRepository.swift +++ b/p2p_wallet/Common/Services/NotificationsService/Repository/NotificationRepository.swift @@ -1,34 +1,44 @@ import Foundation +import KeyAppNetworking import Resolver protocol NotificationRepository { - typealias DeviceTokenResponse = JsonRpcResponseDto - - func sendDeviceToken(model: DeviceTokenDto) async throws -> DeviceTokenResponse - func removeDeviceToken(model: DeleteDeviceTokenDto) async throws -> DeviceTokenResponse + func sendDeviceToken(model: DeviceTokenDto) async throws -> DeviceTokenResponseDto + func removeDeviceToken(model: DeleteDeviceTokenDto) async throws -> DeviceTokenResponseDto } final class NotificationRepositoryImpl: NotificationRepository { - let httpClient = HttpClientImpl() + let httpClient = JSONRPCHTTPClient(urlSession: DebugLoggingURLSession()) + + private var baseURL: String { + GlobalAppState.shared.pushServiceEndpoint + } - func sendDeviceToken(model: DeviceTokenDto) async throws -> DeviceTokenResponse { + private var header: [String: String] { + [ + "Content-Type": "application/json", + "Accept": "application/json", + "CHANNEL_ID": "P2PWALLET_MOBILE", + ] + } + + func sendDeviceToken(model: DeviceTokenDto) async throws -> DeviceTokenResponseDto { do { - return try await httpClient.sendRequest( - endpoint: NotifierEndpoint.addDevice(dto: .init( + return try await httpClient.request( + baseURL: baseURL, + header: header, + body: .init( method: "add_device", - params: [model] - )), - responseModel: DeviceTokenResponse.self + params: model + ) ) - } catch let error as JsonRpcError { + } catch let error as JSONRPCError { if error.code == -32001 { + // Already sent return .init( - id: "", - result: .init( - deviceToken: model.deviceToken, - timestamp: String(Date().timeIntervalSince1970), - clientId: model.clientId - ) + deviceToken: model.deviceToken, + timestamp: String(Date().timeIntervalSince1970), + clientId: model.clientId ) } throw error @@ -37,13 +47,14 @@ final class NotificationRepositoryImpl: NotificationRepository { } } - func removeDeviceToken(model: DeleteDeviceTokenDto) async throws -> DeviceTokenResponse { - try await httpClient.sendRequest( - endpoint: NotifierEndpoint.deleteDevice(dto: .init( + func removeDeviceToken(model: DeleteDeviceTokenDto) async throws -> DeviceTokenResponseDto { + try await httpClient.request( + baseURL: baseURL, + header: header, + body: .init( method: "delete_device", - params: [model] - )), - responseModel: DeviceTokenResponse.self + params: model + ) ) } } diff --git a/p2p_wallet/Common/Services/NotificationsService/Repository/NotifierEndpoint.swift b/p2p_wallet/Common/Services/NotificationsService/Repository/NotifierEndpoint.swift deleted file mode 100644 index ac98d38a48..0000000000 --- a/p2p_wallet/Common/Services/NotificationsService/Repository/NotifierEndpoint.swift +++ /dev/null @@ -1,25 +0,0 @@ -import Foundation - -enum NotifierEndpoint { - case addDevice(dto: JsonRpcRequestDto) - case deleteDevice(dto: JsonRpcRequestDto) -} - -extension NotifierEndpoint: Endpoint { - var path: String { - "" - } - - var method: RequestMethod { - .post - } - - var body: String? { - switch self { - case let .addDevice(dto): - return dto.snakeCaseEncoded - case let .deleteDevice(dto): - return dto.snakeCaseEncoded - } - } -} From 08589cb6cfc0d851ba9968533673fb4ef6ac845a Mon Sep 17 00:00:00 2001 From: Chung Tran Date: Fri, 26 Jan 2024 13:29:18 +0700 Subject: [PATCH 2/2] fix: models --- .../Repository/Dto/Request/DeleteDeviceTokenDto.swift | 7 +++++++ .../Repository/Dto/Request/DeviceTokenDto.swift | 8 ++++++++ .../Repository/Dto/Request/DeviceTokenInfoDto.swift | 6 ++++++ .../Repository/Dto/Response/DeviceTokenResposeDto.swift | 6 ++++++ .../Repository/NotificationRepository.swift | 4 ++-- 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/p2p_wallet/Common/Services/NotificationsService/Repository/Dto/Request/DeleteDeviceTokenDto.swift b/p2p_wallet/Common/Services/NotificationsService/Repository/Dto/Request/DeleteDeviceTokenDto.swift index 4891bcb0e0..06eebde122 100644 --- a/p2p_wallet/Common/Services/NotificationsService/Repository/Dto/Request/DeleteDeviceTokenDto.swift +++ b/p2p_wallet/Common/Services/NotificationsService/Repository/Dto/Request/DeleteDeviceTokenDto.swift @@ -5,4 +5,11 @@ struct DeleteDeviceTokenDto: Encodable { let clientId: String let ethPubkey: String? let type = "device" + + enum CodingKeys: String, CodingKey { + case deviceToken = "device_token" + case clientId = "client_id" + case ethPubkey = "eth_pubkey" + case type + } } diff --git a/p2p_wallet/Common/Services/NotificationsService/Repository/Dto/Request/DeviceTokenDto.swift b/p2p_wallet/Common/Services/NotificationsService/Repository/Dto/Request/DeviceTokenDto.swift index 1b9e511267..dda23910b7 100644 --- a/p2p_wallet/Common/Services/NotificationsService/Repository/Dto/Request/DeviceTokenDto.swift +++ b/p2p_wallet/Common/Services/NotificationsService/Repository/Dto/Request/DeviceTokenDto.swift @@ -6,4 +6,12 @@ struct DeviceTokenDto: Encodable { let ethPubkey: String? let type = "device" let deviceInfo: DeviceTokenInfo? + + enum CodingKeys: String, CodingKey { + case deviceToken = "device_token" + case clientId = "client_id" + case ethPubkey = "eth_pubkey" + case type + case deviceInfo = "device_info" + } } diff --git a/p2p_wallet/Common/Services/NotificationsService/Repository/Dto/Request/DeviceTokenInfoDto.swift b/p2p_wallet/Common/Services/NotificationsService/Repository/Dto/Request/DeviceTokenInfoDto.swift index 43b7825a3b..3f0ce430aa 100644 --- a/p2p_wallet/Common/Services/NotificationsService/Repository/Dto/Request/DeviceTokenInfoDto.swift +++ b/p2p_wallet/Common/Services/NotificationsService/Repository/Dto/Request/DeviceTokenInfoDto.swift @@ -4,4 +4,10 @@ struct DeviceTokenInfo: Codable { let osName: String let osVersion: String let deviceModel: String + + enum CodingKeys: String, CodingKey { + case osName = "os_name" + case osVersion = "os_version" + case deviceModel = "device_model" + } } diff --git a/p2p_wallet/Common/Services/NotificationsService/Repository/Dto/Response/DeviceTokenResposeDto.swift b/p2p_wallet/Common/Services/NotificationsService/Repository/Dto/Response/DeviceTokenResposeDto.swift index 6cbc05dc56..f541b5f10c 100644 --- a/p2p_wallet/Common/Services/NotificationsService/Repository/Dto/Response/DeviceTokenResposeDto.swift +++ b/p2p_wallet/Common/Services/NotificationsService/Repository/Dto/Response/DeviceTokenResposeDto.swift @@ -4,4 +4,10 @@ struct DeviceTokenResponseDto: Decodable { let deviceToken: String let timestamp: String let clientId: String + + enum CodingKeys: String, CodingKey { + case deviceToken = "device_token" + case timestamp + case clientId = "client_id" + } } diff --git a/p2p_wallet/Common/Services/NotificationsService/Repository/NotificationRepository.swift b/p2p_wallet/Common/Services/NotificationsService/Repository/NotificationRepository.swift index 9c9730a175..1431232594 100644 --- a/p2p_wallet/Common/Services/NotificationsService/Repository/NotificationRepository.swift +++ b/p2p_wallet/Common/Services/NotificationsService/Repository/NotificationRepository.swift @@ -29,7 +29,7 @@ final class NotificationRepositoryImpl: NotificationRepository { header: header, body: .init( method: "add_device", - params: model + params: [model] ) ) } catch let error as JSONRPCError { @@ -53,7 +53,7 @@ final class NotificationRepositoryImpl: NotificationRepository { header: header, body: .init( method: "delete_device", - params: model + params: [model] ) ) }