-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing status code to sendRequest handler. #39
Passing status code to sendRequest handler. #39
Conversation
Actually, passing status code is important in some practical case, but I'm hesitating to add an argument to completion handler because there are many case that status code is not necessary. Personally, I think it would be better to keep current implementation of public class func sendRequest<T: Request>(request: T, URLSession: NSURLSession = defaultURLSession, handler: (Result<T.Response, NSError>) -> Void = {r in}) -> NSURLSessionDataTask?
public class func sendRequest<T: Request>(request: T, URLSession: NSURLSession = defaultURLSession, handler: (Result<T.Response, NSError>, Int?) -> Void = {r in}) -> NSURLSessionDataTask? These methods can pass HTTP status code and keep backward compatibility. |
Thank you for you reply. // send request and build response object
public class func sendRequest<T: Request>(request: T, URLSession: NSURLSession = defaultURLSession, handler: (Result<T.Response, NSError>) -> Void = {r in}) -> NSURLSessionDataTask? {
return self.sendRequest(request, URLSession: URLSession, handler: { (r, s) in
handler(r)
})
} But it gives me and error 'Ambiguous use of 'sendRequest' when I trying APITests. |
Ah, Swift compiler can't determine type because Anyway, we have to make breaking change to pass status code to caller. There are some ways:
class Response<T> {
let statusCode: Int?
let result: Result<T, NSError>
} @pocket7878 @ikesyo How do you think? |
Hm...
I prefer former than later. Because it seperating concern. |
Sorry for the late response. I was on vacation last week. 😅
I also prefer the former. How about just passing |
OK, let's add sendRequest(request) { result, response in
switch result {
case .Success: // ...
case .Failure: // ...
}
} |
👍 for |
@@ -12,7 +12,7 @@ class ViewController: UITableViewController { | |||
|
|||
let request = GitHub.Endpoint.SearchRepositories(query: "APIKit") | |||
|
|||
GitHub.sendRequest(request) { response in | |||
GitHub.sendRequest(request) { response,status in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
result, response
?
👍 except for 1 note. Thank you for nice patch! |
@@ -76,7 +76,7 @@ public class API { | |||
} | |||
|
|||
// send request and build response object | |||
public class func sendRequest<T: Request>(request: T, URLSession: NSURLSession = defaultURLSession, handler: (Result<T.Response, NSError>) -> Void = {r in}) -> NSURLSessionDataTask? { | |||
public class func sendRequest<T: Request>(request: T, URLSession: NSURLSession = defaultURLSession, handler: (Result<T.Response, NSError>, NSHTTPURLResponse?) -> Void = {res, resp in}) -> NSURLSessionDataTask? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style: An extra space.
I'm sorry for the nitpicks. 😅 Other than them, looks good! 👌 |
@ikesyo @pocket7878 All the information about response should be expressed in Sample code for pagination: struct PaginatedResponse<T> {
var results: Array<T>
var nextPage: Int { get }
var hasNext: Bool { get }
init(results: Array<T>, URLResponse: NSHTTPURLResponse) {
self.results = results
self.nextPage = /* get nextPage from `Link` field of URLResponse */
self.hasNext = /* get hasNext from `Link` field of URLResponse */
}
}
struct SomePaginatedRequest: Request {
typealias Response = PaginatedResponse<Some>
var URLRequest: NSURLRequest? {
return SomeAPI.URLRequest(
method: .GET,
path: "/paginated",
parameters: ["page": page]
)
}
let page: Int
static func responseFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) -> Response? {
var somes = [Some]()
if let dictionaries = object as? [[String: AnyObject]] {
for dictionary in dictionaries {
if let some = Some(dictionary: dictionary) {
somes.append()
}
}
}
return PaginatedResponse(results: somes, URLResponse: URLResponse)
}
} let request = SomeAPI.SomePaginatedRequest(page: 1)
SomeAPI.sendRequest(request) { result in
switch result {
case .Success(let box):
let response = box.value
// response.results, response.hasNext and response.nextPage are available
case .Failure:
break
}
} Another example for 204, which indicates empty body: class SomeRequest: Request {
typealias Response = Some?
var URLRequest: NSURLRequest? {
return SomeAPI.URLRequest(
method: .GET,
path: "/some"
)
}
class func responseFromObject(object: AnyObject, URLResponse: NSHTTPURLResponse) -> Response? {
switch URLResponse.statusCode {
case 204:
// 204 indicates empty body
return nil
default:
return Some(object: object)
}
}
} |
@ishkawa Sorry for my late reply. |
OK, I will close this pull request. Thank you for giving a chance to think about HTTP status code and closure design. The idea I mentioned above is now available on master branch. Please try master if you need it! |
Some API returning multiple status code, so client needs to know what status code was returned.