Skip to content

Commit 2c32bf8

Browse files
committed
clean old api fetch
1 parent 270c6da commit 2c32bf8

File tree

5 files changed

+197
-307
lines changed

5 files changed

+197
-307
lines changed

SwiftPamphletApp/GitHubAPI/ListView/IssuesListFromCustomView.swift

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ struct IssuesListFromCustomView: View {
2727

2828
}
2929
}
30-
.alert(vm.errMsg, isPresented: $vm.errHint, actions: {})
3130
.onAppear {
3231
vm.doing(.customIssues)
3332
}

SwiftPamphletApp/GitHubAPI/Network/APIRequest.swift

+87-87
Original file line numberDiff line numberDiff line change
@@ -5,90 +5,90 @@
55
// Created by Ming Dai on 2021/11/8.
66
//
77

8-
import Foundation
9-
import Combine
10-
11-
// MARK: - API Request Fundation
12-
13-
protocol APIReqType {
14-
associatedtype Res: Decodable
15-
var path: String { get }
16-
var qItems: [URLQueryItem]? { get }
17-
}
18-
19-
protocol APIVMable: ObservableObject {
20-
associatedtype ActionType
21-
func doing(_ somethinglike: ActionType)
22-
}
23-
24-
protocol APISevType {
25-
func response<Request>(from req: Request) -> AnyPublisher<Request.Res, APISevError> where Request: APIReqType
26-
}
27-
28-
final class APISev: APISevType {
29-
private let rootUrl: URL
30-
31-
init(rootUrl: URL = URL(string: "https://api.github.com")!) {
32-
self.rootUrl = rootUrl
33-
}
34-
35-
func response<Request>(from req: Request) -> AnyPublisher<Request.Res, APISevError> where Request : APIReqType {
36-
let path = URL(string: req.path, relativeTo: rootUrl)!
37-
var comp = URLComponents(url: path, resolvingAgainstBaseURL: true)!
38-
comp.queryItems = req.qItems
39-
// print(comp.url?.description ?? "url wrong")
40-
var req = URLRequest(url: comp.url!)
41-
42-
// token 处理
43-
// TODO: 支持 OAuth
44-
// TODO: 访问受限后会crash,异常待处理
45-
var githubat = ""
46-
if SPC.gitHubAccessToken.isEmpty == true {
47-
githubat = SPC.githubAccessToken()
48-
} else {
49-
githubat = SPC.gitHubAccessToken
50-
}
51-
52-
req.addValue("token \(githubat)", forHTTPHeaderField: "Authorization")
53-
54-
// print(req.allHTTPHeaderFields!)
55-
let de = JSONDecoder()
56-
de.keyDecodingStrategy = .convertFromSnakeCase
57-
let sch = DispatchQueue(label: "GitHub API Queue", qos: .default, attributes: .concurrent)
58-
return URLSession.shared.dataTaskPublisher(for: req)
59-
.retry(3)
60-
.subscribe(on: sch)
61-
.receive(on: sch)
62-
.map { data, _ in
63-
// print(String(decoding: data, as: UTF8.self))
64-
// print(res.description)
65-
// 打印api访问额度
66-
// let hres = res as! HTTPURLResponse
67-
// print(hres.value(forHTTPHeaderField: "x-ratelimit-remaining") ?? "none")
68-
return data
69-
}
70-
.mapError { _ in
71-
APISevError.resError
72-
}
73-
.decode(type: Request.Res.self, decoder: de)
74-
.mapError { _ in
75-
APISevError.parseError
76-
}
77-
.receive(on: RunLoop.main)
78-
.eraseToAnyPublisher()
79-
}
80-
}
81-
82-
enum APISevError: Error {
83-
case resError
84-
case parseError
85-
86-
var message: String {
87-
switch self {
88-
case .resError:
89-
return "网络无法访问"
90-
case .parseError:
91-
return "网络出错"
92-
}
93-
}
94-
}
8+
//import Foundation
9+
//import Combine
10+
//
11+
//// MARK: - API Request Fundation
12+
//
13+
//protocol APIReqType {
14+
// associatedtype Res: Decodable
15+
// var path: String { get }
16+
// var qItems: [URLQueryItem]? { get }
17+
//}
18+
//
19+
//protocol APIVMable: ObservableObject {
20+
// associatedtype ActionType
21+
// func doing(_ somethinglike: ActionType)
22+
//}
23+
//
24+
//protocol APISevType {
25+
// func response<Request>(from req: Request) -> AnyPublisher<Request.Res, APISevError> where Request: APIReqType
26+
//}
27+
//
28+
//final class APISev: APISevType {
29+
// private let rootUrl: URL
30+
//
31+
// init(rootUrl: URL = URL(string: "https://api.github.com")!) {
32+
// self.rootUrl = rootUrl
33+
// }
34+
//
35+
// func response<Request>(from req: Request) -> AnyPublisher<Request.Res, APISevError> where Request : APIReqType {
36+
// let path = URL(string: req.path, relativeTo: rootUrl)!
37+
// var comp = URLComponents(url: path, resolvingAgainstBaseURL: true)!
38+
// comp.queryItems = req.qItems
39+
//// print(comp.url?.description ?? "url wrong")
40+
// var req = URLRequest(url: comp.url!)
41+
//
42+
// // token 处理
43+
// // TODO: 支持 OAuth
44+
// // TODO: 访问受限后会crash,异常待处理
45+
// var githubat = ""
46+
// if SPC.gitHubAccessToken.isEmpty == true {
47+
// githubat = SPC.githubAccessToken()
48+
// } else {
49+
// githubat = SPC.gitHubAccessToken
50+
// }
51+
//
52+
// req.addValue("token \(githubat)", forHTTPHeaderField: "Authorization")
53+
//
54+
//// print(req.allHTTPHeaderFields!)
55+
// let de = JSONDecoder()
56+
// de.keyDecodingStrategy = .convertFromSnakeCase
57+
// let sch = DispatchQueue(label: "GitHub API Queue", qos: .default, attributes: .concurrent)
58+
// return URLSession.shared.dataTaskPublisher(for: req)
59+
// .retry(3)
60+
// .subscribe(on: sch)
61+
// .receive(on: sch)
62+
// .map { data, _ in
63+
//// print(String(decoding: data, as: UTF8.self))
64+
//// print(res.description)
65+
// // 打印api访问额度
66+
//// let hres = res as! HTTPURLResponse
67+
//// print(hres.value(forHTTPHeaderField: "x-ratelimit-remaining") ?? "none")
68+
// return data
69+
// }
70+
// .mapError { _ in
71+
// APISevError.resError
72+
// }
73+
// .decode(type: Request.Res.self, decoder: de)
74+
// .mapError { _ in
75+
// APISevError.parseError
76+
// }
77+
// .receive(on: RunLoop.main)
78+
// .eraseToAnyPublisher()
79+
// }
80+
//}
81+
//
82+
//enum APISevError: Error {
83+
// case resError
84+
// case parseError
85+
//
86+
// var message: String {
87+
// switch self {
88+
// case .resError:
89+
// return "网络无法访问"
90+
// case .parseError:
91+
// return "网络出错"
92+
// }
93+
// }
94+
//}

