Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(712)

Unified Diff: impl/prod/context.go

Issue 1549223002: Add remote_api support (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: tweak docstring Created 4 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: impl/prod/context.go
diff --git a/impl/prod/context.go b/impl/prod/context.go
index b62614891a0425a3fedc9c91bf5c231427d6b55f..73fec355fd739518a432bc310b43ae6acb7344b6 100644
--- a/impl/prod/context.go
+++ b/impl/prod/context.go
@@ -9,7 +9,9 @@ import (
"github.com/luci/gae/service/info"
"golang.org/x/net/context"
+ gOAuth "golang.org/x/oauth2/google"
"google.golang.org/appengine"
+ "google.golang.org/appengine/remote_api"
)
type key int
@@ -47,6 +49,12 @@ func AEContextNoTxn(c context.Context) context.Context {
return aeCtx
}
+func setupAECtx(c, aeCtx context.Context) context.Context {
+ c = context.WithValue(c, prodContextKey, aeCtx)
+ c = context.WithValue(c, prodContextNoTxnKey, aeCtx)
+ return useMail(useUser(useURLFetch(useRDS(useMC(useTQ(useGI(useLogging(c))))))))
+}
+
// Use adds production implementations for all the gae services to the
// context.
//
@@ -64,8 +72,36 @@ func AEContextNoTxn(c context.Context) context.Context {
//
// The implementations are all backed by the real appengine SDK functionality,
func Use(c context.Context, r *http.Request) context.Context {
- aeCtx := appengine.NewContext(r)
- c = context.WithValue(c, prodContextKey, aeCtx)
- c = context.WithValue(c, prodContextNoTxnKey, aeCtx)
- return useMail(useUser(useURLFetch(useRDS(useMC(useTQ(useGI(useLogging(c))))))))
+ return setupAECtx(c, appengine.NewContext(r))
+}
+
+// UseRemote is the same as Use, except that it lets you attach a context to
+// a remote host using the Remote API feature. See the docs for the
+// prerequisites.
+//
+// docs: https://cloud.google.com/appengine/docs/go/tools/remoteapi
+//
+// If client is nil, this will use a default Google OAuth2 client. Otherwise the
+// client must be configured to have the following OAuth2 scopes:
+// "https://www.googleapis.com/auth/appengine.apis"
+// "https://www.googleapis.com/auth/userinfo.email"
+// "https://www.googleapis.com/auth/cloud.platform"
+func UseRemote(c context.Context, host string, client *http.Client) (context.Context, error) {
+ err := error(nil)
+ if client == nil {
+ client, err = gOAuth.DefaultClient(context.Background(),
+ "https://www.googleapis.com/auth/appengine.apis",
+ "https://www.googleapis.com/auth/userinfo.email",
+ "https://www.googleapis.com/auth/cloud.platform",
+ )
+ if err != nil {
+ return nil, err
+ }
+ }
+
+ aeCtx, err := remote_api.NewRemoteContext(host, client)
+ if err != nil {
+ return nil, err
+ }
+ return setupAECtx(c, aeCtx), nil
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698