Skip to content

Commit a34c5c0

Browse files
author
Liora Milbaum
committed
feat: PEP 544 – Protocols: Structural subtyping (static duck typing)
1 parent 3c7c7fc commit a34c5c0

File tree

6 files changed

+75
-7
lines changed

6 files changed

+75
-7
lines changed

.github/workflows/protocol.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: 'Protocaol'
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- gitlab/protocols/**
9+
pull_request:
10+
branches:
11+
- main
12+
paths:
13+
- gitlab/protocols/**
14+
15+
jobs:
16+
protocol:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Fail
20+
run: /bin/false

gitlab/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,10 +746,10 @@ def http_request(
746746

747747
raise
748748

749-
self._check_redirects(result.response)
749+
self._check_redirects(result)
750750

751751
if 200 <= result.status_code < 300:
752-
return result.response
752+
return result
753753

754754
def should_retry() -> bool:
755755
if result.status_code == 429 and obey_rate_limit:

gitlab/http_backends/requests_backend.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
from requests.structures import CaseInsensitiveDict
77
from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore
88

9+
from gitlab.protocols.http_backend import HttpBackend
10+
from gitlab.protocols.http_backend_response import HttpBackendResponse
911

10-
class RequestsResponse:
12+
13+
class RequestsResponse(HttpBackendResponse):
1114
def __init__(self, response: requests.Response) -> None:
1215
self._response: requests.Response = response
1316

@@ -35,7 +38,7 @@ def json(self) -> Any:
3538
return self._response.json()
3639

3740

38-
class RequestsBackend:
41+
class RequestsBackend(HttpBackend):
3942
def __init__(self, session: Optional[requests.Session] = None) -> None:
4043
self._client: requests.Session = session or requests.Session()
4144

@@ -86,7 +89,7 @@ def http_request(
8689
verify: Optional[Union[bool, str]] = True,
8790
stream: Optional[bool] = False,
8891
**kwargs: Any,
89-
) -> RequestsResponse:
92+
) -> requests.Response:
9093
"""Make HTTP request
9194
9295
Args:
@@ -103,7 +106,7 @@ def http_request(
103106
Returns:
104107
A requests Response object.
105108
"""
106-
response: requests.Response = self._client.request(
109+
return self._client.request(
107110
method=method,
108111
url=url,
109112
params=params,
@@ -114,4 +117,3 @@ def http_request(
114117
json=json,
115118
**kwargs,
116119
)
117-
return RequestsResponse(response=response)

gitlab/protocols/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
Defines protocols
3+
"""

gitlab/protocols/http_backend.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sys
2+
from abc import abstractmethod
3+
from typing import Any, Dict, Optional, Union
4+
5+
import requests
6+
from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore
7+
8+
if sys.version_info >= (3, 8):
9+
from typing import Protocol
10+
else:
11+
from typing_extensions import Protocol
12+
13+
14+
class HttpBackend(Protocol):
15+
@abstractmethod
16+
def http_request(
17+
self,
18+
method: str,
19+
url: str,
20+
json: Optional[Union[Dict[str, Any], bytes]],
21+
data: Optional[Union[Dict[str, Any], MultipartEncoder]],
22+
params: Optional[Any],
23+
timeout: Optional[float],
24+
verify: Optional[Union[bool, str]],
25+
stream: Optional[bool],
26+
**kwargs: Any,
27+
) -> requests.Response:
28+
...
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import sys
2+
from abc import abstractmethod
3+
4+
import requests
5+
6+
if sys.version_info >= (3, 8):
7+
from typing import Protocol
8+
else:
9+
from typing_extensions import Protocol
10+
11+
12+
class HttpBackendResponse(Protocol):
13+
@abstractmethod
14+
def __init__(self, response: requests.Response) -> None:
15+
...

0 commit comments

Comments
 (0)