Skip to content

Commit

Permalink
clean old api fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
ming1016 committed Apr 9, 2024
1 parent 270c6da commit 2c32bf8
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 307 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ struct IssuesListFromCustomView: View {

}
}
.alert(vm.errMsg, isPresented: $vm.errHint, actions: {})
.onAppear {
vm.doing(.customIssues)
}
Expand Down
174 changes: 87 additions & 87 deletions SwiftPamphletApp/GitHubAPI/Network/APIRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,90 +5,90 @@
// Created by Ming Dai on 2021/11/8.
//

import Foundation
import Combine

// MARK: - API Request Fundation

protocol APIReqType {
associatedtype Res: Decodable
var path: String { get }
var qItems: [URLQueryItem]? { get }
}

protocol APIVMable: ObservableObject {
associatedtype ActionType
func doing(_ somethinglike: ActionType)
}

protocol APISevType {
func response<Request>(from req: Request) -> AnyPublisher<Request.Res, APISevError> where Request: APIReqType
}

final class APISev: APISevType {
private let rootUrl: URL

init(rootUrl: URL = URL(string: "https://api.github.com")!) {
self.rootUrl = rootUrl
}

func response<Request>(from req: Request) -> AnyPublisher<Request.Res, APISevError> where Request : APIReqType {
let path = URL(string: req.path, relativeTo: rootUrl)!
var comp = URLComponents(url: path, resolvingAgainstBaseURL: true)!
comp.queryItems = req.qItems
// print(comp.url?.description ?? "url wrong")
var req = URLRequest(url: comp.url!)

// token 处理
// TODO: 支持 OAuth
// TODO: 访问受限后会crash,异常待处理
var githubat = ""
if SPC.gitHubAccessToken.isEmpty == true {
githubat = SPC.githubAccessToken()
} else {
githubat = SPC.gitHubAccessToken
}

req.addValue("token \(githubat)", forHTTPHeaderField: "Authorization")

// print(req.allHTTPHeaderFields!)
let de = JSONDecoder()
de.keyDecodingStrategy = .convertFromSnakeCase
let sch = DispatchQueue(label: "GitHub API Queue", qos: .default, attributes: .concurrent)
return URLSession.shared.dataTaskPublisher(for: req)
.retry(3)
.subscribe(on: sch)
.receive(on: sch)
.map { data, _ in
// print(String(decoding: data, as: UTF8.self))
// print(res.description)
// 打印api访问额度
// let hres = res as! HTTPURLResponse
// print(hres.value(forHTTPHeaderField: "x-ratelimit-remaining") ?? "none")
return data
}
.mapError { _ in
APISevError.resError
}
.decode(type: Request.Res.self, decoder: de)
.mapError { _ in
APISevError.parseError
}
.receive(on: RunLoop.main)
.eraseToAnyPublisher()
}
}

enum APISevError: Error {
case resError
case parseError

var message: String {
switch self {
case .resError:
return "网络无法访问"
case .parseError:
return "网络出错"
}
}
}
//import Foundation
//import Combine
//
//// MARK: - API Request Fundation
//
//protocol APIReqType {
// associatedtype Res: Decodable
// var path: String { get }
// var qItems: [URLQueryItem]? { get }
//}
//
//protocol APIVMable: ObservableObject {
// associatedtype ActionType
// func doing(_ somethinglike: ActionType)
//}
//
//protocol APISevType {
// func response<Request>(from req: Request) -> AnyPublisher<Request.Res, APISevError> where Request: APIReqType
//}
//
//final class APISev: APISevType {
// private let rootUrl: URL
//
// init(rootUrl: URL = URL(string: "https://api.github.com")!) {
// self.rootUrl = rootUrl
// }
//
// func response<Request>(from req: Request) -> AnyPublisher<Request.Res, APISevError> where Request : APIReqType {
// let path = URL(string: req.path, relativeTo: rootUrl)!
// var comp = URLComponents(url: path, resolvingAgainstBaseURL: true)!
// comp.queryItems = req.qItems
//// print(comp.url?.description ?? "url wrong")
// var req = URLRequest(url: comp.url!)
//
// // token 处理
// // TODO: 支持 OAuth
// // TODO: 访问受限后会crash,异常待处理
// var githubat = ""
// if SPC.gitHubAccessToken.isEmpty == true {
// githubat = SPC.githubAccessToken()
// } else {
// githubat = SPC.gitHubAccessToken
// }
//
// req.addValue("token \(githubat)", forHTTPHeaderField: "Authorization")
//
//// print(req.allHTTPHeaderFields!)
// let de = JSONDecoder()
// de.keyDecodingStrategy = .convertFromSnakeCase
// let sch = DispatchQueue(label: "GitHub API Queue", qos: .default, attributes: .concurrent)
// return URLSession.shared.dataTaskPublisher(for: req)
// .retry(3)
// .subscribe(on: sch)
// .receive(on: sch)
// .map { data, _ in
//// print(String(decoding: data, as: UTF8.self))
//// print(res.description)
// // 打印api访问额度
//// let hres = res as! HTTPURLResponse
//// print(hres.value(forHTTPHeaderField: "x-ratelimit-remaining") ?? "none")
// return data
// }
// .mapError { _ in
// APISevError.resError
// }
// .decode(type: Request.Res.self, decoder: de)
// .mapError { _ in
// APISevError.parseError
// }
// .receive(on: RunLoop.main)
// .eraseToAnyPublisher()
// }
//}
//
//enum APISevError: Error {
// case resError
// case parseError
//
// var message: String {
// switch self {
// case .resError:
// return "网络无法访问"
// case .parseError:
// return "网络出错"
// }
// }
//}
184 changes: 92 additions & 92 deletions SwiftPamphletApp/GitHubAPI/Network/CCYGitHubAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,95 +5,95 @@
// Created by Ming Dai on 2021/12/16.
//

