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)
| 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]. |
| 560 | func 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 |
searching dependent graphs…