first commit for e-party

This commit is contained in:
edwinQQQ
2025-07-07 14:19:07 +08:00
parent 007c10daaf
commit 5926906f3c
14 changed files with 1248 additions and 201 deletions

View File

@@ -2,15 +2,49 @@ import Foundation
import ComposableArchitecture
// MARK: - API Service Protocol
/// API
///
/// `APIRequestProtocol`
///
///
/// 使
/// ```swift
/// let apiService: APIServiceProtocol = LiveAPIService()
/// let request = ConfigRequest()
/// let response = try await apiService.request(request)
/// ```
protocol APIServiceProtocol {
///
/// - Parameter request: APIRequestProtocol
/// - Returns:
/// - Throws: APIError
func request<T: APIRequestProtocol>(_ request: T) async throws -> T.Response
}
// MARK: - Live API Service Implementation
/// API
///
///
/// - URL
/// -
/// -
/// -
/// -
///
///
/// - GET/POST/PUT/DELETE HTTP
/// -
/// -
/// - /
/// -
struct LiveAPIService: APIServiceProtocol {
private let session: URLSession
private let baseURL: String
/// API
/// - Parameter baseURL: API URL使
init(baseURL: String = APIConfiguration.baseURL) {
self.baseURL = baseURL
@@ -27,6 +61,19 @@ struct LiveAPIService: APIServiceProtocol {
self.session = URLSession(configuration: config)
}
///
///
///
/// 1. URL
/// 2.
/// 3.
/// 4.
/// 5.
/// 6.
///
/// - Parameter request: APIRequestProtocol
/// - Returns:
/// - Throws: APIError
func request<T: APIRequestProtocol>(_ request: T) async throws -> T.Response {
let startTime = Date()
@@ -57,7 +104,9 @@ struct LiveAPIService: APIServiceProtocol {
//
var finalBody = bodyParams
if request.includeBaseParameters {
let baseParams = BaseRequest()
var baseParams = BaseRequest()
// API rule
baseParams.generateSignature(with: bodyParams)
let baseDict = try baseParams.toDictionary()
finalBody.merge(baseDict) { existing, _ in existing }
}
@@ -129,6 +178,15 @@ struct LiveAPIService: APIServiceProtocol {
// MARK: - Private Helper Methods
/// URL
///
///
/// - URL
/// -
/// - GET
///
/// - Parameter request: API
/// - Returns: URL nil
private func buildURL<T: APIRequestProtocol>(for request: T) -> URL? {
guard var urlComponents = URLComponents(string: baseURL + request.endpoint) else {
return nil
@@ -140,7 +198,10 @@ struct LiveAPIService: APIServiceProtocol {
// GET
if request.method == .GET && request.includeBaseParameters {
do {
let baseParams = BaseRequest()
var baseParams = BaseRequest()
// GET
let queryParamsDict = request.queryParameters ?? [:]
baseParams.generateSignature(with: queryParamsDict)
let baseDict = try baseParams.toDictionary()
for (key, value) in baseDict {
queryItems.append(URLQueryItem(name: key, value: "\(value)"))
@@ -164,6 +225,12 @@ struct LiveAPIService: APIServiceProtocol {
return urlComponents.url
}
///
///
/// JSON
///
/// - Parameter data:
/// - Returns: nil
private func extractErrorMessage(from data: Data) -> String? {
guard let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
return nil
@@ -181,6 +248,13 @@ struct LiveAPIService: APIServiceProtocol {
return nil
}
/// API
///
/// URLError APIError
/// 便
///
/// - Parameter error:
/// - Returns: APIError
private func mapSystemError(_ error: Error) -> APIError {
if let urlError = error as? URLError {
switch urlError.code {
@@ -200,6 +274,20 @@ struct LiveAPIService: APIServiceProtocol {
}
// MARK: - Mock API Service (for testing)
/// API
///
/// API
/// -
/// -
/// - UI
///
/// 使
/// ```swift
/// var mockService = MockAPIService()
/// mockService.setMockResponse(for: "/client/config", response: mockConfigResponse)
/// let response = try await mockService.request(ConfigRequest())
/// ```
struct MockAPIService: APIServiceProtocol {
private var mockResponses: [String: Any] = [:]