Skip to content

Commit 34f9790

Browse files
authored
Merge pull request #170 from AzureAD/http_interface
Defines a minimalistic http interface. Earlier today, Abhi and I had a line-by-line discussion on this already.
2 parents dc52fc5 + d1a0a88 commit 34f9790

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

oauth2cli/http.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""This module documents the minimal http behaviors used by this package.
2+
3+
Its interface is influenced by, and similar to a subset of some popular,
4+
real-world http libraries, such as requests, aiohttp and httpx.
5+
"""
6+
7+
8+
class HttpClient(object):
9+
"""This describes a minimal http request interface used by this package."""
10+
11+
def post(self, url, params=None, data=None, headers=None, **kwargs):
12+
"""HTTP post.
13+
14+
params, data and headers MUST accept a dictionary.
15+
It returns an :class:`~Response`-like object.
16+
17+
Note: In its async counterpart, this method would be defined as async.
18+
"""
19+
return Response()
20+
21+
def get(self, url, params=None, headers=None, **kwargs):
22+
"""HTTP get.
23+
24+
params, data and headers MUST accept a dictionary.
25+
It returns an :class:`~Response`-like object.
26+
27+
Note: In its async counterpart, this method would be defined as async.
28+
"""
29+
return Response()
30+
31+
32+
class Response(object):
33+
"""This describes a minimal http response interface used by this package.
34+
35+
:var int status_code:
36+
The status code of this http response.
37+
38+
Our async code path would also accept an alias as "status".
39+
40+
:var string text:
41+
The body of this http response.
42+
43+
Our async code path would also accept an awaitable with the same name.
44+
"""
45+
status_code = 200 # Our async code path would also accept a name as "status"
46+
47+
text = "body as a string" # Our async code path would also accept an awaitable
48+
# We could define a json() method instead of a text property/method,
49+
# but a `text` would be more generic,
50+
# when downstream packages would potentially access some XML endpoints.
51+
52+
def raise_for_status(self):
53+
"""Raise an exception when http response status contains error"""
54+
raise NotImplementedError("Your implementation should provide this")
55+
56+
57+
def _get_status_code(resp):
58+
# RFC defines and some libraries use "status_code", others use "status"
59+
return getattr(resp, "status_code", None) or resp.status
60+

0 commit comments

Comments
 (0)