krz/domain-dig

an ios app for DNS & SSL analysis

clone: git clone https://gitbay.org/krz/domain-dig.git

v4.6.0: DomainDig/IPGeolocationService.swift · raw

 1import Foundation
 2
 3struct IPGeolocationService {
 4    static func lookup(ip: String) async -> ServiceResult<IPGeolocation> {
 5        let url = URL(string: "https://ipapi.co/\(ip)/json/")!
 6        let request = URLRequest(url: url, timeoutInterval: 10)
 7
 8        do {
 9            let (data, response) = try await URLSession.shared.data(for: request)
10
11            guard let httpResponse = response as? HTTPURLResponse,
12                  httpResponse.statusCode == 200 else {
13                return .error(URLError(.badServerResponse).localizedDescription)
14            }
15
16            let geolocation = try JSONDecoder().decode(IPGeolocation.self, from: data)
17            return .success(geolocation)
18        } catch {
19            return .error(error.localizedDescription)
20        }
21    }
22}