SwiftPamphletApp/GitHubAPI/Network/CCYGitHubAPI.swift

+92-92
Original file line numberDiff line numberDiff line change
@@ -5,95 +5,95 @@
55
// Created by Ming Dai on 2021/12/16.
66
//
77

8-
import Foundation
9-
import SwiftUI
10-
11-
enum Github {}
12-
13-
// MARK: - /repos/{reponame}
14-
extension Github {
15-
static func repos(_ name: String) -> Repos {
16-
Repos(path: "/repos/\(name)")
17-
}
18-
struct Repos {
19-
let path: String
20-
var get: Req<RepoModel> {
21-
.get(path)
22-
}
23-
}
24-
}
25-
// MARK: - /repos/{reponame}/issues/{issuenumber}
26-
extension Github.Repos {
27-
func issues(_ number: Int) -> Issues {
28-
Issues(path: path + "/issues/\(number)")
29-
}
30-
struct Issues {
31-
let path: String
32-
var get: Req<IssueModel> {
33-
.get(path)
34-
}
35-
}
36-
}
37-
// MARK: - /repos/{reponame}/commits
38-
extension Github.Repos {
39-
var commits: Commits {
40-
Commits(path: path + "/commits", query: [("per_page", "100")])
41-
}
42-
43-
struct Commits {
44-
let path: String
45-
let query: [(String, String?)]?
46-
var get: Req<[CommitModel]> {
47-
.get(path, query: query)
48-
}
49-
}
50-
}
51-
52-
// MARK: - /user
53-
extension Github {
54-
static var user: User {
55-
User()
56-
}
57-
struct User {
58-
let path: String = "/user"
59-
var get: Req<UserModel> {
60-
.get(path)
61-
}
62-
}
63-
}
64-
// MARK: - /user/following
65-
extension Github.User {
66-
var following: Following {
67-
Following()
68-
}
69-
struct Following {
70-
let path: String = "/user/following"
71-
var get: Req<[GitUserModel]> {
72-
.get(path)
73-
}
74-
}
75-
}
76-
// MARK: - /users/{username}
77-
extension Github {
78-
static func users(_ name: String) -> Users {
79-
Users(path: "/users/\(name)")
80-
}
81-
struct Users {
82-
let path: String
83-
var get: Req<UserModel> {
84-
.get(path)
85-
}
86-
}
87-
}
88-
// MARK: - /users/{username}/followers
89-
extension Github.Users {
90-
var followers: Followers {
91-
Followers(path: path + "/follower")
92-
}
93-
struct Followers {
94-
let path: String
95-
var get: Req<[GitUserModel]> {
96-
.get(path)
97-
}
98-
}
99-
}
8+
//import Foundation
9+
//import SwiftUI
10+
//
11+
//enum Github {}
12+
//
13+
//// MARK: - /repos/{reponame}
14+
//extension Github {
15+
// static func repos(_ name: String) -> Repos {
16+
// Repos(path: "/repos/\(name)")
17+
// }
18+
// struct Repos {
19+
// let path: String
20+
// var get: Req<RepoModel> {
21+
// .get(path)
22+
// }
23+
// }
24+
//}
25+
//// MARK: - /repos/{reponame}/issues/{issuenumber}
26+
//extension Github.Repos {
27+
// func issues(_ number: Int) -> Issues {
28+
// Issues(path: path + "/issues/\(number)")
29+
// }
30+
// struct Issues {
31+
// let path: String
32+
// var get: Req<IssueModel> {
33+
// .get(path)
34+
// }
35+
// }
36+
//}
37+
//// MARK: - /repos/{reponame}/commits
38+
//extension Github.Repos {
39+
// var commits: Commits {
40+
// Commits(path: path + "/commits", query: [("per_page", "100")])
41+
// }
42+
//
43+
// struct Commits {
44+
// let path: String
45+
// let query: [(String, String?)]?
46+
// var get: Req<[CommitModel]> {
47+
// .get(path, query: query)
48+
// }
49+
// }
50+
//}
51+
//
52+
//// MARK: - /user
53+
//extension Github {
54+
// static var user: User {
55+
// User()
56+
// }
57+
// struct User {
58+
// let path: String = "/user"
59+
// var get: Req<UserModel> {
60+
// .get(path)
61+
// }
62+
// }
63+
//}
64+
//// MARK: - /user/following
65+
//extension Github.User {
66+
// var following: Following {
67+
// Following()
68+
// }
69+
// struct Following {
70+
// let path: String = "/user/following"
71+
// var get: Req<[GitUserModel]> {
72+
// .get(path)
73+
// }
74+
// }
75+
//}
76+
//// MARK: - /users/{username}
77+
//extension Github {
78+
// static func users(_ name: String) -> Users {
79+
// Users(path: "/users/\(name)")
80+
// }
81+
// struct Users {
82+
// let path: String
83+
// var get: Req<UserModel> {
84+
// .get(path)
85+
// }
86+
// }
87+
//}
88+
//// MARK: - /users/{username}/followers
89+
//extension Github.Users {
90+
// var followers: Followers {
91+
// Followers(path: path + "/follower")
92+
// }
93+
// struct Followers {
94+
// let path: String
95+
// var get: Req<[GitUserModel]> {
96+
// .get(path)
97+
// }
98+
// }
99+
//}

0 commit comments

Comments
 (0)