ducky/devices

Paddy 2015-11-12

0:b6494e1a499e Go to Latest

ducky/devices/vendor/golang.org/x/net/context/ctxhttp/ctxhttp.go

Initial attempt. Create the basic types (Device, DeviceType, DeviceChange) that we'll be using in the service. Also, create our Storer interface, and the helper methods to store, retrieve, and update information in the datastore. Also, we're using Godep, so check in our Godeps folder and the vendor folder. Note that this only works on Go 1.5 and later, with the GO15VENDOREXPERIMENT environment variable set to 1.

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vendor/golang.org/x/net/context/ctxhttp/ctxhttp.go	Thu Nov 12 20:33:26 2015 -0800
     1.3 @@ -0,0 +1,79 @@
     1.4 +// Copyright 2015 The Go Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style
     1.6 +// license that can be found in the LICENSE file.
     1.7 +
     1.8 +// Package ctxhttp provides helper functions for performing context-aware HTTP requests.
     1.9 +package ctxhttp
    1.10 +
    1.11 +import (
    1.12 +	"io"
    1.13 +	"net/http"
    1.14 +	"net/url"
    1.15 +	"strings"
    1.16 +
    1.17 +	"golang.org/x/net/context"
    1.18 +)
    1.19 +
    1.20 +// Do sends an HTTP request with the provided http.Client and returns an HTTP response.
    1.21 +// If the client is nil, http.DefaultClient is used.
    1.22 +// If the context is canceled or times out, ctx.Err() will be returned.
    1.23 +func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
    1.24 +	if client == nil {
    1.25 +		client = http.DefaultClient
    1.26 +	}
    1.27 +
    1.28 +	// Request cancelation changed in Go 1.5, see cancelreq.go and cancelreq_go14.go.
    1.29 +	cancel := canceler(client, req)
    1.30 +
    1.31 +	type responseAndError struct {
    1.32 +		resp *http.Response
    1.33 +		err  error
    1.34 +	}
    1.35 +	result := make(chan responseAndError, 1)
    1.36 +
    1.37 +	go func() {
    1.38 +		resp, err := client.Do(req)
    1.39 +		result <- responseAndError{resp, err}
    1.40 +	}()
    1.41 +
    1.42 +	select {
    1.43 +	case <-ctx.Done():
    1.44 +		cancel()
    1.45 +		return nil, ctx.Err()
    1.46 +	case r := <-result:
    1.47 +		return r.resp, r.err
    1.48 +	}
    1.49 +}
    1.50 +
    1.51 +// Get issues a GET request via the Do function.
    1.52 +func Get(ctx context.Context, client *http.Client, url string) (*http.Response, error) {
    1.53 +	req, err := http.NewRequest("GET", url, nil)
    1.54 +	if err != nil {
    1.55 +		return nil, err
    1.56 +	}
    1.57 +	return Do(ctx, client, req)
    1.58 +}
    1.59 +
    1.60 +// Head issues a HEAD request via the Do function.
    1.61 +func Head(ctx context.Context, client *http.Client, url string) (*http.Response, error) {
    1.62 +	req, err := http.NewRequest("HEAD", url, nil)
    1.63 +	if err != nil {
    1.64 +		return nil, err
    1.65 +	}
    1.66 +	return Do(ctx, client, req)
    1.67 +}
    1.68 +
    1.69 +// Post issues a POST request via the Do function.
    1.70 +func Post(ctx context.Context, client *http.Client, url string, bodyType string, body io.Reader) (*http.Response, error) {
    1.71 +	req, err := http.NewRequest("POST", url, body)
    1.72 +	if err != nil {
    1.73 +		return nil, err
    1.74 +	}
    1.75 +	req.Header.Set("Content-Type", bodyType)
    1.76 +	return Do(ctx, client, req)
    1.77 +}
    1.78 +
    1.79 +// PostForm issues a POST request via the Do function.
    1.80 +func PostForm(ctx context.Context, client *http.Client, url string, data url.Values) (*http.Response, error) {
    1.81 +	return Post(ctx, client, url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
    1.82 +}