DomainDig/IPGeolocationService.swift

ede15c586af71b8d6f2c190ffdad0324414cac06
domain-dig/DomainDig/IPGeolocationService.swift history · blame · raw

17 lines · 576 bytes

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