File: session.py

package info (click to toggle)
python-coverage 7.6.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,120 kB
  • sloc: python: 30,196; ansic: 1,181; javascript: 773; makefile: 293; sh: 107; xml: 48
file content (30 lines) | stat: -rw-r--r-- 882 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt

"""Help make a requests Session with proper authentication."""

import os
import sys

import requests

_SESSION = None

def get_session():
    """Get a properly authenticated requests Session."""

    global _SESSION

    if _SESSION is None:
        # If GITHUB_TOKEN is in the environment, use it.
        token = os.environ.get("GITHUB_TOKEN")
        if token is None:
            sys.exit("!! Must have a GITHUB_TOKEN")

        _SESSION = requests.session()
        _SESSION.headers["Authorization"] = f"token {token}"
        # requests.get() will always prefer the .netrc file even if a header
        # is already set.  This tells it to ignore the .netrc file.
        _SESSION.trust_env = False

    return _SESSION