httpDo sends an HTTP request and returns an HTTP response.
(req *http.Request)
| 543 | |
| 544 | // httpDo sends an HTTP request and returns an HTTP response. |
| 545 | func (c *Client) httpDo(req *http.Request) (*http.Response, error) { |
| 546 | client := c.HTTPClient |
| 547 | if client == nil { |
| 548 | client = http.DefaultClient |
| 549 | } |
| 550 | |
| 551 | req.Header.Set("User-Agent", c.client.UserAgent) |
| 552 | req.Header.Set("Origin", "https://youtube.com") |
| 553 | req.Header.Set("Sec-Fetch-Mode", "navigate") |
| 554 | |
| 555 | if len(c.consentID) == 0 { |
| 556 | c.consentID = strconv.Itoa(rand.Intn(899) + 100) //nolint:gosec |
| 557 | } |
| 558 | |
| 559 | req.AddCookie(&http.Cookie{ |
| 560 | Name: "CONSENT", |
| 561 | Value: "YES+cb.20210328-17-p0.en+FX+" + c.consentID, |
| 562 | Path: "/", |
| 563 | Domain: ".youtube.com", |
| 564 | }) |
| 565 | |
| 566 | res, err := client.Do(req) |
| 567 | |
| 568 | log := slog.With("method", req.Method, "url", req.URL) |
| 569 | |
| 570 | if err == nil && res.StatusCode != http.StatusOK { |
| 571 | err = ErrUnexpectedStatusCode(res.StatusCode) |
| 572 | res.Body.Close() |
| 573 | res = nil |
| 574 | } |
| 575 | |
| 576 | if err != nil { |
| 577 | log.Debug("HTTP request failed", "error", err) |
| 578 | } else { |
| 579 | log.Debug("HTTP request succeeded", "status", res.Status) |
| 580 | } |
| 581 | |
| 582 | return res, err |
| 583 | } |
| 584 | |
| 585 | // httpGet does a HTTP GET request, checks the response to be a 200 OK and returns it |
| 586 | func (c *Client) httpGet(ctx context.Context, url string) (*http.Response, error) { |
no test coverage detected