當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Client.Transport方法代碼示例

本文整理匯總了Golang中net/http.Client.Transport方法的典型用法代碼示例。如果您正苦於以下問題:Golang Client.Transport方法的具體用法?Golang Client.Transport怎麽用?Golang Client.Transport使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在net/http.Client的用法示例。


在下文中一共展示了Client.Transport方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: WithHTTPClient

// WithHTTPClient configures a Maps API client with a http.Client to make requests over.
func WithHTTPClient(c *http.Client) ClientOption {
	return func(client *Client) error {
		if _, ok := c.Transport.(*transport); !ok {
			t := c.Transport
			if t != nil {
				c.Transport = &transport{Base: t}
			} else {
				c.Transport = &transport{Base: http.DefaultTransport}
			}
		}
		client.httpClient = c
		return nil
	}
}
開發者ID:nicholasray,項目名稱:sandstone,代碼行數:15,代碼來源:client.go

示例2: fiddler2_enable

func fiddler2_enable(client *http.Client, proxys string) {
	transport := &http.Transport{}
	http_proxy := url.URL{}
	proxy, _ := http_proxy.Parse(proxys)
	transport.Proxy = http.ProxyURL(proxy)
	client.Transport = transport
}
開發者ID:luoshuhui,項目名稱:catm,代碼行數:7,代碼來源:catm.go

示例3: NewRemoteContext

// NewRemoteContext returns a context that gives access to the production
// APIs for the application at the given host. All communication will be
// performed over SSL unless the host is localhost.
func NewRemoteContext(host string, client *http.Client) (appengine.Context, error) {
	// Add an appcfg header to outgoing requests.
	t := client.Transport
	if t == nil {
		t = http.DefaultTransport
	}
	client.Transport = &headerAddingRoundTripper{t}

	url := url.URL{
		Scheme: "https",
		Host:   host,
		Path:   "/_ah/remote_api",
	}
	if host == "localhost" || strings.HasPrefix(host, "localhost:") {
		url.Scheme = "http"
	}
	u := url.String()
	appID, err := getAppID(client, u)
	if err != nil {
		return nil, fmt.Errorf("unable to contact server: %v", err)
	}
	return &context{
		client: client,
		url:    u,
		appID:  appID,
	}, nil
}
開發者ID:odeke-em,項目名稱:appengine-go,代碼行數:30,代碼來源:client.go

示例4: Request

func (c *PleskApiClient) Request(request string) (response string, err error) {
	url := fmt.Sprintf("%s://%s:%d/enterprise/control/agent.php", c.protocol, c.host, c.port)

	req, err := http.NewRequest("POST", url, strings.NewReader(request))
	if err != nil {
		return
	}
	req.Header.Add("Content-type", "text/xml")
	req.Header.Add("HTTP_PRETTY_PRINT", "TRUE")
	if c.secretKey != "" {
		req.Header.Add("KEY", c.secretKey)
	} else {
		req.Header.Add("HTTP_AUTH_LOGIN", c.login)
		req.Header.Add("HTTP_AUTH_PASSWD", c.password)
	}

	client := http.Client{}
	if c.InsecureSkipVerify {
		tr := &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		}
		client.Transport = tr
	}

	resp, err := client.Do(req)
	if err != nil {
		return
	}
	defer resp.Body.Close()
	bytes, err := ioutil.ReadAll(resp.Body)
	response = string(bytes)
	return
}
開發者ID:plesk,項目名稱:api-examples,代碼行數:33,代碼來源:plesk_api_client.go

示例5: main

func main() {

	transport := new(http.Transport)
	transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}

	client := new(http.Client)

	client.Transport = transport

	url := "https://10.80.65.70:9443/rest/agent/1a5dc1ef-f0e6-4aad-839a-a7880d539e8d"
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		panic(err)
	}

	creds := convertCredentials("admin", "admin")

	req.Header.Add("Authorization", "Basic "+creds)

	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}

	fmt.Println(resp)

}
開發者ID:OMGapotato,項目名稱:goserver,代碼行數:27,代碼來源:restTest2.go

示例6: NewRemoteContext

