145 lines
5.6 KiB
Swift
145 lines
5.6 KiB
Swift
//
|
||
// Api.swift
|
||
// dimensionhub
|
||
//
|
||
// Created by 安礼成 on 2025/2/19.
|
||
//
|
||
import Foundation
|
||
|
||
struct API {
|
||
|
||
// 失败时候的返回
|
||
struct APIErrorResponse: Codable {
|
||
struct APIError: Codable {
|
||
let code: Int32
|
||
let message: String
|
||
}
|
||
let error: APIError
|
||
}
|
||
|
||
// 成功时候的结构体
|
||
struct APISuccessResponse<T: Codable>: Codable {
|
||
let result: T
|
||
}
|
||
|
||
enum APIResponse<T: Codable> {
|
||
case result(T)
|
||
case error(Int32, String)
|
||
}
|
||
|
||
// 加载模式
|
||
enum LoadMode: String {
|
||
case prev = "prev"
|
||
case next = "next"
|
||
}
|
||
|
||
// 服务器地址
|
||
static let baseUrl = "https://dimensionhub.s5s8.com"
|
||
|
||
// 发送设备的token到服务
|
||
static func sendDeviceTokenToServer<T: Codable>(userId: String, token: String, as: T.Type) async -> APIResponse<T> {
|
||
// Create the request
|
||
var request = URLRequest(url: URL(string: baseUrl + "/api/device_token")!)
|
||
request.httpMethod = "POST"
|
||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||
|
||
// Convert parameters to JSON data
|
||
let parameters: [String:String] = [
|
||
"user_id": userId,
|
||
"token": token
|
||
]
|
||
let jsonData = try! JSONSerialization.data(withJSONObject: parameters, options: [])
|
||
request.httpBody = jsonData
|
||
|
||
return await doRequest(request: request, as: T.self)
|
||
}
|
||
|
||
// 获取首页的数据
|
||
static func getIndexData<T: Codable>(userId: String, as: T.Type) async -> APIResponse<T> {
|
||
let request = URLRequest(url: URL(string: baseUrl + "/api/index?user_id=\(userId)")!)
|
||
|
||
return await doRequest(request: request, as: T.self)
|
||
}
|
||
|
||
// 获取用户的关注数
|
||
static func getFlowNum<T: Codable>(userId: String, as: T.Type) async -> APIResponse<T> {
|
||
let request = URLRequest(url: URL(string: baseUrl + "/api/follow_num?user_id=\(userId)")!)
|
||
|
||
return await doRequest(request: request, as: T.self)
|
||
}
|
||
|
||
// 前后刷新获取数据
|
||
static func loadMoreUpdateDramas<T: Codable>(userId: String, mode: LoadMode, id: Int, as: T.Type) async -> APIResponse<T> {
|
||
let request = URLRequest(url: URL(string: baseUrl + "/api/load_more_dramas?user_id=\(userId)&mode=\(mode.rawValue)&id=\(id)")!)
|
||
|
||
return await doRequest(request: request, as: T.self)
|
||
}
|
||
|
||
// 获取用户收藏的数据列表
|
||
static func getUserFollowList<T: Codable>(userId: String, as: T.Type) async -> APIResponse<T> {
|
||
let request = URLRequest(url: URL(string: baseUrl + "/api/follow_list?user_id=\(userId)")!)
|
||
|
||
return await doRequest(request: request, as: T.self)
|
||
}
|
||
|
||
// 指定时间索引
|
||
static func loadDateUpdateDramas<T: Codable>(userId: String, date: String, as: T.Type) async -> APIResponse<T> {
|
||
let request = URLRequest(url: URL(string: baseUrl + "/api/load_date_dramas?user_id=\(userId)&date=\(date)")!)
|
||
|
||
return await doRequest(request: request, as: T.self)
|
||
}
|
||
|
||
static func getDateIndex<T: Codable>(userId: String, as: T.Type) async -> APIResponse<T> {
|
||
let request = URLRequest(url: URL(string: baseUrl + "/api/date_index?user_id=\(userId)")!)
|
||
|
||
return await doRequest(request: request, as: T.self)
|
||
}
|
||
|
||
static func getDramaDetail<T: Codable>(userId: String, id: Int, as: T.Type) async -> APIResponse<T> {
|
||
let request = URLRequest(url: URL(string: baseUrl + "/api/detail?user_id=\(userId)&id=\(id)")!)
|
||
|
||
return await doRequest(request: request, as: T.self)
|
||
}
|
||
|
||
static func followDrama<T: Codable>(userId: String, id: Int, status: String, as: T.Type) async -> APIResponse<T> {
|
||
let request = URLRequest(url: URL(string: baseUrl + "/api/follow?user_id=\(userId)&id=\(id)&status=\(status)")!)
|
||
|
||
return await doRequest(request: request, as: T.self)
|
||
}
|
||
|
||
// 传入的时候,name参数是经过url_encode逻辑处理过的
|
||
static func searchDrama<T: Codable>(userId: String, name: String, as: T.Type) async -> APIResponse<T> {
|
||
let request = URLRequest(url: URL(string: baseUrl + "/api/search?user_id=\(userId)&name=\(name)")!)
|
||
|
||
return await doRequest(request: request, as: T.self)
|
||
}
|
||
|
||
// 执行http请求
|
||
private static func doRequest<T: Codable>(request: URLRequest, as: T.Type) async -> APIResponse<T> {
|
||
do {
|
||
let (data, response) = try await URLSession.shared.data(for: request)
|
||
guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
|
||
print("url: \(request.url!.absoluteString)")
|
||
// print(String(data: data, encoding: .utf8)!)
|
||
return .error(-1, "http status error")
|
||
}
|
||
|
||
print("request url: \(request.url!.absoluteString)")
|
||
// let x = String(data: data, encoding: .utf8)!
|
||
// print("url: \(request.url!.path()), data is: \(x)")
|
||
do {
|
||
let result = try JSONDecoder().decode(APISuccessResponse<T>.self, from: data)
|
||
return .result(result.result)
|
||
} catch let err {
|
||
print("http request: \(request.url!.path()), get error: \(err)")
|
||
let apiError = try JSONDecoder().decode(APIErrorResponse.self, from: data)
|
||
return .error(apiError.error.code, apiError.error.message)
|
||
}
|
||
} catch let err {
|
||
print("http error: \(err)")
|
||
return .error(-1, err.localizedDescription)
|
||
}
|
||
}
|
||
|
||
}
|