summaryrefslogtreecommitdiff
path: root/DomainDig/IPGeolocationService.swift
blob: 6c602957afecdac47ed822c02a16a1e396e4918f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import Foundation

struct IPGeolocationService {
    static func lookup(ip: String) async throws -> IPGeolocation {
        let url = URL(string: "https://ipapi.co/\(ip)/json/")!
        let request = URLRequest(url: url, timeoutInterval: 10)

        let (data, response) = try await URLSession.shared.data(for: request)

        guard let httpResponse = response as? HTTPURLResponse,
              httpResponse.statusCode == 200 else {
            throw URLError(.badServerResponse)
        }

        return try JSONDecoder().decode(IPGeolocation.self, from: data)
    }
}