+
Skip to content

Return http.Response from DownloadContents #1542

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

Merged
merged 4 commits into from
Aug 22, 2020

Conversation

mctofu
Copy link
Contributor

@mctofu mctofu commented May 30, 2020

This is the breaking change we discussed at: #1531 (comment)

Fixes #1531

@googlebot googlebot added the cla: yes Indication that the PR author has signed a Google Contributor License Agreement. label May 30, 2020
@codecov
Copy link

codecov bot commented May 30, 2020

Codecov Report

Merging #1542 into master will increase coverage by 0.02%.
The diff coverage is 75.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1542      +/-   ##
==========================================
+ Coverage   67.90%   67.92%   +0.02%     
==========================================
  Files          94       94              
  Lines        8670     8670              
==========================================
+ Hits         5887     5889       +2     
+ Misses       1882     1880       -2     
  Partials      901      901              
Impacted Files Coverage Δ
github/repos_contents.go 62.33% <75.00%> (ø)
github/messages.go 81.17% <0.00%> (ø)
github/event.go 93.87% <0.00%> (+2.04%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a86f060...7802c66. Read the comment docs.

Copy link
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

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

Thanks, @mctofu. Since we have the response, let's go ahead and return it.

dir := path.Dir(filepath)
filename := path.Base(filepath)
_, dirContents, _, err := s.GetContents(ctx, owner, repo, dir, opts)
if err != nil {
return nil, err
return nil, nil, err
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since s.GetContents returns the response with its third return value, let's go ahead and return it here.

_, dirContents, resp, err := s.GetContents(ctx, owner, repo, dir, opts)
if err != nil {
  return nil, resp, err

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The response from s.GetContents is of type *github.Response rather than *http.Response. Do you think there is any value in returning just the http.Response from the github.Response?

We could also consider adding a 4th value to the return to allow the caller to check the rate limits from s.GetContents regardless of other errors?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah! Gotcha. Hmmm... the vast majority of calls in this repo return *github.Response... is it a simple matter to do the same for this endpoint? That would be ideal so that this endpoint would not be a glaring exception.
I'm thinking you can wrap the regular *http.Response just by calling newResponse (around line 438 in github.go) and returning the result.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, that looks doable. Would it make sense to return the *github.Response from the s.GetContents call with its *http.Response swapped out with the one from requesting the download url? I'm not very familiar with what callers are expecting from the github.Response but that would at least expose the rate limit information from s.GetContents as part of the return value.

This also makes me think it'd be useful to have a method like RepositoriesService.GetDownloadURL(ctx context.Context, owner, repo, filepath string, opts *RepositoryContentGetOptions) (string, *github.Response, error) that just returns the URL and leaves it up to the caller to make a request for the contents.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmmm... I would return the most recent response because the point of this PR is to return the most recent response code... so I don't think we want to return anything but the response from the most recent http call, if that makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, made the change. I worry a little that this looks consistent by returning github.Response but may not behave consistently. The call to get the download url contents isn't an api call so we aren't able to populate any additional fields on the github.Response like the remaining rate limits.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right, but I think this is probably the best option, so let's try it out. If it causes problems in practice, we can make another breaking API change later. 😄

But honestly, I like this best because the other calls that might fail could also be caused by a rate limit or some other issue that we are now exposing back to the caller.

}
for _, contents := range dirContents {
if *contents.Name == filename {
if contents.DownloadURL == nil || *contents.DownloadURL == "" {
return nil, fmt.Errorf("No download link found for %s", filepath)
return nil, nil, fmt.Errorf("No download link found for %s", filepath)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here...
return nil, resp, fmt.Errorf...

}
}
return nil, fmt.Errorf("No file named %s found in %s", filename, dir)
return nil, nil, fmt.Errorf("No file named %s found in %s", filename, dir)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here.
return nil, resp, fmt.Errorf...

@gmlewis gmlewis added the Breaking API Change PR will require a bump to the major version num in next release. Look here to see the change(s). label May 30, 2020
// is from a successful response.
func (s *RepositoriesService) DownloadContents(ctx context.Context, owner, repo, filepath string, opts *RepositoryContentGetOptions) (io.ReadCloser, *http.Response, error) {
// It is possible for the download to result in a failed response when the
// returned error is nil. Callers should check the returned http.Response
Copy link
Collaborator

Choose a reason for hiding this comment

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

http.Response => Response

Copy link
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

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

Thank you, @mctofu!
LGTM.

Awaiting second LGTM before merging.

@gmlewis gmlewis requested review from gauntface and wesleimp May 31, 2020 03:18
@gmlewis
Copy link
Collaborator

gmlewis commented Jun 24, 2020

Ping @wesleimp - thank you!

Copy link
Collaborator

@wesleimp wesleimp left a comment

Choose a reason for hiding this comment

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

Thank you, @mctofu
LGTM 👌🏼

@gmlewis
Copy link
Collaborator

gmlewis commented Aug 22, 2020

Thank you, @wesleimp !
Merging.

Note that this is a breaking API change.

@gmlewis gmlewis merged commit d57a3a8 into google:master Aug 22, 2020
n1lesh pushed a commit to n1lesh/go-github that referenced this pull request Oct 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking API Change PR will require a bump to the major version num in next release. Look here to see the change(s). cla: yes Indication that the PR author has signed a Google Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RepositoriesService.DownloadContents should handle response codes
4 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载