MCPcopy Index your code
hub / github.com/google/go-github / newClient

Function newClient

github/github.go:560–685  ·  view source on GitHub ↗

newClient creates a new Client with the provided options. This is an internal helper function that is called by [NewClient] and [Client.Clone].

(opts clientOptions)

Source from the content-addressed store, hash-verified

558// newClient creates a new Client with the provided options. This is an internal
559// helper function that is called by [NewClient] and [Client.Clone].
560func newClient(opts clientOptions) (*Client, error) {
561 c := &Client{}
562
563 if opts.httpClient != nil {
564 c.client = opts.httpClient
565 } else {
566 c.client = &http.Client{}
567 }
568
569 if opts.transport != nil {
570 c.client.Transport = opts.transport
571 }
572
573 if opts.timeout != nil {
574 c.client.Timeout = *opts.timeout
575 }
576
577 if opts.envProxy {
578 transport := c.client.Transport
579 if transport == nil {
580 transport = http.DefaultTransport
581 }
582
583 t, ok := transport.(*http.Transport)
584 if !ok {
585 return nil, errors.New("cannot set environment proxy on non-http transport")
586 }
587
588 t2 := t.Clone()
589 t2.Proxy = http.ProxyFromEnvironment
590 c.client.Transport = t2
591 }
592
593 if opts.token != nil {
594 transport := c.client.Transport
595 if transport == nil {
596 transport = http.DefaultTransport
597 }
598 c.client.Transport = roundTripperFunc(func(req *http.Request) (*http.Response, error) {
599 req = req.Clone(req.Context())
600 req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", *opts.token))
601 return transport.RoundTrip(req)
602 })
603 }
604
605 c.clientIgnoreRedirects = &http.Client{
606 Transport: c.client.Transport,
607 Timeout: c.client.Timeout,
608 Jar: c.client.Jar,
609 CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse },
610 }
611
612 if opts.userAgent != nil {
613 c.userAgent = *opts.userAgent
614 } else {
615 c.userAgent = defaultUserAgent
616 }
617

Callers 3

NewClientFunction · 0.70
CloneMethod · 0.70
Test_newClientFunction · 0.70

Calls 3

roundTripperFuncFuncType · 0.85
CloneMethod · 0.80
RoundTripMethod · 0.45

Tested by 1

Test_newClientFunction · 0.56

Used in the wild real call sites across dependent graphs

searching dependent graphs…