mirror of
https://github.com/Moya/Moya.git
synced 2026-03-18 19:52:28 +00:00
52 lines
1.6 KiB
Swift
52 lines
1.6 KiB
Swift
import Foundation
|
|
import Moya
|
|
|
|
let gitHubUserContentProvider = MoyaProvider<GitHubUserContent>(plugins: [NetworkLoggerPlugin(configuration: .init(logOptions: .verbose))])
|
|
|
|
public enum GitHubUserContent {
|
|
case downloadMoyaWebContent(String)
|
|
}
|
|
|
|
extension GitHubUserContent: TargetType {
|
|
public var baseURL: URL { URL(string: "https://raw.githubusercontent.com")! } // swiftlint:disable:this force_unwrapping
|
|
public var path: String {
|
|
switch self {
|
|
case .downloadMoyaWebContent(let contentPath):
|
|
return "/Moya/Moya/master/web/\(contentPath)"
|
|
}
|
|
}
|
|
public var method: Moya.Method {
|
|
switch self {
|
|
case .downloadMoyaWebContent:
|
|
return .get
|
|
}
|
|
}
|
|
public var task: Task {
|
|
switch self {
|
|
case .downloadMoyaWebContent:
|
|
return .downloadDestination(defaultDownloadDestination)
|
|
}
|
|
}
|
|
public var sampleData: Data {
|
|
switch self {
|
|
case .downloadMoyaWebContent:
|
|
return Giphy.animatedBirdData
|
|
}
|
|
}
|
|
public var headers: [String: String]? { nil }
|
|
|
|
}
|
|
|
|
private let defaultDownloadDestination: DownloadDestination = { temporaryURL, response in
|
|
let directoryURLs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
|
|
|
|
if !directoryURLs.isEmpty {
|
|
guard let suggestedFilename = response.suggestedFilename else {
|
|
fatalError("@Moya/contributor error!! We didn't anticipate this being nil")
|
|
}
|
|
return (directoryURLs[0].appendingPathComponent(suggestedFilename), [])
|
|
}
|
|
|
|
return (temporaryURL, [])
|
|
}
|