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

make api reject 304s so caller can implement conditional requests #673

Merged
merged 4 commits into from Dec 31, 2017
Merged

make api reject 304s so caller can implement conditional requests #673

merged 4 commits into from Dec 31, 2017

Conversation

jsg2021
Copy link

@jsg2021 jsg2021 commented Dec 22, 2017

I was attempting to implement conditional requests and hit an error in this library...

Copy link
Contributor

@gr2m gr2m left a comment

Choose a reason for hiding this comment

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

Hey I’m sorry but I don’t quite understand your changes? 304 is not an error and hence shouldn’t be handled like one. Can you elaborate on the problem you are trying to solve? I can’t merge it like this.

@jsg2021
Copy link
Author

jsg2021 commented Dec 23, 2017 via email

@jsg2021
Copy link
Author

jsg2021 commented Dec 23, 2017

I’m writing a program to query my orgs repos and generate a report. It’s pretty request heavy so i need to add the conditional request headers so i don’t exhaust my api limits. 304s are currently falling because it’s trying to parse a url from a non existant location header. I need to know explicitly that the response was a 304. While not an http error, it is an exception. when rased the caller can know nothing changed.

@gr2m
Copy link
Contributor

gr2m commented Dec 23, 2017

the location header is not present in a 304

gotcha. Could you create a failing test for a 304 and we take it from there?

@jsg2021
Copy link
Author

jsg2021 commented Dec 23, 2017 via email

@jsg2021
Copy link
Author

jsg2021 commented Dec 25, 2017

Merry Christmas!

I added a test for 304. It will fail without my original changes. No matter how you expect it to resolve...because the original issue is the code is assuming all 301-307 codes are redirects. 304s do not have a location header, and the url parser blows up if the value passed to it is not a string.

My understanding & expectation is that 300s are still not 200's. They are still exceptions that must be dealt with. The library can transparently handle redirects... that's fine, but 304 and 308+ are still exceptions...not necessarily errors, but abort/short=circuit the request in some way. In my opinion, a rejection is appropriate.

@gr2m
Copy link
Contributor

gr2m commented Dec 25, 2017

Thank you @jsg2021, Merry Christmas to you, too 🎄

I’ll check in with my fellow @octokit maintainers to see how they handle it in the kits for the other languages.

options.path = Url.parse(res.headers.location, true).path
httpSendRequest()
return
}

if (res.statusCode >= 400 || res.statusCode < 10) {
if (res.statusCode === 304 || res.statusCode >= 400 || res.statusCode < 10) {
callCallback(new error.HttpError(data, res.statusCode, res.headers))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a 304 status in https://github.com/octokit/node-github/blob/master/lib/error.js#L47-L48? Maybe add a comment to reference this pull request, as it looks a little confusing to have a single 304 besides all the 400+ error codes

Copy link
Author

Choose a reason for hiding this comment

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

sure thing, i’ll try to do it tomorrow

Copy link
Author

Choose a reason for hiding this comment

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

I've added the 304 to that file

@gr2m
Copy link
Contributor

gr2m commented Dec 25, 2017

Okay I think I understand the context now. It’s correct that node-github is currently not handling 304 responses the way it should according to the documentation on conditional requests.

I’d argue caching is within the scope of Octokit libraries, just like pagination and authentication, I’ll have to give it some more thought. I guess the main question is:

Would you expect the caching to be handled by node-github automatically by keeping state of etags and sending the If-None-Match accordingly?

Either way, the library does and will allow you to keep that state yourself, so it’s possible that a 304 response occurs without an internal state of the response. If the library would be able to keep state, it could resend the request and cache the response for future requests. In your case, I agree that the method call should respond with an error of some kind.

@jsg2021
Copy link
Author

jsg2021 commented Dec 27, 2017

Caching is a complicated realm. I would leave that to the consumers to decide on when/how/why to use caching. This library is more pure without that complicating things.

With this tweak, opting into caching is pretty trivial, and in my opinion “good enough” :)

@gr2m gr2m merged commit a0c65a2 into octokit:master Dec 31, 2017
@gr2m
Copy link
Contributor

gr2m commented Dec 31, 2017

Thanks @jsg2021 👍 A new release is on its way

@jsg2021 jsg2021 mentioned this pull request Jan 3, 2018
gr2m pushed a commit that referenced this pull request Jan 11, 2018
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

2 participants