Skip to content

Commit 1eb2ef7

Browse files
committed
非 token 方式来请求接口
1 parent 256fd60 commit 1eb2ef7

File tree

3 files changed

+56
-28
lines changed

3 files changed

+56
-28
lines changed

SwiftPamphletApp/GitHubAPI/APIVM/APIVM.swift

+40-15
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,59 @@ import SwiftUI
1010

1111
@Observable
1212
final class APIRepoVM {
13+
var name: String = ""
1314
var repo: RepoModel = RepoModel()
15+
var commits: [CommitModel] = [CommitModel]()
1416

17+
init(name: String) {
18+
self.name = name
19+
}
20+
21+
func updateAllData() async {
22+
await obtainRepos()
23+
await obtainCommits()
24+
}
25+
26+
// https://docs.github.com/zh/rest/repos/repos?apiVersion=2022-11-28
1527
@MainActor
16-
func repos(_ name: String) async {
17-
28+
func obtainRepos() async {
1829
do {
1930
let (data, _) = try await URLSession.shared.data(for: GitHubReq.req("repos/\(name)"))
20-
let de = JSONDecoder()
21-
de.keyDecodingStrategy = .convertFromSnakeCase
22-
repo = try de.decode(RepoModel.self, from: data)
31+
repo = try GitHubReq.jsonDecoder().decode(RepoModel.self, from: data)
2332
} catch {
2433
print("问题是:\(error)")
2534
}
2635
}
36+
37+
// https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28
38+
@MainActor
39+
func obtainCommits() async {
40+
do {
41+
let (data, _) = try await URLSession.shared.data(for: GitHubReq.req("repos/\(name)/commits"))
42+
commits = try GitHubReq.jsonDecoder().decode([CommitModel].self, from: data)
43+
} catch {
44+
print("问题是:\(error)")
45+
}
46+
}
47+
2748
}
2849

2950
class GitHubReq {
30-
51+
static func jsonDecoder() -> JSONDecoder {
52+
let de = JSONDecoder()
53+
de.keyDecodingStrategy = .convertFromSnakeCase
54+
return de
55+
}
3156
static func req(_ path: String) -> URLRequest {
32-
var req = URLRequest(url: URL(string: "https://api.github.com/\(path)")!)
33-
var githubat = ""
34-
if SPC.gitHubAccessToken.isEmpty == true {
35-
githubat = SPC.githubAccessToken()
36-
} else {
37-
githubat = SPC.gitHubAccessToken
38-
}
39-
40-
req.addValue("token \(githubat)", forHTTPHeaderField: "Authorization")
57+
let req = URLRequest(url: URL(string: "https://api.github.com/\(path)")!)
58+
// var githubat = ""
59+
// if SPC.gitHubAccessToken.isEmpty == true {
60+
// githubat = SPC.githubAccessToken()
61+
// } else {
62+
// githubat = SPC.gitHubAccessToken
63+
// }
64+
//
65+
// req.addValue("token \(githubat)", forHTTPHeaderField: "Authorization")
4166
return req
4267
}
4368
}

SwiftPamphletApp/GitHubAPI/Developer/EditDeveloper.swift

+11-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct EditDeveloper: View {
1616
@State var vmRepo: RepoVM
1717
@State private var tabSelctRepo = 1
1818

19-
@State var repoVM: APIRepoVM = APIRepoVM()
19+
@State var repoVM: APIRepoVM
2020

2121
var body: some View {
2222
Form {
@@ -25,8 +25,9 @@ struct EditDeveloper: View {
2525
.onChange(of: dev.name) { oldValue, newValue in
2626
let dn = dev.name.components(separatedBy: "/")
2727
if dn.count > 1 {
28+
repoVM = APIRepoVM(name: dev.name)
2829
Task {
29-
await repoVM.repos(dev.name)
30+
await repoVM.updateAllData()
3031
}
3132

3233
dev.repoName = dn.last ?? ""
@@ -86,7 +87,7 @@ struct EditDeveloper: View {
8687
dev.avatar = newValue.owner.avatarUrl
8788
}
8889
})
89-
.onChange(of: vmRepo.commits, { oldValue, newValue in
90+
.onChange(of: repoVM.commits, { oldValue, newValue in
9091
if ((newValue.first?.commit.author.date.isEmpty) != nil) {
9192
let iso8601String = newValue.first?.commit.author.date ?? ""
9293
let formatter = ISO8601DateFormatter()
@@ -96,19 +97,21 @@ struct EditDeveloper: View {
9697
.padding(EdgeInsets(top: 20, leading: 10, bottom: 0, trailing: 10))
9798
.onAppear {
9899
Task {
99-
await repoVM.repos(dev.name)
100+
await repoVM.updateAllData()
100101
}
101102
}
102103
// end HStack
103104

104105
TabView(selection: $tabSelct) {
105-
RepoCommitsView(commits: vmRepo.commits, repo: vmRepo.repo)
106+
RepoCommitsView(commits: repoVM.commits, repo: repoVM.repo)
106107
.tabItem {
107108
Text("新提交")
108109
}
109-
.onAppear {
110-
vmRepo.doing(.inCommit)
111-
}
110+
// .onAppear {
111+
// Task {
112+
// await repoVM.obtainCommits()
113+
// }
114+
// }
112115
.tag(1)
113116

114117
IssuesView(issues: vmRepo.issues, repo: vmRepo.repo)

SwiftPamphletApp/HomeUI/DataLink.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct DataLink: Identifiable {
4545
case .detail:
4646
if let dev = selectDevBindable {
4747
if SPC.gitHubAccessToken.isEmpty == false || SPC.githubAccessToken().isEmpty == false {
48-
EditDeveloper(dev: dev, vm: UserVM(userName: dev.name), vmRepo: RepoVM(repoName: dev.name))
48+
EditDeveloper(dev: dev, vm: UserVM(userName: dev.name), vmRepo: RepoVM(repoName: dev.name), repoVM: APIRepoVM(name: dev.name))
4949
} else {
5050
Text("请在设置里写上 Github 的 access token")
5151
}
@@ -77,9 +77,6 @@ extension DataLink {
7777
DataLink(title: "资料", imageName: "", children: [
7878
DataLink(title: "资料整理", imageName: "p11")
7979
]),
80-
DataLink(title: "Github", imageName: "", children: [
81-
DataLink(title: "开发/仓库", imageName: "p5"),
82-
]),
8380
DataLink(title: "Swift指南", imageName: "", children: [
8481
DataLink(title: "语法速查", imageName: "p23"),
8582
DataLink(title: "特性", imageName: "p10"),
@@ -89,6 +86,9 @@ extension DataLink {
8986
DataLink(title: "SwiftUI", imageName: "p3"),
9087
DataLink(title: "Combine", imageName: "p19"),
9188
DataLink(title: "Concurrency", imageName: "p1")
92-
])
89+
]),
90+
DataLink(title: "Github", imageName: "", children: [
91+
DataLink(title: "开发/仓库", imageName: "p5"),
92+
]),
9393
]
9494
}

0 commit comments

Comments
 (0)