Skip to content

Commit

Permalink
Switch to Profiles API for domain name resolution (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg-Pecheneg authored Jul 16, 2024
1 parent f6ac34b commit 200ac4b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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? {
Expand Down

0 comments on commit 200ac4b

Please sign in to comment.