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 takes the new namespace as a string, and returns a context | |
M-A Ruel
2015/05/25 17:14:52
I think it's better to leave it out.
If we don't
iannucci
2015/05/26 18:25:06
I actually do plan on using it, and I know some ot
| |
34 // set to use that namespace, or an error. I'm not sure what the error | |
35 // could be, since it's not documented on the appengine SDK. | |
36 Namespace(namespace string) (context.Context, error) | |
37 | |
38 // Really global functions... these don't normally even require context, but | |
39 // for the purposes of testing+consistency, they're included here. | |
40 | |
41 Datacenter() string | |
42 InstanceID() string | |
43 IsDevAppserver() bool | |
44 ServerSoftware() string | |
45 | |
46 IsCapabilityDisabled(err error) bool | |
47 IsOverQuota(err error) bool | |
48 IsTimeoutError(err error) bool | |
49 | |
50 // Backends are deprecated in favor of modules, so simplify this a bit b y | |
51 // omitting them from the interface. | |
52 // BackendHostname(name string, index int) string | |
53 // BackendInstance() (name string, index int) | |
54 } | |
55 | |
56 // GIFactory is the function signature for factory methods compatible with | |
57 // SetGIFactory. | |
58 type GIFactory func(context.Context) GlobalInfo | |
59 | |
60 // GetGI gets gets the GlobalInfo implementation from context. | |
61 func GetGI(c context.Context) GlobalInfo { | |
62 obj := c.Value(globalInfoKey) | |
63 if obj == nil || obj.(GIFactory) == nil { | |
M-A Ruel
2015/05/25 17:14:52
http://golang.org/ref/spec#Type_assertions
iannucci
2015/05/26 18:25:06
http://play.golang.org/p/g1bN9UNSkS
Typed v. unty
| |
64 return nil | |
65 } | |
66 return obj.(GIFactory)(c) | |
67 } | |
68 | |
69 // SetGIFactory sets the function to produce GlobalInfo instances, as returned b y | |
70 // the GetGI method. | |
71 func SetGIFactory(c context.Context, gif GIFactory) context.Context { | |
72 return context.WithValue(c, globalInfoKey, gif) | |
73 } | |
74 | |
75 // SetGI sets the current GlobalInfo object in the context. Useful for testing | |
76 // with a quick mock. This is just a shorthand SetGIFactory invocation to set | |
77 // a factory which always returns the same object. | |
78 func SetGI(c context.Context, gi GlobalInfo) context.Context { | |
79 return SetGIFactory(c, func(context.Context) GlobalInfo { return gi }) | |
80 } | |
OLD | NEW |