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