// NewRemoteContext returns a context that gives access to the production
// APIs for the application at the given host. All communication will be
// performed over SSL unless the host is localhost.
func NewRemoteContext(host string, client *http.Client) (context.Context, error) {
	// Add an appcfg header to outgoing requests.
	t := client.Transport
	if t == nil {
		t = http.DefaultTransport
	}
	client.Transport = &headerAddingRoundTripper{t}

	url := url.URL{
		Scheme: "https",
		Host:   host,
		Path:   "/_ah/remote_api",
	}
	if host == "localhost" || strings.HasPrefix(host, "localhost:") {
		url.Scheme = "http"
	}
	u := url.String()
	appID, err := getAppID(client, u)
	if err != nil {
		return nil, fmt.Errorf("unable to contact server: %v", err)
	}
	rc := &remoteContext{
		client: client,
		url:    u,
	}
	ctx := internal.WithCallOverride(context.Background(), rc.call)
	ctx = internal.WithLogOverride(ctx, rc.logf)
	ctx = internal.WithAppIDOverride(ctx, appID)
	return ctx, nil
}
開發者ID:nildev,項目名稱:account,代碼行數:33,代碼來源:client.go

示例7: NewClient

// Create a new client for making http requests against a Jazz server with the provided credentials
// The client will execute the requests authenticating somewhat transparently when needed
func NewClient(userID string, password string) (*Client, error) {
	jClient := &Client{}

	jClient.userID = userID
	jClient.password = password

	options := cookiejar.Options{
		PublicSuffixList: publicsuffix.List,
	}
	jar, err := cookiejar.New(&options)
	if err != nil {
		return nil, err
	}
	client := http.Client{Jar: jar}

	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	client.Transport = tr
	client.CheckRedirect = nil

	jClient.httpClient = &client

	// Provide a no-op logger as the default
	jClient.Log = log.New(ioutil.Discard, "", log.LstdFlags)

	return jClient, nil
}
開發者ID:sirnewton01,項目名稱:gojazz,代碼行數:30,代碼來源:client.go

示例8: NewClient

func NewClient(host string, User string, Passwd string, httpClient *http.Client) (client *Client, err error) {
	if httpClient == nil {
		httpClient = http.DefaultClient
	}

	transport := &httpclient.Transport{
		ConnectTimeout:        1 * time.Second,
		ResponseHeaderTimeout: 10 * time.Second,
		RequestTimeout:        30 * time.Second,
	}
	defer transport.Close()
	httpClient.Transport = transport

	client = &Client{
		client:    httpClient,
		UserAgent: userAgent,
		apiUser:   User,
		apiPasswd: Passwd,
	}

	if err := client.SetHost(host); err != nil {
		return nil, err
	}

	client.Repositories = &RepositoriesService{client: client}
	client.Tasks = &TasksService{client: client}

	return
}
開發者ID:msutter,項目名稱:nodetree,代碼行數:29,代碼來源:pulp.go

示例9: NewSession

// TODO(tiborvass): remove authConfig param once registry client v2 is vendored
func NewSession(client *http.Client, authConfig *cliconfig.AuthConfig, endpoint *Endpoint) (r *Session, err error) {
	r = &Session{
		authConfig:    authConfig,
		client:        client,
		indexEndpoint: endpoint,
	}

	var alwaysSetBasicAuth bool

	// If we're working with a standalone private registry over HTTPS, send Basic Auth headers
	// alongside all our requests.
	if endpoint.VersionString(1) != IndexServerAddress() && endpoint.URL.Scheme == "https" {
		info, err := endpoint.Ping()
		if err != nil {
			return nil, err
		}

		if info.Standalone && authConfig != nil {
			logrus.Debugf("Endpoint %s is eligible for private registry. Enabling decorator.", endpoint.String())
			alwaysSetBasicAuth = true
		}
	}

	// Annotate the transport unconditionally so that v2 can
	// properly fallback on v1 when an image is not found.
	client.Transport = AuthTransport(client.Transport, authConfig, alwaysSetBasicAuth)

	jar, err := cookiejar.New(nil)
	if err != nil {
		return nil, errors.New("cookiejar.New is not supposed to return an error")
	}
	client.Jar = jar

	return r, nil
}
開發者ID:AdamOssenford,項目名稱:docker-pi,代碼行數:36,代碼來源:session.go

示例10: NewClient

