Skip to content

Fix Google App Engine support and provide working example #697

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 3 commits into from
Aug 16, 2017

Conversation

gmlewis
Copy link
Collaborator

@gmlewis gmlewis commented Aug 16, 2017

Fixes #578.

Fixes google#578.

Change-Id: Id7f3cc079d12b9d6d27baa2308c78a04ceaeb181
@gmlewis gmlewis requested a review from dmitshur August 16, 2017 04:29
)

func withContext(ctx context.Context, req *http.Request) (context.Context, *http.Request) {
return appengine.WithContext(ctx, req), req
return ctx, req
Copy link
Member

Choose a reason for hiding this comment

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

Given this change, I think you can simplify withContext signature to return only *http.Request then. Can you?

Copy link
Member

@dmitshur dmitshur Aug 16, 2017

Choose a reason for hiding this comment

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

Also, now that this func is a no-op for appengine case, I think you should add a comment explaining that. E.g.:

// No-op implementation for appengine because it doesn't support adding context to a request.
return req

Change-Id: I8aa5b61c4c65e7dc70ff6ad17d9bbc52f2c52b28
Copy link
Member

@dmitshur dmitshur left a comment

Choose a reason for hiding this comment

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

I'm not an app engine user, so I can't review that side thoroughly, and will leave it to you.

The non-appengine side looks good to me.

LGTM.

&oauth2.Token{AccessToken: os.Getenv("GITHUB_AUTH_TOKEN")},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
Copy link
Member

Choose a reason for hiding this comment

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

So a new client must be made for every incoming request? That seems pretty unfortunate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was expedient for this demo, but I believe it is possible to make a single client as a package global during initialization.

One caveat is that you can't call os.Getenv on App Engine within an init function due to the environment not being fully passed-in on app startup. The details are quite hairy.

So in cases like that, you could have a sync.Once block get the environment variable value and create the client before its first use... but I wanted to have a simple example here and not complicate things too much.

commits, _, err := client.Repositories.ListCommits(ctx, "google", "go-github", nil)
if err != nil {
log.Errorf(ctx, "ListCommits: %v", err)
return
Copy link
Member

Choose a reason for hiding this comment

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

You haven't written anything to w yet, so you could also do http.Error(w, err.Error(), http.Status...) here.

@@ -399,7 +399,7 @@ func parseRate(r *http.Response) Rate {
// The provided ctx must be non-nil. If it is canceled or times out,
// ctx.Err() will be returned.
func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Response, error) {
ctx, req = withContext(ctx, req)
req = withContext(ctx, req)
Copy link
Member

Choose a reason for hiding this comment

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

This is one of those rare times where I feel it might be worth breaking the "context is always the first parameter" pattern.

Here, withContext is basically a method-as-a-function that adds ctx to req. I think seeing it like this:

req = withContext(req, ctx)

Might read better...

Well, after writing it up and looking at it for a few minutes, I'm not so sure it looks better. It seems about about the same. 😑

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agreed. I'll leave it alone.

Change-Id: I2a5ef31709f7333004fc8e4f29761e46e6236900
@gmlewis
Copy link
Collaborator Author

gmlewis commented Aug 16, 2017

Thank you, @shurcooL! Merging.

@gmlewis gmlewis merged commit 163ced7 into google:master Aug 16, 2017
@gmlewis gmlewis deleted the appengine-fix branch August 16, 2017 21:47
nbareil pushed a commit to nbareil/go-github that referenced this pull request May 1, 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.

2 participants