-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpysimpleurl.py
128 lines (104 loc) · 3.31 KB
/
pysimpleurl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
"""Basic example of an HTTP client that does not depend on any external libraries.
from https://github.com/bowmanjd/pysimpleurl
"""
import json
import typing
import urllib.error
import urllib.parse
import urllib.request
from email.message import Message
class Response(typing.NamedTuple):
"""Container for HTTP response."""
body: str
headers: Message
status: int
error_count: int = 0
def json(self) -> typing.Any:
"""
Decode body's JSON.
Returns:
Pythonic representation of the JSON object
"""
try:
output = json.loads(self.body)
except json.JSONDecodeError:
output = ""
return output
def request(
url: str,
data: dict = None,
params: dict = None,
headers: dict = None,
method: str = "GET",
data_as_json: bool = True,
error_count: int = 0,
) -> Response:
"""
Perform HTTP request.
Args:
url: url to fetch
data: dict of keys/values to be encoded and submitted
params: dict of keys/values to be encoded in URL query string
headers: optional dict of request headers
method: HTTP method , such as GET or POST
data_as_json: if True, data will be JSON-encoded
error_count: optional current count of HTTP errors, to manage recursion
Raises:
URLError: if url starts with anything other than "http"
Returns:
A dict with headers, body, status code, and, if applicable, object
rendered from JSON
"""
if not url.startswith("http"):
raise urllib.error.URLError("Incorrect and possibly insecure protocol in url")
method = method.upper()
request_data = None
headers = headers or {}
data = data or {}
params = params or {}
headers = {"Accept": "application/json", **headers}
if method == "GET":
params = {**params, **data}
data = None
if params:
url += "?" + urllib.parse.urlencode(params, doseq=True, safe="/")
if data:
if data_as_json:
request_data = json.dumps(data).encode()
headers["Content-Type"] = "application/json; charset=UTF-8"
else:
request_data = urllib.parse.urlencode(data).encode()
httprequest = urllib.request.Request(
url, data=request_data, headers=headers, method=method
)
try:
with urllib.request.urlopen(httprequest) as httpresponse:
response = Response(
headers=httpresponse.headers,
status=httpresponse.status,
body=httpresponse.read().decode(
httpresponse.headers.get_content_charset("utf-8")
),
)
except urllib.error.HTTPError as e:
response = Response(
body=str(e.reason),
headers=e.headers,
status=e.code,
error_count=error_count + 1,
)
return response
def run() -> None:
"""Run as script."""
url = "https://jsonplaceholder.typicode.com/posts"
data = {
"title": "Sample post 1",
"body": "Lorem ipsum dolor sit amet",
"userId": 22,
}
response = request(url, data=data, method="post")
json_response = response.json()
print(json.dumps(json_response, indent=2))
if __name__ == "__main__":
run()