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
Support for Alamofire Statistical Metrics? #76
Comments
Hi, @gaplo917 |
@sergdort exactly! My project needs to time the API later... It would be super nice if RxAlamofire can return Observale that include all useful meta data. |
@sergdort Any workaround for this? I have a different use case. The backend error message contains a key which is needed to identify what causes a validation error. http code is 400 but it's not enough. The error key provides additional details for a signup function, examples can be invalid_password, or email_already_exists. |
I'm not 100% in-the-know of the structure of RxAlamofire but if you need a workaround, a simple wrapper function would assist here. Something like this would work @gaplo917 @simoami public func requestJSON(_ method: Alamofire.HTTPMethod,
_ url: URLConvertible) -> Observable<DataResponse<Any>> {
return Observable.create { observer in
let request = Alamofire.request(url)
request.responseJSON { resp in
observer.onNext(resp)
observer.onCompleted()
}
return Disposables.create {
request.cancel()
}
}
} |
I added code to allow this directly from RxAlamofire but
If this makes sense I can submit a PR and we can keep the discussion going there. |
@freak4pc maybe just |
@kzaher It comes specifically from responseJSON in Alamofire e.g. Alamofire.request(...).responseJSON { resp in
/// resp is DataResponse<Any>
} and also the naming convention across seems to be that Might need to have other overloads for raw data etc but the basic use case here is JSON response |
@freak4pc I think |
Yup thats why I tagged @sergdort who is definitely more active than me on this :) Using the library and maintaining, that is. I'm also in favor of The original responseJSON belongs to the response of a request() method (e.g. DataRequest). where request returns extension ObservableType where E == DataRequest {
func responseJSON() -> Observable<DataResponse<Any>> {
// some sort of flatmapping of self
}
} Some feedback from the community would be great here. Probably involving this level of complexity is less of a good idea. I think leaving everything under RxAlamofire.method is easier/more understood, so it's just a naming convention thing. |
@freak4pc @kzaher request(method, path, parameters: parameters, encoding: encoding, headers: headers)
.flatMap { (req: DataRequest) in
return req.validate({ (request, response, data) -> Alamofire.Request.ValidationResult in
// Only check for known error codes
if self.isHTTPSuccess(code: response.statusCode) { return .success }
let (name, message) = data != nil ? getHttpError(data!) : (nil, nil)
// do something with error name and message
})
.rx.json()
}
func getHttpError(_ json: Data) -> (String?, String?) {
var errorName: String?
var errorMessage: String?
let d = try? JSONSerialization.jsonObject(with: json, options: JSONSerialization.ReadingOptions.allowFragments)
if let data = d as? Dictionary<String, AnyObject> {
if let message = data["error"]?["message"] {
errorMessage = message!
}
if let name = data["error"]?["name"] {
errorName = name
}
}
return (errorName, errorMessage)
} |
Thanks @simoami for sharing! |
@freak4pc I'm aware of the difference to the original question, which is why I keep referring to mine as a specific "use case" for fetching response data. btw, mind pointing me to the new syntax? I didn't check if a new update was published to make the response accessible. would love to incorporate! |
Hey @simoami - Take a look here, I just pushed a PR explaining my thoughts on how to expose DataResponse. I'd love your feedback if it makes sense to you as an active user! |
Nice, great extension. It definitely looks easier. Will it also run onNext with other http status codes? say 401 or 500? |
No. Errors don't come back as onNext event, they come back as onError events |
Fixed by #79 |
Thanks @freak4pc. |
From Alamofire documentation,
DataResponse
provided a lot of useful information.Statistical Metrics
Timeline
Alamofire collects timings throughout the lifecycle of a
Request
and creates aTimeline
object exposed as a property on all response types.The above reports the following
Timeline
info:Latency
: 0.428 secondsRequest Duration
: 0.428 secondsSerialization Duration
: 0.001 secondsTotal Duration
: 0.429 secondsDoes RxAlamofire currently provides such API?
The text was updated successfully, but these errors were encountered: