Skip to content

Commit

Permalink
非 token 方式来请求接口
Browse files Browse the repository at this point in the history
  • Loading branch information
ming1016 committed Apr 7, 2024
1 parent 256fd60 commit 1eb2ef7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 28 deletions.
55 changes: 40 additions & 15 deletions SwiftPamphletApp/GitHubAPI/APIVM/APIVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,59 @@ import SwiftUI

@Observable
final class APIRepoVM {
var name: String = ""
var repo: RepoModel = RepoModel()
var commits: [CommitModel] = [CommitModel]()

init(name: String) {
self.name = name
}

func updateAllData() async {
await obtainRepos()
await obtainCommits()
}

// https://docs.github.com/zh/rest/repos/repos?apiVersion=2022-11-28
@MainActor
func repos(_ name: String) async {

func obtainRepos() async {
do {
let (data, _) = try await URLSession.shared.data(for: GitHubReq.req("repos/\(name)"))
let de = JSONDecoder()
de.keyDecodingStrategy = .convertFromSnakeCase
repo = try de.decode(RepoModel.self, from: data)
repo = try GitHubReq.jsonDecoder().decode(RepoModel.self, from: data)
} catch {
print("问题是:\(error)")
}
}

// https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28
@MainActor
func obtainCommits() async {
do {
let (data, _) = try await URLSession.shared.data(for: GitHubReq.req("repos/\(name)/commits"))
commits = try GitHubReq.jsonDecoder().decode([CommitModel].self, from: data)
} catch {
print("问题是:\(error)")
}
}

}

class GitHubReq {

static func jsonDecoder() -> JSONDecoder {
let de = JSONDecoder()
de.keyDecodingStrategy = .convertFromSnakeCase
return de
}
static func req(_ path: String) -> URLRequest {
var req = URLRequest(url: URL(string: "https://api.github.com/\(path)")!)
var githubat = ""
if SPC.gitHubAccessToken.isEmpty == true {
githubat = SPC.githubAccessToken()
} else {
githubat = SPC.gitHubAccessToken
}

req.addValue("token \(githubat)", forHTTPHeaderField: "Authorization")
let req = URLRequest(url: URL(string: "https://api.github.com/\(path)")!)
// var githubat = ""
// if SPC.gitHubAccessToken.isEmpty == true {
// githubat = SPC.githubAccessToken()
// } else {
// githubat = SPC.gitHubAccessToken
// }
//
// req.addValue("token \(githubat)", forHTTPHeaderField: "Authorization")
return req
}
}
Expand Down
19 changes: 11 additions & 8 deletions SwiftPamphletApp/GitHubAPI/Developer/EditDeveloper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct EditDeveloper: View {
@State var vmRepo: RepoVM
@State private var tabSelctRepo = 1

@State var repoVM: APIRepoVM = APIRepoVM()
@State var repoVM: APIRepoVM

var body: some View {
Form {
Expand All @@ -25,8 +25,9 @@ struct EditDeveloper: View {
.onChange(of: dev.name) { oldValue, newValue in
let dn = dev.name.components(separatedBy: "/")
if dn.count > 1 {
repoVM = APIRepoVM(name: dev.name)
Task {
await repoVM.repos(dev.name)
await repoVM.updateAllData()
}

dev.repoName = dn.last ?? ""
Expand Down Expand Up @@ -86,7 +87,7 @@ struct EditDeveloper: View {
dev.avatar = newValue.owner.avatarUrl
}
})
.onChange(of: vmRepo.commits, { oldValue, newValue in
.onChange(of: repoVM.commits, { oldValue, newValue in
if ((newValue.first?.commit.author.date.isEmpty) != nil) {
let iso8601String = newValue.first?.commit.author.date ?? ""
let formatter = ISO8601DateFormatter()
Expand All @@ -96,19 +97,21 @@ struct EditDeveloper: View {
.padding(EdgeInsets(top: 20, leading: 10, bottom: 0, trailing: 10))
.onAppear {
Task {
await repoVM.repos(dev.name)
await repoVM.updateAllData()
}
}
// end HStack

TabView(selection: $tabSelct) {
RepoCommitsView(commits: vmRepo.commits, repo: vmRepo.repo)
RepoCommitsView(commits: repoVM.commits, repo: repoVM.repo)
.tabItem {
Text("新提交")
}
.onAppear {
vmRepo.doing(.inCommit)
}
// .onAppear {
// Task {
// await repoVM.obtainCommits()
// }
// }
.tag(1)

IssuesView(issues: vmRepo.issues, repo: vmRepo.repo)
Expand Down
10 changes: 5 additions & 5 deletions SwiftPamphletApp/HomeUI/DataLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct DataLink: Identifiable {
case .detail:
if let dev = selectDevBindable {
if SPC.gitHubAccessToken.isEmpty == false || SPC.githubAccessToken().isEmpty == false {
EditDeveloper(dev: dev, vm: UserVM(userName: dev.name), vmRepo: RepoVM(repoName: dev.name))
EditDeveloper(dev: dev, vm: UserVM(userName: dev.name), vmRepo: RepoVM(repoName: dev.name), repoVM: APIRepoVM(name: dev.name))
} else {
Text("请在设置里写上 Github 的 access token")
}
Expand Down Expand Up @@ -77,9 +77,6 @@ extension DataLink {
DataLink(title: "资料", imageName: "", children: [
DataLink(title: "资料整理", imageName: "p11")
]),
DataLink(title: "Github", imageName: "", children: [
DataLink(title: "开发/仓库", imageName: "p5"),
]),
DataLink(title: "Swift指南", imageName: "", children: [
DataLink(title: "语法速查", imageName: "p23"),
DataLink(title: "特性", imageName: "p10"),
Expand All @@ -89,6 +86,9 @@ extension DataLink {
DataLink(title: "SwiftUI", imageName: "p3"),
DataLink(title: "Combine", imageName: "p19"),
DataLink(title: "Concurrency", imageName: "p1")
])
]),
DataLink(title: "Github", imageName: "", children: [
DataLink(title: "开发/仓库", imageName: "p5"),
]),
]
}

0 comments on commit 1eb2ef7

Please sign in to comment.