Skip to content
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

Add a method to return DataResponse from DataRequest.responseString() #116

Conversation

philippelatulippe
Copy link

This is similar to PR #79 except for responseString() instead of responseJSON(). I wrote it to work identically to the code in that pull request.

It allows access to metrics (NSURLSessionTaskMetrics mentioned in issue #76 ) for those who need the response as a string, not JSON.

@philippelatulippe philippelatulippe force-pushed the feature/dataresponse-for-responseString branch from 0cdfb79 to 5797953 Compare April 20, 2018 11:29
@jacobh0
Copy link

jacobh0 commented May 19, 2018

Can we merge this?

@freak4pc
Copy link
Member

I'm not entirely sure we'd want to overload responseString this way. responseJSON only has one meaning - and responseString will now have an overload returning two very different variations.

@sunshinejr Any chance you have thoughts on this?

@sunshinejr
Copy link
Member

Hm, I'm not sure about this one. How about making it a dataResponse? Personally, I wouldn't expect that DataResponse<String>would be the return type of responseString function.

@jacobh0
Copy link

jacobh0 commented Jul 28, 2018

Ended up ditching RxAlamofire as its become rather out of date and surfaces no data operations. Just wrapped this response method in an observable manually and saved a Framework import.

@freak4pc
Copy link
Member

If the op of this PR wants to go with dataResponse I think we'll be OK with that. Glad you were able to solve your problem - Indeed if you only have a very narrow use case, using a library (for anything, really) might not be needed.

@philippelatulippe
Copy link
Author

Since the merge of PR #79, responseJSON does in fact have two meanings. It can return an observable of (HTTPURLResponse, Data) or an observable of DataResponse<Any>. In my merge request I copied this format exactly. But I agree that the naming isn't clear.

Should I rename both of them to dataResponseJSON and dataResponseString?

A further issue with both of these methods is that they don't return the DataResponse on error, which is important for the main use-case (metrics and error logging). I'm not sure how to implement this though: wrap the error and the DataResponse into a new error type, or call onNext even on error and let the consumer sort it out.

@philippelatulippe philippelatulippe force-pushed the feature/dataresponse-for-responseString branch from 5797953 to 482bbfe Compare June 24, 2019 10:48
@philippelatulippe
Copy link
Author

I have rebased this branch on top of master, and I've also updated the function names to distinguish them from the regular response functions. I would love to see this merged. :)

As an additional feature, the observables returned by these functions could also return the DataResponse on error. I'm not sure how to go about doing this. Would it be OK to emit the DataResponse as an error instead of the error?

The following code:

if let error = response.result.error {
      observer.on(.error(error))
}

would become:

if let _ = response.result.error {
      observer.on(response)
}

Subscribers would then need to extract the error themselves.

Alternatively, I could create a new error case that contains both the error and the response, or I could emit a tuple with both the error and the data response.

@philippelatulippe philippelatulippe force-pushed the feature/dataresponse-for-responseString branch from 482bbfe to 839b72c Compare June 24, 2019 11:31
@freak4pc
Copy link
Member

Hey @philippelatulippe - we follow semantic versioning, we can't make breaking changes like you did to the API without bumping a major version, and I wouldn't want to bump a major version for something to minor.

@philippelatulippe
Copy link
Author

Thanks for responding so quick, it wasn't my intention to make a breaking change to the API, renaming responseJSON was a mistake.

@philippelatulippe philippelatulippe force-pushed the feature/dataresponse-for-responseString branch from 839b72c to b97473e Compare June 26, 2019 09:12
@philippelatulippe
Copy link
Author

I've pushed a change to my commit which only introduces a new function, dataResponseString.

Copy link
Member

@freak4pc freak4pc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good with a few tiny notes.
Also wondering if it might be better as a Single but I don't mind too much.


- parameter encoding: Type of the string encoding, **default:** `nil`

- returns: An instance of `Observable<SDataResponse<String>>`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- returns: An instance of `Observable<SDataResponse<String>>`
- returns: An instance of `Observable<DataResponse<String>>`

@@ -848,6 +852,34 @@ extension Reactive where Base: DataRequest {
return result(responseSerializer: DataRequest.dataResponseSerializer())
}

/**
Returns an `Observable` of a DataResponse<String> for the current request.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Returns an `Observable` of a DataResponse<String> for the current request.
Returns an `Observable` of a `DataResponse<String>` for the current request.

@freak4pc
Copy link
Member

Would appreciate a second set of eyes if you have a minute @sunshinejr :)

Allows access to metrics (NSURLSessionTaskMetrics) for those who need the response as a string, not JSON.
@philippelatulippe philippelatulippe force-pushed the feature/dataresponse-for-responseString branch from b97473e to f8b38cc Compare June 26, 2019 14:25
@philippelatulippe
Copy link
Author

I pushed the changes based on the review suggestions. The tests seem to have failed because someone registered http://401.xyz.

@rynecheow
Copy link
Member

@philippelatulippe able to resolve the conflicts with master branch and update the PR?

@rynecheow rynecheow closed this Jun 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants