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

Unified Diff: service/urlfetch/context.go

Issue 1354083002: Add simplest service/urlfetch implementation. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: panic Created 5 years, 3 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 | « service/info/context.go ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: service/urlfetch/context.go
diff --git a/service/urlfetch/context.go b/service/urlfetch/context.go
new file mode 100644
index 0000000000000000000000000000000000000000..1fe901f30083b8ae5891b1ed21a9c3d7137ae865
--- /dev/null
+++ b/service/urlfetch/context.go
@@ -0,0 +1,46 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Package urlfetch provides a way for an application to get http.RoundTripper
+// that can make outbound HTTP requests. If used for https:// protocol, will
+// always validate SSL certificates.
+package urlfetch
+
+import (
+ "errors"
+ "net/http"
+
+ "golang.org/x/net/context"
+)
+
+type key int
+
+var serviceKey key
+
+// Factory is the function signature for factory methods compatible with
+// SetFactory.
+type Factory func(context.Context) http.RoundTripper
+
+// Get pulls http.RoundTripper implementation from context or panics if it
+// wasn't set. Use SetFactory(...) or Set(...) in unit tests to mock
+// the round tripper.
+func Get(c context.Context) http.RoundTripper {
+ if f, ok := c.Value(serviceKey).(Factory); ok && f != nil {
+ return f(c)
+ }
+ panic(errors.New("no http.RoundTripper is set in context"))
+}
+
+// SetFactory sets the function to produce http.RoundTripper instances,
+// as returned by the Get method.
+func SetFactory(c context.Context, f Factory) context.Context {
+ return context.WithValue(c, serviceKey, f)
+}
+
+// Set sets the current http.RoundTripper object in the context. Useful for
+// testing with a quick mock. This is just a shorthand SetFactory invocation
+// to set a factory which always returns the same object.
+func Set(c context.Context, r http.RoundTripper) context.Context {
+ return SetFactory(c, func(context.Context) http.RoundTripper { return r })
+}
« no previous file with comments | « service/info/context.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698