Skip to content

Commit

Permalink
feat(client): bootstrap the http backends concept (#2391)
Browse files Browse the repository at this point in the history
  • Loading branch information
lmilbaum committed Dec 5, 2022
1 parent faf842e commit 91a665f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
18 changes: 13 additions & 5 deletions gitlab/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import re
import time
from typing import Any, cast, Dict, List, Optional, Tuple, TYPE_CHECKING, Union
from typing import Any, cast, Dict, List, Optional, Tuple, Type, TYPE_CHECKING, Union
from urllib import parse

import requests
Expand All @@ -14,7 +14,7 @@
import gitlab.config
import gitlab.const
import gitlab.exceptions
from gitlab import utils
from gitlab import http_backends, utils

REDIRECT_MSG = (
"python-gitlab detected a {status_code} ({reason!r}) redirection. You must update "
Expand All @@ -32,6 +32,7 @@


class Gitlab:

"""Represents a GitLab server connection.
Args:
Expand All @@ -53,6 +54,10 @@ class Gitlab:
or 52x responses. Defaults to False.
keep_base_url: keep user-provided base URL for pagination if it
differs from response headers
Keyward Args:
requests.Session session: Http Requests Session
RequestsBackend http_backend: Backend that will be used to make http requests
"""

def __init__(
Expand All @@ -66,15 +71,14 @@ def __init__(
http_password: Optional[str] = None,
timeout: Optional[float] = None,
api_version: str = "4",
session: Optional[requests.Session] = None,
per_page: Optional[int] = None,
pagination: Optional[str] = None,
order_by: Optional[str] = None,
user_agent: str = gitlab.const.USER_AGENT,
retry_transient_errors: bool = False,
keep_base_url: bool = False,
**kwargs: Any,
) -> None:

self._api_version = str(api_version)
self._server_version: Optional[str] = None
self._server_revision: Optional[str] = None
Expand All @@ -98,7 +102,11 @@ def __init__(
self._set_auth_info()

#: Create a session object for requests
self.session = session or requests.Session()
http_backend: Type[http_backends.DefaultBackend] = kwargs.pop(
"http_backend", http_backends.DefaultBackend
)
self.http_backend = http_backend(**kwargs)
self.session = self.http_backend.client

self.per_page = per_page
self.pagination = pagination
Expand Down
7 changes: 7 additions & 0 deletions gitlab/http_backends/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
Defines http backends for processing http requests
"""

from .requests_backend import RequestsBackend

DefaultBackend = RequestsBackend
12 changes: 12 additions & 0 deletions gitlab/http_backends/requests_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from typing import Optional

import requests


class RequestsBackend:
def __init__(self, session: Optional[requests.Session] = None) -> None:
self._client: requests.Session = session or requests.Session()

@property
def client(self) -> requests.Session:
return self._client

0 comments on commit 91a665f

Please sign in to comment.