RxAlamofire源代码中实际的API调用在哪里?

时间:2019-10-08 21:44:30

标签: swift rxalamofire

我正在尝试找出API调用中的数据来自何处。具体来说,我想知道req.user的来源,因为我可以知道API调用中没有传递参数。

这是服务器端代码(在JS中):

let APIHandler = (req, res) = > {
    if (req.user) {
       latitude = req.user.location.latitude
       longitude = req.user.location.longitude
    }
}

这是客户端代码(在Swift中):

 let strURLTo = SERVICE.BASE_URL + apiName + limit
 let headers = AuthorizationHeader.getAuthHeader(staticToken: false)

 var urlComponents = URLComponents(string: strURLTo)
 urlComponents?.queryItems = [URLQueryItem(name: "offset", value: String(offset)),
                             URLQueryItem(name: "limit", value: String(limit))]

 let strURL  = urlComponents?.url

 RxAlamofire.requestJSON(.get,strURL!,parameters:[:],headers:headers) 

            .debug()

            .subscribe(onNext: {(HeaderResponse, bodyResponse) in

                if let dict = bodyResponse as? [String: AnyObject] {
                    if let respDict: [String: Any] = JSON(dict).dictionaryObject {
                        let response = ResponseModel.init(statusCode: HeaderResponse.statusCode, response:respDict)
                        self.subject_response.onNext(response)
                    }
                }

            }, onError: { (error) in
                self.subject_response.onError(error)
            })

            .disposed(by: disposeBag)

最后是RxAlamofire.requestJson的定义:

public func requestJSON(_ method: Alamofire.HTTPMethod,
    _ url: URLConvertible,
    parameters: [String: Any]? = nil,
    encoding: ParameterEncoding = URLEncoding.default,
    headers: [String: String]? = nil)
    -> Observable<(HTTPURLResponse, Any)>
{
    return SessionManager.default.rx.responseJSON(
        method,
        url,
        parameters: parameters,
        encoding: encoding,
        headers: headers
    )
}

我尝试逐步跟踪RxAlamofire.requestJSON函数,但看不到实际API调用发生的任何地方(对我来说,它似乎只是概述了期望/返回的类型) 。此外,RxAlamofire.requestJSON(.get,strURL!,parameters:[:],headers:headers)中没有参数 打电话。

1 个答案:

答案 0 :(得分:0)

实际的呼叫是在这一行中进行的(为了清楚起见,它已分散在多行中):

return SessionManager.default.rx.responseJSON(
    method,
    url,
    parameters: parameters,
    encoding: encoding,
    headers: headers
    )

正在此处设置参数

var urlComponents = URLComponents(string: strURLTo)
urlComponents?.queryItems = [URLQueryItem(name: "offset", value: String(offset)),
                             URLQueryItem(name: "limit", value: String(limit))]

但是,我也没有看到设置user,所以也许答案顶部的API函数中正在发生这种情况?