From 200ac4b12b331edd287830fd571b063f0b672553 Mon Sep 17 00:00:00 2001 From: Oleg Date: Tue, 16 Jul 2024 15:28:47 +0300 Subject: [PATCH] Switch to Profiles API for domain name resolution (#609) --- .../ApiRequestBuilder.swift | 14 ++++++++ .../NetworkService+ProfilesApi.swift | 20 +++++++++++ .../Services/Networking/NetworkService.swift | 34 ++----------------- 3 files changed, 36 insertions(+), 32 deletions(-) diff --git a/unstoppable-ios-app/domains-manager-ios/NetworkEnvironment/ApiRequestBuilder.swift b/unstoppable-ios-app/domains-manager-ios/NetworkEnvironment/ApiRequestBuilder.swift index 20d2904cd..b238c0cd0 100644 --- a/unstoppable-ios-app/domains-manager-ios/NetworkEnvironment/ApiRequestBuilder.swift +++ b/unstoppable-ios-app/domains-manager-ios/NetworkEnvironment/ApiRequestBuilder.swift @@ -811,6 +811,20 @@ extension Endpoint { ) } + static func getProfileReverseResolution(for identifier: HexAddress, + supportedNameServices: [NetworkService.ProfilesSupportedNameServices]?) throws -> Endpoint { + var queryItems: [URLQueryItem] = [] + if let supportedNameServices { + let services = supportedNameServices.map { $0.rawValue }.joined(separator: ",") + queryItems.append(URLQueryItem(name: "resolutionOrder", value: services)) + } + return Endpoint( + host: NetworkConfig.baseAPIHost, + path: "/profile/resolve/\(identifier)", + queryItems: queryItems, + body: "" + ) + } static func joinBadgeCommunity(body: String) -> Endpoint { return Endpoint( diff --git a/unstoppable-ios-app/domains-manager-ios/Services/Networking/NetworkService+ProfilesApi.swift b/unstoppable-ios-app/domains-manager-ios/Services/Networking/NetworkService+ProfilesApi.swift index 43273c35d..6ef77242d 100644 --- a/unstoppable-ios-app/domains-manager-ios/Services/Networking/NetworkService+ProfilesApi.swift +++ b/unstoppable-ios-app/domains-manager-ios/Services/Networking/NetworkService+ProfilesApi.swift @@ -228,6 +228,26 @@ extension NetworkService: DomainProfileNetworkServiceProtocol { } } + enum ProfilesSupportedNameServices: String { + case ud, ens, lens + } + + /// This function will return UD/ENS/Lens/Null name and corresponding PFP if available OR throw 404 + public func fetchProfilesReverseResolution(for identifier: HexAddress, + supportedNameServices: [ProfilesSupportedNameServices]? = nil) async throws -> GlobalRR? { + do { + let endpoint = try Endpoint.getProfileReverseResolution(for: identifier, + supportedNameServices: supportedNameServices) + let data = try await fetchDataHandlingThrottleFor(endpoint: endpoint, method: .get) + let response = try JSONDecoder().decode(GlobalRR.self, from: data) + return response + } catch NetworkLayerError.badResponseOrStatusCode(let code, _, _) where code == 404 { // 404 means no RR domain or ENS domain + return nil + } catch { + throw error + } + } + //MARK: private methods private func getGeneratedMessageToRetrieve(for domain: DomainItem) async throws -> GeneratedMessage { guard let url = Endpoint.getGeneratedMessageToRetrieve(for: domain).url else { diff --git a/unstoppable-ios-app/domains-manager-ios/Services/Networking/NetworkService.swift b/unstoppable-ios-app/domains-manager-ios/Services/Networking/NetworkService.swift index f3f295092..fe0209136 100644 --- a/unstoppable-ios-app/domains-manager-ios/Services/Networking/NetworkService.swift +++ b/unstoppable-ios-app/domains-manager-ios/Services/Networking/NetworkService.swift @@ -62,19 +62,6 @@ struct NetworkService { } } - static var authAPIKey: String { - let isTestnetUsed = User.instance.getSettings().isTestnetUsed - if isTestnetUsed { - return NetworkService.testnetMetadataAPIKey - } else { - return NetworkService.mainnetMetadataAPIKey - } - } - - static var authHeader: [String : String] { - return ["Authorization" : "Bearer \(authAPIKey)"] - } - static var currentProfilesAPIKey: String { let isTestnetUsed = User.instance.getSettings().isTestnetUsed if isTestnetUsed { @@ -464,28 +451,11 @@ extension NetworkService { } func fetchReverseResolution(for address: HexAddress) async throws -> DomainName? { - let url = URL(string: "\(NetworkConfig.baseResolveUrl)/reverse/\(address)")! - let data = try await NetworkService().fetchData(for: url, - method: .get, - extraHeaders: NetworkService.authHeader) - let response = try JSONDecoder().decode(ResolveDomainsApiResponse.self, from: data) - return response.meta.domain + try await fetchProfilesReverseResolution(for: address, supportedNameServices: [.ud])?.name } - /// This function will return UD/ENS/Null name and corresponding PFP if available OR throw 404 func fetchGlobalReverseResolution(for identifier: HexAddress) async throws -> GlobalRR? { - do { - guard let url = URL(string: "\(NetworkConfig.baseAPIUrl)/profile/resolve/\(identifier)") else { return nil } // User's input contains not allowed characters - - let data = try await NetworkService().fetchData(for: url, - method: .get) - let response = try JSONDecoder().decode(GlobalRR.self, from: data) - return response - } catch NetworkLayerError.badResponseOrStatusCode(let code, _, _) where code == 404 { // 404 means no RR domain or ENS domain - return nil - } catch { - throw error - } + try await fetchProfilesReverseResolution(for: identifier) } private func getRegex(for expandedTicker: String, coins: [CoinRecord]) -> String? {