Moya 如何自定义 超时、 HTTP Header 等

import Alamofire
import Moya

class WebService {
    
    // set false when release
    static var verbose: Bool = true
    
    // session manager
    static func manager() -> Alamofire.SessionManager {
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
        configuration.timeoutIntervalForRequest = 15 // timeout
        let manager = Alamofire.SessionManager(configuration: configuration)
        manager.adapter = CustomRequestAdapter()
        return manager
    }
    
    // request adpater to add default http header parameter
    private class CustomRequestAdapter: RequestAdapter {
        public func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
            var urlRequest = urlRequest
            urlRequest.setValue("XMLHttpRequest", forHTTPHeaderField: "X-Requested-With")          
            return urlRequest
        }
    }
    
    // response result type
    enum Result {
        case success(JSON)
        case failure(String)
    }
}

// use WebService.manager for provider
static let provider = MoyaProvider(manager: WebService.manager())

你可能感兴趣的:(Moya 如何自定义 超时、 HTTP Header 等)