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

Google App Engine compatibility #30

Closed
pjebs opened this issue Sep 18, 2016 · 3 comments · Fixed by #64
Closed

Google App Engine compatibility #30

pjebs opened this issue Sep 18, 2016 · 3 comments · Fixed by #64

Comments

@pjebs
Copy link

pjebs commented Sep 18, 2016

Introduction

Google App Engine is a sandboxed environment. We can't make calls to the outside world without using the net/http's Request to create a http.Client by:

import (
        "net/http"

        "google.golang.org/appengine"
        "google.golang.org/appengine/urlfetch"
)
ctx := appengine.NewContext(r)
client := urlfetch.Client(ctx)

Without doing that, the library doesn't work.

The way Stripe-Go-Library handles it is like this: https://github.com/stripe/stripe-go#google-appengine

Basically Stripe allows Google App Engine users the ability to feed in their own http Client and the library will then use that.

    ctx := appengine.NewContext(r)
    httpClient := urlfetch.Client(ctx)

    sc := client.New(key, stripe.NewBackends(httpClient))

The way I handle it is like this:

https://github.com/pjebs/optimus-go/blob/master/gae.go
https://github.com/pjebs/optimus-go/blob/master/standalone.go

My way may be a lot easier to implement.

In your code, everytime you refer to a http Client, you instead call: client() from the files above.

There will be no affect to the rest of your code.

More information

https://cloud.google.com/appengine/docs/go/issue-requests
https://cloud.google.com/appengine/docs/go/urlfetch/reference

@pjebs
Copy link
Author

pjebs commented Sep 18, 2016

Since you are using gorequest library @L102:

You can see that gorequest exposes the Client.

You can use the approach I used above so that for non-GAE, the function above literally re-returns the net/http Client reference.

For the GAE version, it can grab the Jar https://github.com/parnurzeal/gorequest/blob/develop/gorequest.go#L97,
create the GAE urlfetch.Client(ctx) and then replace it.

@mltucker
Copy link

mltucker commented Dec 16, 2016

I think it's not as simple since the client this library creates has the jsonKey baked in.

This seems to be how to get it running on GAE:

func New(ctx context.Context, jsonKey []byte) (Client, error) {
	conf, err := google.JWTConfigFromJSON(jsonKey, androidpublisher.AndroidpublisherScope)

	if err != nil {
		return Client{}, err
	}

	httpClient := &http.Client{
		Transport: &oauth2.Transport{
			Source: conf.TokenSource(ctx),
			Base: &urlfetch.Transport{
				Context: ctx,
			},
		},
	}

	return Client{
		httpClient: httpClient,
	}, err
}

@mltucker
Copy link

I'm doing app store verification manually. But to get play store verification working, I copied the playstore/validator.go to my project and edited it. Gist here:

https://gist.github.com/mltucker/db4df00918d7b917f49b60012b615c02

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 a pull request may close this issue.

2 participants