From bd396c3b783157a9d0bb08c882eddad16dc10a8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=B4=E9=93=AD?= Date: Tue, 9 Apr 2024 16:43:32 +0800 Subject: [PATCH] clean useless config --- SwiftPamphletApp.xcodeproj/project.pbxproj | 12 - .../App/SwiftPamphletAppConfig.swift | 64 +--- .../GitHubAPI/DetailView/GuideView.swift | 1 - .../GitHubAPI/DetailView/RepoView.swift | 1 - SwiftPamphletApp/Network/CCYRESTfulAPI.swift | 292 ------------------ 5 files changed, 1 insertion(+), 369 deletions(-) delete mode 100644 SwiftPamphletApp/Network/CCYRESTfulAPI.swift diff --git a/SwiftPamphletApp.xcodeproj/project.pbxproj b/SwiftPamphletApp.xcodeproj/project.pbxproj index b7f364ed9..cd16fc231 100644 --- a/SwiftPamphletApp.xcodeproj/project.pbxproj +++ b/SwiftPamphletApp.xcodeproj/project.pbxproj @@ -24,7 +24,6 @@ 08026C512869B43500792EF1 /* 190.md in Resources */ = {isa = PBXBuildFile; fileRef = 08026C502869B41B00792EF1 /* 190.md */; }; 08026C522869B43D00792EF1 /* 191.md in Resources */ = {isa = PBXBuildFile; fileRef = 08026C3E2869AD7800792EF1 /* 191.md */; }; 08026C532869B44400792EF1 /* 192.md in Resources */ = {isa = PBXBuildFile; fileRef = 08026C3F2869B00D00792EF1 /* 192.md */; }; - 08038767276700F100519B15 /* CCYRESTfulAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08038766276700F100519B15 /* CCYRESTfulAPI.swift */; }; 0805F4962BAAABEA0008BB52 /* ViewStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0805F4952BAAABEA0008BB52 /* ViewStyle.swift */; }; 083554E12756503B0095E0EE /* AnimateLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 083554E02756503B0095E0EE /* AnimateLayout.swift */; }; 08397E232B9EE8F400DFDD02 /* InfoDataModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08397E222B9EE8F400DFDD02 /* InfoDataModel.swift */; }; @@ -280,7 +279,6 @@ 08026C402869B1BF00792EF1 /* 183.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = 183.md; sourceTree = ""; }; 08026C422869B22E00792EF1 /* 176.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = 176.md; sourceTree = ""; }; 08026C502869B41B00792EF1 /* 190.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = 190.md; sourceTree = ""; }; - 08038766276700F100519B15 /* CCYRESTfulAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CCYRESTfulAPI.swift; sourceTree = ""; }; 0805F4952BAAABEA0008BB52 /* ViewStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewStyle.swift; sourceTree = ""; }; 083554E02756503B0095E0EE /* AnimateLayout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnimateLayout.swift; sourceTree = ""; }; 08397E222B9EE8F400DFDD02 /* InfoDataModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoDataModel.swift; sourceTree = ""; }; @@ -551,14 +549,6 @@ path = APMAndBuilder; sourceTree = ""; }; - 080387652766FF3200519B15 /* Network */ = { - isa = PBXGroup; - children = ( - 08038766276700F100519B15 /* CCYRESTfulAPI.swift */, - ); - path = Network; - sourceTree = ""; - }; 08397E212B9EE83F00DFDD02 /* InfoOrganizer */ = { isa = PBXGroup; children = ( @@ -1081,7 +1071,6 @@ 08ED801A2B9D1EDA0069B7EC /* Setting */, 080124FD27EC62DC00E44222 /* SwiftPamphletAppDebug.entitlements */, 08CD61FB27758B22008C0935 /* Core */, - 080387652766FF3200519B15 /* Network */, 086A5F482744EF2B00FECE02 /* ViewComponet */, 086A5F472744EF0800FECE02 /* Resource */, 086A5F0A2744E89100FECE02 /* Assets.xcassets */, @@ -1626,7 +1615,6 @@ 08BF26DA276B65160064DDAC /* CCYGitHubAPI.swift in Sources */, 08C411F227951152006FC340 /* PlayFoundation.swift in Sources */, 08448F0B2797F73200B61353 /* PlayArchitecture.swift in Sources */, - 08038767276700F100519B15 /* CCYRESTfulAPI.swift in Sources */, 08522BD827CF344B005FF059 /* PlaySliderView.swift in Sources */, 08A9E1A22BC25D0700A73764 /* ViewComponentMarkdown.swift in Sources */, 0887A59A2BA28F6D00131359 /* CSGuideView.swift in Sources */, diff --git a/SwiftPamphletApp/App/SwiftPamphletAppConfig.swift b/SwiftPamphletApp/App/SwiftPamphletAppConfig.swift index 4737bfb28..e991f080b 100644 --- a/SwiftPamphletApp/App/SwiftPamphletAppConfig.swift +++ b/SwiftPamphletApp/App/SwiftPamphletAppConfig.swift @@ -17,34 +17,12 @@ struct SPC { static let detailMinWidth: CGFloat = 550 static let githubHost = "https://github.com/" - static let pamphletIssueRepoName = "ming1016/SwiftPamphletApp" - - static let timerForDevsSec: Double = 160 - static let timerForExpSec: Double = 125 - static let timerForRssSec: Double = 60 * 60 - - static let unreadMagicNumber = 99999 static func loadCustomIssues(jsonFileName: String) -> [CustomIssuesModel] { let lc: [CustomIssuesModel] = loadBundleJSONFile(jsonFileName + ".json") return lc } - - static func activeDevelopers() -> [SPActiveDevelopersModel] { - let ad: [SPActiveDevelopersModel] = loadBundleJSONFile("developers.json") - return ad - } - - static func repos() -> [SPReposModel] { - let re: [SPReposModel] = loadBundleJSONFile("repos.json") - return re - } - - static func rssFeed() -> [RSSFeedModel] { - let re: [RSSFeedModel] = loadBundleJSONFile("rssfeed.json") - return re - } - + static func rssStyle() -> String { let data = loadBundleData("css_cn.html") return String(data: data, encoding: .utf8) ?? "" @@ -54,45 +32,5 @@ struct SPC { let data = loadBundleData("footer_js.html") return String(data: data, encoding: .utf8) ?? "" } - - static func outputRepo() { - let re = repos() - for r in re { - print("#### \(r.name)") - for ar in r.repos { - let arr = ar.id.components(separatedBy: "/") - print("* \(arr[1]):\(ar.des ?? "") (https://github.com/\(ar.id))") - } - } - } -} - -struct SPActiveDevelopersModel: Jsonable { - var id: Int64 - var name: String - var users: [ADeveloperModel] -} - -struct ADeveloperModel: Jsonable { - var id: String - var des: String? } -struct SPReposModel: Jsonable { - var id: Int64 - var name: String - var repos: [ARepoModel] -} - -struct ARepoModel: Jsonable { - var id: String - var des: String? -} - -struct RSSFeedModel: Jsonable { - var id: Int64 - var title: String - var des: String - var siteLink: String - var feedLink: String -} diff --git a/SwiftPamphletApp/GitHubAPI/DetailView/GuideView.swift b/SwiftPamphletApp/GitHubAPI/DetailView/GuideView.swift index ec8b71307..39b80a7d5 100644 --- a/SwiftPamphletApp/GitHubAPI/DetailView/GuideView.swift +++ b/SwiftPamphletApp/GitHubAPI/DetailView/GuideView.swift @@ -19,6 +19,5 @@ struct GuideView: View { } .padding(EdgeInsets(top: 10, leading: 10, bottom: 2, trailing: 10)) WebUIView(html: wrapperHtmlContent(content: MarkdownParser().html(from: "\(loadBundleString("\(number)" + ".md"))")), baseURLStr: "") - .frame(minWidth: SPC.detailMinWidth) } } diff --git a/SwiftPamphletApp/GitHubAPI/DetailView/RepoView.swift b/SwiftPamphletApp/GitHubAPI/DetailView/RepoView.swift index 77c4d7dd7..adbe9d28a 100644 --- a/SwiftPamphletApp/GitHubAPI/DetailView/RepoView.swift +++ b/SwiftPamphletApp/GitHubAPI/DetailView/RepoView.swift @@ -53,7 +53,6 @@ struct RepoCommitsView: View { RepoCommitLabelView(repo: repo, commit: commit, isUnRead: unReadCount > 0 && i < unReadCount) } // end ForEach } // end List - .frame(minWidth: SPC.detailMinWidth) } // end body } diff --git a/SwiftPamphletApp/Network/CCYRESTfulAPI.swift b/SwiftPamphletApp/Network/CCYRESTfulAPI.swift deleted file mode 100644 index 5a8602df4..000000000 --- a/SwiftPamphletApp/Network/CCYRESTfulAPI.swift +++ /dev/null @@ -1,292 +0,0 @@ -// -// RESTfulAPI.swift -// SwiftPamphletApp -// -// Created by Ming Dai on 2021/12/13. -// - -import Foundation -import SwiftUI -/// 参考: https://kean.blog/post/new-api-client - -// MARK: - RESTful -public actor RESTful { - enum Host: String { - case github = "api.github.com" - } - - private let conf: Conf - private let session: URLSession - private let serializer: Serializer - - convenience init(host: Host, conf: URLSessionConfiguration = .default) { - self.init(conf: Conf(host: host, sessionConf: conf)) - } - - public init(conf: Conf) { - self.conf = conf - self.session = URLSession(configuration: conf.sessionConf) - self.serializer = Serializer() - } - // MARK: - Conf - public struct Conf { - var host: Host - var port: Int? - var isInsecure = false // false 用 https,true 为 http - var sessionConf: URLSessionConfiguration = .default - - init(host: Host, port: Int? = nil, isInsecure: Bool = false, sessionConf: URLSessionConfiguration = .default, decoder: JSONDecoder? = nil, encoder: JSONEncoder? = nil) { - self.host = host - self.port = port - self.isInsecure = isInsecure - self.sessionConf = sessionConf - } // end init - } // end struct Conf - - // MARK: - Return Value - public func value(for req: Req) async throws -> T { - try await send(req).value - } - -} -extension RESTful { - - // MARK: - send - public func send(_ req: Req) async throws -> Res { - try await send(req) { data in - if T.self == Data.self { - return data as! T - } else if T.self == String.self { - guard let string = String(data: data, encoding: .utf8) else { - throw URLError(.badServerResponse) - } - return string as! T - } else { - return try await self.serializer.decode(data) - } - } - } - - @discardableResult - public func send(_ req: Req) async throws -> Res { - try await send(req) { _ in () } - } - - private func send(_ req: Req, _ decode: @escaping (Data) async throws -> T) async throws -> Res { - let res = try await data(for: req) - let value = try await decode(res.value) - return res.map { _ in - value - } - } - - public func data(for req: Req) async throws -> Res { - let req = try await makeRequest(for: req) - return try await send(req) - } - - private func send(_ req: URLRequest) async throws -> Res { - do { - return try await actuallySend(req) - } catch { - throw error - } - } - - private func actuallySend(_ req: URLRequest) async throws -> Res { - let (data, res) = try await session.data(for: req, delegate: nil) - try validate(res: res, data: data) - let hRes = (res as? HTTPURLResponse) ?? HTTPURLResponse() - return Res(value: data, data: data, req: req, res: hRes, sCode: hRes.statusCode) - } - - // MARK: - Make - private func makeRequest(for req: Req) async throws -> URLRequest { - let url = try makeURL(path: req.path, query: req.query) - return try await makeRequest(url: url, method: req.method, body: req.body, headers: req.headers) - } - private func makeRequest(url: URL, method: String, body: AnyEncodable?, headers: [String: String]?) async throws -> URLRequest { - var req = URLRequest(url: url) - req.allHTTPHeaderFields = headers - req.httpMethod = method - if let body = body { - req.httpBody = try await serializer.encode(body) - req.setValue("application/json", forHTTPHeaderField: "Content-Type") - } - req.setValue("application/json", forHTTPHeaderField: "Accept") - // 不同平台接口的 token - // 处理 github token - var githubat = "" - if SPC.gitHubAccessToken.isEmpty == true { - githubat = SPC.githubAccessToken() - } else { - githubat = SPC.gitHubAccessToken - } - - // 不同情况的设置 - switch self.conf.host { - case .github: - req.setValue("token \(githubat)", forHTTPHeaderField: "Authorization") - } - - return req - } - private func makeURL(path: String, query: [(String, String?)]?) throws -> URL { - guard let url = URL(string: path), var comps = URLComponents(url: url, resolvingAgainstBaseURL: false) else { - throw URLError(.badURL) - } - if path.starts(with: "/") { - comps.scheme = conf.isInsecure ? "http" : "https" - comps.host = conf.host.rawValue - if let port = conf.port { - comps.port = port - } - } - if let query = query { - comps.queryItems = query.map(URLQueryItem.init) - } - guard let url = comps.url else { - throw URLError(.badURL) - } - return url - } - private func validate(res: URLResponse, data: Data) throws { - guard let hRes = res as? HTTPURLResponse else { - return - } - if !(200..<300).contains(hRes.statusCode) { - print("Wrong, statusCode is \(hRes.statusCode)") - throw URLError(.badServerResponse) - } - } -} - -public enum RESTfulError: Error, LocalizedError { - case wrongStateCode(Int) - public var des: String? { - switch self { - case .wrongStateCode(let sCode): - return "错误的状态码 \(sCode)" - } - } -} - -// MARK: - Serializer -private actor Serializer { - private let decoder: JSONDecoder - private let encoder: JSONEncoder - init() { - self.decoder = JSONDecoder() - self.decoder.keyDecodingStrategy = .convertFromSnakeCase - self.decoder.dateDecodingStrategy = .iso8601 - - self.encoder = JSONEncoder() - self.encoder.dateEncodingStrategy = .iso8601 - } - - func decode(_ data: Data) async throws -> T { - try decoder.decode(T.self, from: data) - } - func encode(_ entity: T) async throws -> Data { - try encoder.encode(entity) - } -} - -// MARK: - 请求和响应 - -public struct Req { - public typealias ReqQType = [(String, String?)]? - public typealias ReqHType = [String: String]? - - public var method: String - public var path: String - public var query: ReqQType - var body: AnyEncodable? - public var headers: ReqHType - public var id: String? - - public static func get(_ path: String, query: ReqQType = nil, headers: ReqHType = nil) -> Req { - Req(method: "GET", path: path, query: query, headers: headers) // GET请求传 body URLSession 会报错 - } - - public static func post(_ path: String, query: ReqQType = nil, headers: ReqHType = nil) -> Req { - Req(method: "POST", path: path, query: query, headers: headers) - } - - public static func post(_ path: String, query: ReqQType = nil, body: U?, headers: ReqHType = nil) -> Req { - Req(method: "POST", path: path, query: query, body: body.map(AnyEncodable.init), headers: headers) - } - - public static func put(_ path: String, query: ReqQType = nil, headers: ReqHType = nil) -> Req { - Req(method: "PUT", path: path, query: query, headers: headers) - } - public static func put(_ path: String, query: ReqQType = nil, body: U?, headers: ReqHType = nil) -> Req { - Req(method: "PUT", path: path, query: query, body: body.map(AnyEncodable.init), headers: headers) - } - public static func patch(_ path: String, query: ReqQType = nil, headers: ReqHType = nil) -> Req { - Req(method: "PATCH", path: path, query: query, headers: headers) - } - public static func patch(_ path: String, query: ReqQType = nil, body: U?, headers: ReqHType = nil) -> Req { - Req(method: "PATCH", path: path, query: query, body: body.map(AnyEncodable.init), headers: headers) - } - public static func delete(_ path: String, query: ReqQType = nil, headers: ReqHType = nil) -> Req { - Req(method: "DELETE", path: path, query: query, headers: headers) - } - public static func delete(_ path: String, query: ReqQType = nil, body: U?, headers: ReqHType = nil) -> Req { - Req(method: "DELETE", path: path, query: query, body: body.map(AnyEncodable.init), headers: headers) - } - public static func options(_ path: String, query: ReqQType = nil, headers: ReqHType = nil) -> Req { - Req(method: "OPTIONS", path: path, query: query, headers: headers) - } - public static func head(_ path: String, query: ReqQType = nil, headers: ReqHType = nil) -> Req { - Req(method: "HEAD", path: path, query: query, headers: headers) - } - public static func trace(_ path: String, query: ReqQType = nil, headers: ReqHType = nil) -> Req { - Req(method: "TRACE", path: path, query: query, headers: headers) - } -} - -struct AnyEncodable: Encodable { - private let value: Encodable - init(_ value: Encodable) { - self.value = value - } - func encode(to encoder: Encoder) throws { - try value.encode(to: encoder) - } -} - -public struct Res { - public let value: T - public let data: Data // 原始数据 - public let req: URLRequest - public let res: HTTPURLResponse - public let sCode: Int - - // 通过闭包生成指定类型 - func map(_ closure: (T) -> U) -> Res { - Res(value: closure(value), data: data, req: req, res: res, sCode: sCode) - } -} - -extension URLRequest { - public func cURLDes() -> String { - guard let url = url, let method = httpMethod else { - return "$ curl command generation failed" - } - var comps = ["curl -v"] - comps.append("-X \(method)") - for hd in allHTTPHeaderFields ?? [:] { - let v = hd.value.replacingOccurrences(of: "\"", with: "\\\"") - comps.append("-H \"\(hd.key): \(v)\"") - } - if let hBData = httpBody { - let httpBody = String(decoding: hBData, as: UTF8.self) - var eB = httpBody.replacingOccurrences(of: "\\\"", with: "\\\\\"") - eB = eB.replacingOccurrences(of: "\"", with: "\\\"") - comps.append("-d \"\(eB)\"") - } - comps.append("\"\(url.absoluteString)\"") - return comps.joined(separator: " \\\n\t") - } -}