Skip to content

Commit 0fde7ca

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

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
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/http_backends/requests_backend.py

Lines changed: 5 additions & 2 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

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