Uber API 沙盒请求状态未正确更新
Uber API sandbox request status is not updating correctly
我正在使用 Swift 构建一个 iOS 应用程序,该应用程序使用 Uber API 乘车请求端点。使用 Sandbox 我无法正确更改乘车请求状态。我已经尝试使用 Uber 在他们的文档中的 curl 命令更新它,它似乎被 Uber 正确接收(我没有收到错误回复)。但是,在我的应用程序中,当我进行另一个状态调用时,它不会从 processing
改变。
目前,当我 POST 向 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
。请注意,两个请求中的乘车请求 ID 相同。我每 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"}'
还有其他人 运行 遇到 Uber 沙盒不可靠的问题吗?我在 2015 年 4 月使用 Ruby 开发了一个应用程序,沙箱运行良好。我认为这可能是我的 Swift 实现中的内容。
是我请求的缓存策略导致我看到上面描述的不一致响应。为了解决这个问题,我在请求中添加了 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()
}
我正在使用 Swift 构建一个 iOS 应用程序,该应用程序使用 Uber API 乘车请求端点。使用 Sandbox 我无法正确更改乘车请求状态。我已经尝试使用 Uber 在他们的文档中的 curl 命令更新它,它似乎被 Uber 正确接收(我没有收到错误回复)。但是,在我的应用程序中,当我进行另一个状态调用时,它不会从 processing
改变。
目前,当我 POST 向 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
。请注意,两个请求中的乘车请求 ID 相同。我每 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"}'
还有其他人 运行 遇到 Uber 沙盒不可靠的问题吗?我在 2015 年 4 月使用 Ruby 开发了一个应用程序,沙箱运行良好。我认为这可能是我的 Swift 实现中的内容。
是我请求的缓存策略导致我看到上面描述的不一致响应。为了解决这个问题,我在请求中添加了 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()
}