import Foundation
import SwiftUI

enum Github {}

// MARK: - /repos/{reponame}
extension Github {
static func repos(_ name: String) -> Repos {
Repos(path: "/repos/\(name)")
}
struct Repos {
let path: String
var get: Req<RepoModel> {
.get(path)
}
}
}
// MARK: - /repos/{reponame}/issues/{issuenumber}
extension Github.Repos {
func issues(_ number: Int) -> Issues {
Issues(path: path + "/issues/\(number)")
}
struct Issues {
let path: String
var get: Req<IssueModel> {
.get(path)
}
}
}
// MARK: - /repos/{reponame}/commits
extension Github.Repos {
var commits: Commits {
Commits(path: path + "/commits", query: [("per_page", "100")])
}

struct Commits {
let path: String
let query: [(String, String?)]?
var get: Req<[CommitModel]> {
.get(path, query: query)
}
}
}

// MARK: - /user
extension Github {
static var user: User {
User()
}
struct User {
let path: String = "/user"
var get: Req<UserModel> {
.get(path)
}
}
}
// MARK: - /user/following
extension Github.User {
var following: Following {
Following()
}
struct Following {
let path: String = "/user/following"
var get: Req<[GitUserModel]> {
.get(path)
}
}
}
// MARK: - /users/{username}
extension Github {
static func users(_ name: String) -> Users {
Users(path: "/users/\(name)")
}
struct Users {
let path: String
var get: Req<UserModel> {
.get(path)
}
}
}
// MARK: - /users/{username}/followers
extension Github.Users {
var followers: Followers {
Followers(path: path + "/follower")
}
struct Followers {
let path: String
var get: Req<[GitUserModel]> {
.get(path)
}
}
}
//import Foundation
//import SwiftUI
//
//enum Github {}
//
//// MARK: - /repos/{reponame}
//extension Github {
// static func repos(_ name: String) -> Repos {
// Repos(path: "/repos/\(name)")
// }
// struct Repos {
// let path: String
// var get: Req<RepoModel> {
// .get(path)
// }
// }
//}
//// MARK: - /repos/{reponame}/issues/{issuenumber}
//extension Github.Repos {
// func issues(_ number: Int) -> Issues {
// Issues(path: path + "/issues/\(number)")
// }
// struct Issues {
// let path: String
// var get: Req<IssueModel> {
// .get(path)
// }
// }
//}
//// MARK: - /repos/{reponame}/commits
//extension Github.Repos {
// var commits: Commits {
// Commits(path: path + "/commits", query: [("per_page", "100")])
// }
//
// struct Commits {
// let path: String
// let query: [(String, String?)]?
// var get: Req<[CommitModel]> {
// .get(path, query: query)
// }
// }
//}
//
//// MARK: - /user
//extension Github {
// static var user: User {
// User()
// }
// struct User {
// let path: String = "/user"
// var get: Req<UserModel> {
// .get(path)
// }
// }
//}
//// MARK: - /user/following
//extension Github.User {
// var following: Following {
// Following()
// }
// struct Following {
// let path: String = "/user/following"
// var get: Req<[GitUserModel]> {
// .get(path)
// }
// }
//}
//// MARK: - /users/{username}
//extension Github {
// static func users(_ name: String) -> Users {
// Users(path: "/users/\(name)")
// }
// struct Users {
// let path: String
// var get: Req<UserModel> {
// .get(path)
// }
// }
//}
//// MARK: - /users/{username}/followers
//extension Github.Users {
// var followers: Followers {
// Followers(path: path + "/follower")
// }
// struct Followers {
// let path: String
// var get: Req<[GitUserModel]> {
// .get(path)
// }
// }
//}
Loading

0 comments on commit 2c32bf8

Please sign in to comment.