// NewClient initializes a new API client for the given host and API version.
// It uses the given http client as transport.
// It also initializes the custom http headers to add to each request.
//
// It won't send any version information if the version number is empty. It is
// highly recommended that you set a version or your client may break if the
// server is upgraded.
func NewClient(host string, version string, client *http.Client, httpHeaders map[string]string) (*Client, error) {
	proto, addr, basePath, err := ParseHost(host)
	if err != nil {
		return nil, err
	}

	if client == nil {
		client = &http.Client{}
	}

	if client.Transport == nil {
		// setup the transport, if not already present
		transport := new(http.Transport)
		sockets.ConfigureTransport(transport, proto, addr)
		client.Transport = transport
	}

	return &Client{
		host:              host,
		proto:             proto,
		addr:              addr,
		basePath:          basePath,
		client:            client,
		version:           version,
		customHTTPHeaders: httpHeaders,
	}, nil
}
開發者ID:vdemeester,項目名稱:rancher-compose,代碼行數:34,代碼來源:client.go

示例11: TestTransformingTransport

func TestTransformingTransport(t *testing.T) {
	client := new(http.Client)
	tr := &TransformingTransport{testTransport{}, client}
	client.Transport = tr

	tests := []struct {
		url         string
		code        int
		expectError bool
	}{
		{"http://good.test/png#1", http.StatusOK, false},
		{"http://good.test/error#1", http.StatusInternalServerError, true},
		// TODO: test more than just status code... verify that image
		// is actually transformed and returned properly and that
		// non-image responses are returned as-is
	}

	for _, tt := range tests {
		req, _ := http.NewRequest("GET", tt.url, nil)

		resp, err := tr.RoundTrip(req)
		if err != nil {
			if !tt.expectError {
				t.Errorf("RoundTrip(%v) returned unexpected error: %v", tt.url, err)
			}
			continue
		} else if tt.expectError {
			t.Errorf("RoundTrip(%v) did not return expected error", tt.url)
		}
		if got, want := resp.StatusCode, tt.code; got != want {
			t.Errorf("RoundTrip(%v) returned status code %d, want %d", tt.url, got, want)
		}
	}
}
開發者ID:rebeccahughes,項目名稱:imageproxy,代碼行數:34,代碼來源:imageproxy_test.go

示例12: WithContext

// WithContext returns a new context in a similar way NewContext does,
// but initiates the new context with the specified parent.
func WithContext(parent context.Context, projID string, c *http.Client) context.Context {
	// TODO(bradfitz): delete internal.Transport. It's too wrappy for what it does.
	// Do User-Agent some other way.
	if _, ok := c.Transport.(*internal.Transport); !ok {
		c.Transport = &internal.Transport{Base: c.Transport}
	}
	return internal.WithContext(parent, projID, c)
}
開發者ID:CowLeo,項目名稱:distribution,代碼行數:10,代碼來源:cloud.go

示例13: NewReplayerClient

func NewReplayerClient(r io.Reader) (http.Client, error) {
	client := http.Client{}
	transport, err := NewReplayerTransport(r)
	if err != nil {
		return client, err
	}
	client.Transport = transport
	return client, err
}
開發者ID:undernewmanagement,項目名稱:besticon,代碼行數:9,代碼來源:vcr.go

示例14: NewWithClient

func NewWithClient(client *http.Client) *Recorder {
	r := &Recorder{Client: client}
	r.originalTransport = client.Transport
	if r.originalTransport == nil {
		r.originalTransport = &http.Transport{}
	}
	client.Transport = &roundTripRecorder{RoundTripper: r.originalTransport}
	return r
}
開發者ID:devick,項目名稱:flynn,代碼行數:9,代碼來源:httprecorder.go

示例15: ClientTLSConfig

// Returns the http.Client's TLS Config, traversing and generating any
// defaults along the way to get it.
func ClientTLSConfig(cl *http.Client) *tls.Config {
	if cl.Transport == nil {
		cl.Transport = http.DefaultTransport
	}
	tr := cl.Transport.(*http.Transport)
	if tr.TLSClientConfig == nil {
		tr.TLSClientConfig = &tls.Config{}
	}
	return tr.TLSClientConfig
}
開發者ID:CaptainIlu,項目名稱:cloud-torrent,代碼行數:12,代碼來源:client.go


注:本文中的net/http.Client.Transport方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。