我正在使用Swift构建一个使用优步API乘坐请求端点的iOS应用。使用Sandbox我无法正确更改乘车请求状态。我已经尝试用Uber在他们的文档中使用curl命令来更新它,它似乎被Uber正确接收(我没有收到错误)。但是,在我的应用程序中,当我进行另一个状态调用时,它不会从processing
更改。
目前,当我将请求发布到https://sandbox-api.uber.com/v1/requests
时,我会收到以下回复,请注意状态为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";
};
}
现在已经成功提交了乘车请求,我使用上面的响应中的requestID来调用我的rideStatus函数。这是我要求的。
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()
}
所以这是奇怪的部分。我现在得到的回复是processing
。注意,两个请求中的ride requestId是相同的。我每5秒钟使用NSTimer调用此函数,每次调用时我不断回复的响应与下面相同。是NSURLNSURLSession
缓存了一些东西,所以我在下面得到旧回复。
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>";
}
另外,如果我使用下面的curl请求,我似乎无法一致地更改状态。我已经尝试将其更改为已接受,但它似乎无法正常工作。如果我将其更改为complete
,它会零星工作并complete
我的骑行。但是,我仍然在我的rideStatus函数中显示processing
。我知道它completed
的唯一方法是,如果我重新启动我的模拟器并发出新的POST请求,我将从Uber获得另一个requestId。这是我更新状态的卷曲:
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"}'
有没有其他人遇到优步沙盒不可靠的问题?我在2015年4月使用Ruby开发了一个应用程序,沙盒工作得很好。我认为这可能是我的Swift实现中的一些内容。
答案 0 :(得分:1)
我的请求的缓存策略导致我看到上面描述的不一致的响应。为了解决这个问题,我在我的请求中添加了ReloadIgnoringCacheData
的缓存策略。我现在可以将Curl PUT发送到Uber的沙箱,更新乘坐请求的状态,并以正确的状态更新我的重复功能更新。
以下完整请求:
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()
}