GeezerVison is showing github.com — Merge pull request #1277 from python-gitlab/feat/override-user-agent · python-gitlab/python-gitlab@643454c
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/api-usage.rst +3Lines changed: 3 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -26,6 +26,9 @@ To connect to a GitLab server, create a ``gitlab.Gitlab`` object: | 26 | 26 | # anonymous gitlab instance, read-only for public resources | 27 | 27 | gl = gitlab.Gitlab('http://10.0.0.1') | 28 | 28 |
| | 29 | + # Define your own custom user agent for requests | | 30 | + gl = gitlab.Gitlab('http://10.0.0.1', user_agent='my-package/1.0.0') | | 31 | +
| 29 | 32 | # make an API request to create the gl.user object. This is mandatory if you | 30 | 33 | # use the username/password authentication. | 31 | 34 | gl.auth() | |
Collapse file Copy file name to clipboardExpand all lines: docs/cli-usage.rst +3Lines changed: 3 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -78,6 +78,9 @@ parameters. You can override the values in each GitLab server section. | 78 | 78 | - Integer between 1 and 100 | 79 | 79 | - The number of items to return in listing queries. GitLab limits the | 80 | 80 | value at 100. | | 81 | + * - ``user_agent`` | | 82 | + - ``str`` | | 83 | + - A string defining a custom user agent to use when ``gitlab`` makes requests. | 81 | 84 |
| 82 | 85 | You must define the ``url`` in each GitLab server section. | 83 | 86 |
| |
Collapse file Copy file name to clipboardExpand all lines: gitlab/__init__.py +12-8Lines changed: 12 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 24 | 24 | import requests.utils | 25 | 25 |
| 26 | 26 | import gitlab.config | | 27 | +from gitlab.__version__ import ( | | 28 | + __author__, | | 29 | + __copyright__, | | 30 | + __email__, | | 31 | + __license__, | | 32 | + __title__, | | 33 | + __version__, | | 34 | +) | 27 | 35 | from gitlab.const import * # noqa | 28 | 36 | from gitlab.exceptions import * # noqa | 29 | 37 | from gitlab import utils # noqa | 30 | 38 | from requests_toolbelt.multipart.encoder import MultipartEncoder | 31 | 39 |
| 32 | 40 |
| 33 | | -__title__ = "python-gitlab" | 34 | | -__version__ = "2.6.0" | 35 | | -__author__ = "Gauvain Pocentek" | 36 | | - | 37 | | -__license__ = "LGPL3" | 38 | | -__copyright__ = "Copyright 2013-2019 Gauvain Pocentek" | 39 | | - | 40 | 41 | warnings.filterwarnings("default", category=DeprecationWarning, module="^gitlab") | 41 | 42 |
| 42 | 43 | REDIRECT_MSG = ( | @@ -64,6 +65,7 @@ class Gitlab(object): | 64 | 65 | api_version (str): Gitlab API version to use (support for 4 only) | 65 | 66 | pagination (str): Can be set to 'keyset' to use keyset pagination | 66 | 67 | order_by (str): Set order_by globally | | 68 | + user_agent (str): A custom user agent to use for making HTTP requests. | 67 | 69 | """ | 68 | 70 |
| 69 | 71 | def __init__( | @@ -81,6 +83,7 @@ def __init__( | 81 | 83 | per_page=None, | 82 | 84 | pagination=None, | 83 | 85 | order_by=None, | | 86 | + user_agent=USER_AGENT, | 84 | 87 | ): | 85 | 88 |
| 86 | 89 | self._api_version = str(api_version) | @@ -90,7 +93,7 @@ def __init__( | 90 | 93 | #: Timeout to use for requests to gitlab server | 91 | 94 | self.timeout = timeout | 92 | 95 | #: Headers that will be used in request to GitLab | 93 | | - self.headers = {"User-Agent": "%s/%s" % (__title__, __version__)} | | 96 | + self.headers = {"User-Agent": user_agent} | 94 | 97 |
| 95 | 98 | #: Whether SSL certificates should be validated | 96 | 99 | self.ssl_verify = ssl_verify | @@ -204,6 +207,7 @@ def from_config(cls, gitlab_id=None, config_files=None): | 204 | 207 | per_page=config.per_page, | 205 | 208 | pagination=config.pagination, | 206 | 209 | order_by=config.order_by, | | 210 | + user_agent=config.user_agent, | 207 | 211 | ) | 208 | 212 |
| 209 | 213 | def auth(self): | |
Collapse file Copy file name to clipboard +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +__author__ = "Gauvain Pocentek, python-gitlab team" | | 2 | +__copyright__ = "Copyright 2013-2019 Gauvain Pocentek, 2019-2021 python-gitlab team" | | 3 | + | | 4 | +__license__ = "LGPL3" | | 5 | +__title__ = "python-gitlab" | | 6 | +__version__ = "2.6.0" |
Collapse file Copy file name to clipboardExpand all lines: gitlab/config.py +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 18 | 18 | import os | 19 | 19 | import configparser | 20 | 20 |
| | 21 | +from gitlab.const import USER_AGENT | | 22 | + | 21 | 23 |
| 22 | 24 | def _env_config(): | 23 | 25 | if "PYTHON_GITLAB_CFG" in os.environ: | @@ -177,3 +179,13 @@ def __init__(self, gitlab_id=None, config_files=None): | 177 | 179 | self.order_by = self._config.get(self.gitlab_id, "order_by") | 178 | 180 | except Exception: | 179 | 181 | pass | | 182 | + | | 183 | + self.user_agent = USER_AGENT | | 184 | + try: | | 185 | + self.user_agent = self._config.get("global", "user_agent") | | 186 | + except Exception: | | 187 | + pass | | 188 | + try: | | 189 | + self.user_agent = self._config.get(self.gitlab_id, "user_agent") | | 190 | + except Exception: | | 191 | + pass |
Collapse file Copy file name to clipboardExpand all lines: gitlab/const.py +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 | # You should have received a copy of the GNU Lesser General Public License | 16 | 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 17 | 17 |
| | 18 | +from gitlab.__version__ import __title__, __version__ | | 19 | + | | 20 | + | 18 | 21 | NO_ACCESS = 0 | 19 | 22 | MINIMAL_ACCESS = 5 | 20 | 23 | GUEST_ACCESS = 10 | | 51 | 54 |
| 52 | 55 | # specific project scope | 53 | 56 | SEARCH_SCOPE_PROJECT_NOTES = "notes" | | 57 | + | | 58 | +USER_AGENT = "{}/{}".format(__title__, __version__) |
Collapse file Copy file name to clipboardExpand all lines: gitlab/tests/test_config.py +32-1Lines changed: 32 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | import mock | 22 | 22 | import io | 23 | 23 |
| 24 | | -from gitlab import config | | 24 | +from gitlab import config, USER_AGENT | 25 | 25 | import pytest | 26 | 26 |
| 27 | 27 |
| | 28 | +custom_user_agent = "my-package/1.0.0" | | 29 | + | 28 | 30 | valid_config = u"""[global] | 29 | 31 | default = one | 30 | 32 | ssl_verify = true | | 51 | 53 | oauth_token = STUV | 52 | 54 | """ | 53 | 55 |
| | 56 | +custom_user_agent_config = """[global] | | 57 | +default = one | | 58 | +user_agent = {} | | 59 | +
| | 60 | +[one] | | 61 | +url = http://one.url | | 62 | +private_token = ABCDEF | | 63 | +""".format( | | 64 | + custom_user_agent | | 65 | +) | | 66 | + | 54 | 67 | no_default_config = u"""[global] | 55 | 68 | [there] | 56 | 69 | url = http://there.url | @@ -178,3 +191,21 @@ def test_valid_data(m_open, path_exists): | 178 | 191 | assert "STUV" == cp.oauth_token | 179 | 192 | assert 2 == cp.timeout | 180 | 193 | assert True == cp.ssl_verify | | 194 | + | | 195 | + | | 196 | +@mock.patch("os.path.exists") | | 197 | +@mock.patch("builtins.open") | | 198 | +@pytest.mark.parametrize( | | 199 | + "config_string,expected_agent", | | 200 | + [ | | 201 | + (valid_config, USER_AGENT), | | 202 | + (custom_user_agent_config, custom_user_agent), | | 203 | + ], | | 204 | +) | | 205 | +def test_config_user_agent(m_open, path_exists, config_string, expected_agent): | | 206 | + fd = io.StringIO(config_string) | | 207 | + fd.close = mock.Mock(return_value=None) | | 208 | + m_open.return_value = fd | | 209 | + | | 210 | + cp = config.GitlabConfigParser() | | 211 | + assert cp.user_agent == expected_agent |
Collapse file Copy file name to clipboardExpand all lines: gitlab/tests/test_gitlab.py +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 18 | 18 |
| 19 | 19 | import pickle | 20 | 20 |
| | 21 | +import pytest | 21 | 22 | from httmock import HTTMock, response, urlmatch, with_httmock # noqa | 22 | 23 |
| 23 | | -from gitlab import Gitlab, GitlabList | | 24 | +from gitlab import Gitlab, GitlabList, USER_AGENT | 24 | 25 | from gitlab.v4.objects import CurrentUser | 25 | 26 |
| 26 | 27 |
| @@ -139,3 +140,15 @@ class MyGitlab(Gitlab): | 139 | 140 | config_path = default_config | 140 | 141 | gl = MyGitlab.from_config("one", [config_path]) | 141 | 142 | assert isinstance(gl, MyGitlab) | | 143 | + | | 144 | + | | 145 | +@pytest.mark.parametrize( | | 146 | + "kwargs,expected_agent", | | 147 | + [ | | 148 | + ({}, USER_AGENT), | | 149 | + ({"user_agent": "my-package/1.0.0"}, "my-package/1.0.0"), | | 150 | + ], | | 151 | +) | | 152 | +def test_gitlab_user_agent(kwargs, expected_agent): | | 153 | + gl = Gitlab("http://localhost", **kwargs) | | 154 | + assert gl.headers["User-Agent"] == expected_agent |
Collapse file Copy file name to clipboardExpand all lines: setup.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 |
| 7 | 7 |
| 8 | 8 | def get_version(): | 9 | | - with open("gitlab/__init__.py") as f: | | 9 | + with open("gitlab/__version__.py") as f: | 10 | 10 | for line in f: | 11 | 11 | if line.startswith("__version__"): | 12 | 12 | return eval(line.split("=")[-1]) | |
|