Skip to content
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
3 changes: 3 additions & 0 deletions Sources/ipinfoKit/API/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extension Service {
case geoLocation(ipAddress: String)
case ASN(asn: String)
case batch(withFilter: Bool)
case resproxy(ipAddress: String)
}
}

Expand All @@ -27,6 +28,8 @@ extension Service.Router {
"\(Service.shared.ipInfoURL)/\(asn)/json"
case .batch(let withFilter):
"\(Service.shared.ipInfoURL)/batch" + (withFilter ? "&filter=1" : "")
case .resproxy(let ipAddress):
"\(Service.shared.ipInfoURL)/resproxy/\(ipAddress)"
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions Sources/ipinfoKit/Model/ResproxyResponse/ResproxyResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// ResproxyResponse.swift
//
//
//

import Foundation

public struct ResproxyResponse: Codable, Sendable {

// MARK: Lifecycle

public init(
ip: String?,
lastSeen: String?,
percentDaysSeen: Double?,
service: String?
) {
self.ip = ip
self.lastSeen = lastSeen
self.percentDaysSeen = percentDaysSeen
self.service = service
}

// MARK: Internal

enum CodingKeys: String, CodingKey {
case ip
case lastSeen = "last_seen"
case percentDaysSeen = "percent_days_seen"
case service
}

public let ip: String?
public let lastSeen: String?
public let percentDaysSeen: Double?
public let service: String?
}
31 changes: 31 additions & 0 deletions Sources/ipinfoKit/Resproxy/Resproxy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Resproxy.swift
//
//
//

import Foundation

extension IPINFO {
public func getResproxy(
ip: String,
completion:
@escaping (_ status: Response, _ data: ResproxyResponse?, _ msg: String?) -> Void
) {
Service.shared.requestAPI(URL: .resproxy(ipAddress: ip), method: .get) {
status, data, msg in
switch status {
case .success:
do {
let decoder = JSONDecoder()
let response = try decoder.decode(ResproxyResponse.self, from: data)
completion(.success, response, nil)
} catch {
completion(.failure, nil, error.localizedDescription)
}
case .failure:
completion(.failure, nil, msg)
}
}
}
}
62 changes: 62 additions & 0 deletions Tests/ipinfoKitTests/ResproxyTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Foundation
import Testing
import ipinfoKit

@MainActor
struct ResproxyTests {
@Test func resproxyTest() async throws {
let response = try await withCheckedThrowingContinuation { continuation in
IPINFO.shared.getResproxy(ip: "175.107.211.204") { status, response, msg in
switch status {
case .success:
if let response = response {
continuation.resume(returning: response)
} else {
continuation.resume(
throwing: NSError(
domain: "ResproxyTests", code: 1,
userInfo: [NSLocalizedDescriptionKey: "Response is nil"]))
}
case .failure:
continuation.resume(
throwing: NSError(
domain: "ResproxyTests", code: 2,
userInfo: [NSLocalizedDescriptionKey: msg ?? "Unknown error"]))
}
}
}

#expect(response.ip == "175.107.211.204")
#expect(response.lastSeen != nil)
#expect(response.percentDaysSeen != nil)
#expect(response.service != nil)
}

@Test func resproxyEmptyTest() async throws {
let response = try await withCheckedThrowingContinuation { continuation in
IPINFO.shared.getResproxy(ip: "8.8.8.8") { status, response, msg in
switch status {
case .success:
if let response = response {
continuation.resume(returning: response)
} else {
continuation.resume(
throwing: NSError(
domain: "ResproxyTests", code: 1,
userInfo: [NSLocalizedDescriptionKey: "Response is nil"]))
}
case .failure:
continuation.resume(
throwing: NSError(
domain: "ResproxyTests", code: 2,
userInfo: [NSLocalizedDescriptionKey: msg ?? "Unknown error"]))
}
}
}

#expect(response.ip == nil)
#expect(response.lastSeen == nil)
#expect(response.percentDaysSeen == nil)
#expect(response.service == nil)
}
}