forked from twodayslate/NetUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master' into safeAreaObserving…
…Branch
- Loading branch information
Showing
10 changed files
with
296 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
ec3730/Data Feeds/WhoisXML/WhoIsXmlCategorizationResult.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import Foundation | ||
|
||
// MARK: - ActivityModelClass | ||
|
||
struct WhoIsXmlCategorizationResult: Codable { | ||
let categories: [CategoryResult]? | ||
let domainName: String? | ||
let websiteResponded: Bool? | ||
} | ||
|
||
extension WhoIsXmlCategorizationResult { | ||
init(data: Data) throws { | ||
self = try newJSONDecoder().decode(WhoIsXmlCategorizationResult.self, from: data) | ||
} | ||
|
||
init(_ json: String, using encoding: String.Encoding = .utf8) throws { | ||
guard let data = json.data(using: encoding) else { | ||
throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil) | ||
} | ||
try self.init(data: data) | ||
} | ||
|
||
init(fromURL url: URL) throws { | ||
try self.init(data: try Data(contentsOf: url)) | ||
} | ||
|
||
func with(categories: [CategoryResult]? = nil, | ||
domainName: String? = nil, | ||
websiteResponded: Bool? = nil) -> WhoIsXmlCategorizationResult { | ||
WhoIsXmlCategorizationResult(categories: categories ?? self.categories, | ||
domainName: domainName ?? self.domainName, | ||
websiteResponded: websiteResponded ?? self.websiteResponded) | ||
} | ||
|
||
func jsonData() throws -> Data { | ||
try newJSONEncoder().encode(self) | ||
} | ||
|
||
func jsonString(encoding: String.Encoding = .utf8) throws -> String? { | ||
String(data: try jsonData(), encoding: encoding) | ||
} | ||
} | ||
|
||
// MARK: - CategoryResult | ||
|
||
struct CategoryResult: Codable { | ||
let tier1: Tier? | ||
let tier2: Tier? | ||
} | ||
|
||
extension CategoryResult { | ||
init(data: Data) throws { | ||
self = try newJSONDecoder().decode(CategoryResult.self, from: data) | ||
} | ||
|
||
init(_ json: String, using encoding: String.Encoding = .utf8) throws { | ||
guard let data = json.data(using: encoding) else { | ||
throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil) | ||
} | ||
try self.init(data: data) | ||
} | ||
|
||
init(fromURL url: URL) throws { | ||
try self.init(data: try Data(contentsOf: url)) | ||
} | ||
|
||
func with(tier1: Tier? = nil, | ||
tier2: Tier? = nil) -> CategoryResult { | ||
CategoryResult(tier1: tier1 ?? self.tier1, tier2: tier2 ?? self.tier2) | ||
} | ||
|
||
func jsonData() throws -> Data { | ||
try newJSONEncoder().encode(self) | ||
} | ||
|
||
func jsonString(encoding: String.Encoding = .utf8) throws -> String? { | ||
String(data: try jsonData(), encoding: encoding) | ||
} | ||
} | ||
|
||
// MARK: - Tier | ||
|
||
struct Tier: Codable { | ||
let confidence: Double? | ||
let id, name: String? | ||
} |
29 changes: 29 additions & 0 deletions
29
ec3730/Data Feeds/WhoisXML/WhoIsXmlCategorizationService.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// WhoIsXmlCategorizationService.swift | ||
// ec3730 | ||
// | ||
// Created by admin on 04/11/22. | ||
// Copyright © 2022 Zachary Gorak. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
class WhoIsXmlCategorizationService: WhoisXMLService { | ||
override func endpoint(_ userData: [String: Any?]?) -> DataFeedEndpoint? { | ||
guard let userData = userData, let userInput = userData["domain"] as? String, let domain = userInput.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) else { | ||
return nil | ||
} | ||
|
||
var params = [URLQueryItem(name: "domainName", value: domain), | ||
URLQueryItem(name: "outputFormat", value: "JSON"), | ||
URLQueryItem(name: "type", value: "_all"), | ||
URLQueryItem(name: "api", value: "whoisXmlWebsiteCategorization"), | ||
URLQueryItem(name: "identifierForVendor", value: UIDevice.current.identifierForVendor?.uuidString), | ||
URLQueryItem(name: "bundleIdentifier", value: Bundle.main.bundleIdentifier)] | ||
if let key = WhoisXml.current.userKey { | ||
params.append(URLQueryItem(name: "apiKey", value: key)) | ||
} | ||
return WhoisXml.Endpoint(host: "api.netutils.workers.dev", path: "/api/v2", queryItems: params) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{ | ||
"categories": [{ | ||
"tier1": { | ||
"confidence": 0.9750956680990432, | ||
"id": "IAB-596", | ||
"name": "Technology & Computing" | ||
}, | ||
"tier2": { | ||
"confidence": 0.9242695836674197, | ||
"id": "IAB-619", | ||
"name": "Internet" | ||
} | ||
}, { | ||
"tier1": { | ||
"confidence": 0.9750956680990432, | ||
"id": "IAB-52", | ||
"name": "Business and Finance" | ||
}, | ||
"tier2": { | ||
"confidence": 0.9242695836674197, | ||
"id": "IAB-99", | ||
"name": "Information Services Industry" | ||
} | ||
}, { | ||
"tier1": { | ||
"confidence": 0.9750956680990432, | ||
"id": "IAB-52", | ||
"name": "Business and Finance" | ||
}, | ||
"tier2": { | ||
"confidence": 0.9242695836674197, | ||
"id": "IAB-115", | ||
"name": "Technology Industry" | ||
} | ||
}, { | ||
"tier1": { | ||
"confidence": 0.9750956680990432, | ||
"id": "IAB-52", | ||
"name": "Business and Finance" | ||
}, | ||
"tier2": { | ||
"confidence": 0.9242695836674197, | ||
"id": "IAB-116", | ||
"name": "Telecommunications Industry" | ||
} | ||
}], | ||
"domainName": "google.com", | ||
"websiteResponded": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
ec3730/Models/Sections/WhoIsXmlCategorizationSectionModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import Cache | ||
import Foundation | ||
|
||
@MainActor | ||
class WhoIsXmlCategorizationSectionModel: HostSectionModel { | ||
required convenience init() { | ||
self.init(WhoisXml.current, service: WhoisXml.CategorizationService, storeModel: StoreKitModel.categorization) | ||
} | ||
|
||
@MainActor | ||
override func configure(with data: Data) throws -> Data? { | ||
reset() | ||
|
||
let result = try JSONDecoder().decode(WhoIsXmlCategorizationResult.self, from: data) | ||
|
||
return try configure(with: result) | ||
} | ||
|
||
@MainActor | ||
func configure(with records: WhoIsXmlCategorizationResult) throws -> Data { | ||
reset() | ||
|
||
let copyData = try JSONEncoder().encode(records) | ||
latestData = copyData | ||
dataToCopy = String(data: copyData, encoding: .utf8) | ||
|
||
if let categories = records.categories { | ||
for (index, category) in categories.enumerated() { | ||
var rows = [CopyCellRow]() | ||
if let tier1 = category.tier1 { | ||
let name = tier1.name ?? "" | ||
let confidence = tier1.confidence ?? 0.0 | ||
let id = tier1.id ?? "" | ||
|
||
rows.append(CopyCellRow(title: "Tier1", content: "Name - \(name)\n Id - \(id)\n Confidence - \(confidence)")) | ||
} | ||
|
||
if let tier2 = category.tier2 { | ||
let name = tier2.name ?? "" | ||
let confidence = tier2.confidence ?? 0.0 | ||
let id = tier2.id ?? "" | ||
rows.append(CopyCellRow(title: "Tier2", content: "Name - \(name)\n Id - \(id)\n Confidence - \(confidence)")) | ||
} | ||
|
||
if !rows.isEmpty { | ||
content.append(CopyCellView(title: "Category \(index + 1)", rows: rows)) | ||
} | ||
} | ||
} | ||
|
||
return copyData | ||
} | ||
|
||
private let cache = MemoryStorage<String, WhoIsXmlCategorizationResult>(config: .init(expiry: .seconds(15), countLimit: 3, totalCostLimit: 0)) | ||
|
||
@discardableResult | ||
override func query(url: URL? = nil) async throws -> Data { | ||
reset() | ||
|
||
guard let host = url?.host else { | ||
throw URLError(.badURL) | ||
} | ||
latestQueriedUrl = url | ||
latestQueryDate = .now | ||
|
||
if let record = try? cache.object(forKey: host) { | ||
return try configure(with: record) | ||
} | ||
|
||
guard dataFeed.userKey != nil || storeModel?.owned ?? false else { | ||
throw MoreStoreKitError.NotPurchased | ||
} | ||
|
||
let response: WhoIsXmlCategorizationResult = try await WhoisXml.CategorizationService.query( | ||
[ | ||
"domain": host, | ||
"minimumBalance": 25, | ||
] | ||
) | ||
|
||
cache.setObject(response, forKey: host) | ||
|
||
return try configure(with: response) | ||
} | ||
} |
Oops, something went wrong.