tylerSF
tylerSF

Reputation: 1539

Uber API sandbox request status is not updating correctly

I'm building an iOS app with Swift that uses the Uber API ride request endpoint. Using Sandbox I'm having trouble changing the ride request status properly. I've tried updating it with the curl command Uber has in their docs and it appears to be received by Uber correctly (I don't get an error back). However, in my app when I make another status call it doesn't change from processing.

Currently, when I POST the request to https://sandbox-api.uber.com/v1/requests I get back the response below, note that the status is accepted.

Prepare to make request -> <p2_OAuth2.OAuth2Request: 0x7fb89a854e50> { URL: https://sandbox-api.uber.com/v1/requests }
Result -> {
  destination =     {
    latitude = "37.393913269";
    longitude = "-122.0800018311";
  };
  driver =     {
    name = John;
    "phone_number" = "(555)555-5555";
    "picture_url" = "https://d1a3f4spazzrp4.cloudfront.net/uberex-sandbox/images/driver.jpg";
    rating = "4.9";
  };
  eta = 1;
  location =     {
    bearing = "-150";
    latitude = "37.33233141";
    longitude = "-122.0312186";
  };
  pickup =     {
    eta = 1;
    latitude = "37.33233141";
    longitude = "-122.0312186";
  };
  "request_id" = "39d76708-3a26-4fb7-bb0f-31f37c931a0b";
  status = accepted;
  "surge_multiplier" = 1;
  vehicle =     {
    "license_plate" = "UBER-PLATE";
    make = Toyota;
    model = Prius;
    "picture_url" = "https://d1a3f4spazzrp4.cloudfront.net/uberex-sandbox/images/prius.jpg";
  };
}

Now that the ride request has been successfully submitted I call my rideStatus function using the requestID from the response above. This is the request I'm making.

class func rideStatus(uberRequestId:String, completion: (status: String) -> Void) {

    var status:String = ""

    let urlPath = "https://sandbox-api.uber.com/v1/requests/\(uberRequestId)"
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    guard let endpoint = NSURL(string: urlPath) else { print("Error creating endpoint");return }
    let request = appDelegate.oauth.request(forURL: NSURL(string:urlPath)!)
    request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
    request.HTTPMethod = "GET"


    NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
        do {
            guard let dat = data else { throw JSONError.NoData }

            let result = try NSJSONSerialization.JSONObjectWithData(dat, options: NSJSONReadingOptions.MutableContainers)

            print(result)

            //set status
            status = result["status"] as! String

            print("found status...returning it back -> \(status)")
            completion(status: "\(status)")

        } catch let error as JSONError {
            print(error.rawValue)
            print("ERROR NEEDS TO BE HANDLED. NO RETURN FROM UBER API")
        } catch {
            print(error)
            print("ERROR NEEDS TO BE HANDLED. NO RETURN FROM UBER API")
        }
    }.resume()

}

So here is the weird part. The response I'm getting is now processing. Note, that the ride requestId is the same in both requests. I call this function using NSTimer every 5 seconds and the response I keep getting back is the same as below with every call. Is NSURLNSURLSession caching something and so I'm getting the old response below.

requestID is -> 39d76708-3a26-4fb7-bb0f-31f37c931a0b
{
  driver = "<null>";
  eta = "<null>";
  location = "<null>";
  "request_id" = "39d76708-3a26-4fb7-bb0f-31f37c931a0b";
  status = processing;
  "surge_multiplier" = 1;
  vehicle = "<null>";
}

Also, if I use the curl request below, I can't seem to get it to change status consistently. I've tried changing it to accepted and it doesn't seem to work. If I change it to complete, it will sporadically work and complete my ride. However, I still show processing in my rideStatus function. The only way I know it's completed is if I restart my simulator and make a new POST ride request and I'll get back a different requestId from Uber. This is my curl to update the status:

curl -X "PUT" "https://sandbox-api.uber.com/v1/sandbox/requests/39d76708-3a26-4fb7-bb0f-31f37c931a0b" \
  -H "Authorization: Bearer RYV7mXXXXXXXXXXXXXXXXXXXXXyzLw" \
  -H "Content-Type: application/json"\
  -d '{"status":"accepted"}'

Has anyone else ran into issues with Uber's Sandbox not being reliable? I developed an app in April 2015 using Ruby and the sandbox worked pretty well. I think this could be something in my Swift implementation.

Upvotes: 1

Views: 424

Answers (1)

tylerSF
tylerSF

Reputation: 1539

It was the cache policy of my request causing me to see the inconsistent responses I described above. To fix I added a cache policy of ReloadIgnoringCacheData to my request. I can now send Curl PUTs to Uber's sandbox updating the status of the ride request and my repeating function updates with the correct status.

Full request below:

class func rideStatus(uberRequestId:String, completion: (status: String) -> Void) {

  var status:String = ""

  let urlPath = "https://sandbox-api.uber.com/v1/requests/\(uberRequestId)"
  let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

  guard let endpoint = NSURL(string: urlPath) else { print("Error creating endpoint");return }
  let request = appDelegate.oauth.request(forURL: NSURL(string:urlPath)!)
  request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
  request.HTTPMethod = "GET"

  // Added this line to set request policy
  request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData

  NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
    do {
        guard let dat = data else { throw JSONError.NoData }

        let result = try NSJSONSerialization.JSONObjectWithData(dat, options: NSJSONReadingOptions.MutableContainers)

        print(result)

        //set status
        status = result["status"] as! String

        print("found status...returning it back -> \(status)")
        completion(status: "\(status)")

    } catch let error as JSONError {
        print(error.rawValue)
        print("ERROR NEEDS TO BE HANDLED. NO RETURN FROM UBER API")
    } catch {
        print(error)
        print("ERROR NEEDS TO BE HANDLED. NO RETURN FROM UBER API")
    }
  }.resume()

}

Upvotes: 1

Related Questions