Where is the actual API call within this RxAlamofire source code?

320 views Asked by At

I'm trying to figure out where data in an API call is coming from. Specifically, I want to know where req.user is coming from as from what I can tell there's no paramters being passed into the API call.

Here's the server-side code (in JS):

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

And here's the client-side code (in 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)

And finally here's the RxAlamofire.requestJson definition:

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
    )
}

What I tried was tracing through the RxAlamofire.requestJSON function step-by-step but I don't see anywhere where the actual API call happens (to me it seems like it's just outlining the types it's expecting/returning). Additionally there's no parameters in the RxAlamofire.requestJSON(.get,strURL!,parameters:[:],headers:headers) call.

1

There are 1 answers

0
Sam On

The actual call is being made in this line (which has been spread out over multiple lines for clarity):

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

The parameters are being set here:

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

However, I also do not see user being set, so maybe it is happening in the API function at the top of this answer?