Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to Profiles API for domain name resolution #609

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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