Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package wrapper | |
| 6 | |
| 7 import ( | |
| 8 "time" | |
| 9 | |
| 10 "appengine" | |
| 11 | |
| 12 "golang.org/x/net/context" | |
| 13 ) | |
| 14 | |
| 15 // GlobalInfo is the interface for all of the package methods which normally | |
| 16 // would be in the 'appengine' package. | |
| 17 type GlobalInfo interface { | |
| 18 // methods usually requiring a Context | |
| 19 | |
| 20 AccessToken(scopes ...string) (token string, expiry time.Time, err error ) | |
| 21 AppID() string | |
| 22 DefaultVersionHostname() string | |
| 23 ModuleHostname(module, version, instance string) (string, error) | |
| 24 ModuleName() string | |
| 25 PublicCertificates() ([]appengine.Certificate, error) | |
| 26 RequestID() string | |
| 27 ServiceAccount() (string, error) | |
| 28 SignBytes(bytes []byte) (keyName string, signature []byte, err error) | |
| 29 VersionID() string | |
| 30 | |
| 31 // our tweaked interface | |
| 32 | |
| 33 Namespace(namespace string) (context.Context, error) | |
|
Vadim Sh.
2015/05/24 19:01:57
document
iannucci
2015/05/24 20:04:31
Done (as much as we can)
| |
| 34 | |
| 35 // Really global functions... these don't normally even require context, but | |
| 36 // for the purposes of testing+consistency, they're included here. | |
| 37 | |
| 38 Datacenter() string | |
| 39 InstanceID() string | |
| 40 IsDevAppserver() bool | |
| 41 ServerSoftware() string | |
| 42 | |
| 43 IsCapabilityDisabled(err error) bool | |
| 44 IsOverQuota(err error) bool | |
| 45 IsTimeoutError(err error) bool | |
| 46 | |
| 47 // Backends are deprecated in favor of modules, so simplify this a bit b y | |
| 48 // omitting them from the interface. | |
| 49 // BackendHostname(name string, index int) string | |
| 50 // BackendInstance() (name string, index int) | |
| 51 } | |
| 52 | |
| 53 // GIFactory is the function signature for factory methods compatible with | |
| 54 // SetGIFactory. | |
| 55 type GIFactory func(context.Context) GlobalInfo | |
| 56 | |
| 57 // GetGI gets gets the GlobalInfo implementation from context. | |
| 58 func GetGI(c context.Context) GlobalInfo { | |
| 59 obj := c.Value(globalInfoKey) | |
| 60 if obj == nil || obj.(GIFactory) == nil { | |
| 61 return nil | |
| 62 } | |
| 63 return obj.(GIFactory)(c) | |
| 64 } | |
| 65 | |
| 66 // SetGIFactory sets the function to produce GlobalInfo instances, as returned b y | |
| 67 // the GetGI method. | |
| 68 func SetGIFactory(c context.Context, gif GIFactory) context.Context { | |
| 69 return context.WithValue(c, globalInfoKey, gif) | |
| 70 } | |
| 71 | |
| 72 // SetGI sets the current GlobalInfo object in the context. Useful for testing | |
| 73 // with a quick mock. This is just a shorthand SetGIFactory invocation to set | |
| 74 // a factory which always returns the same object. | |
| 75 func SetGI(c context.Context, gi GlobalInfo) context.Context { | |
| 76 return SetGIFactory(c, func(context.Context) GlobalInfo { return gi }) | |
| 77 } | |
| OLD | NEW |