How do I call IPRout with Swift?
This example looks up 8.8.8.8 unless the client example uses /ip to demonstrate caller lookup. Replace only the route when switching modes.
var request = URLRequest(url: URL(string: "https://api.iprout.com/ip/8.8.8.8")!)
request.timeoutInterval = 10
request.setValue("Bearer \(ProcessInfo.processInfo.environment["IPROUT_API_KEY"]!)", forHTTPHeaderField: "Authorization")
let (data, response) = try await URLSession.shared.data(for: request)
guard let http = response as? HTTPURLResponse, http.statusCode < 400 else { throw URLError(.badServerResponse) }
let result = try JSONDecoder().decode(IpInfo.self, from: data)How do I look up the caller instead?
Keep the headers and error handling unchanged, but request https://api.iprout.com/ip with no address after /ip.
What does production code need?
Keep the key server-side, use a finite timeout, inspect HTTP status before decoding success data, and handle 401, 422, 429, and transient 500 responses.
- Read IPROUT_API_KEY from a secret environment
- Do not retry 401 or 422 automatically
- Use bounded backoff for transient server or network failures