generated from treeform/nimtemplate
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpuppy.nim
113 lines (98 loc) · 2.86 KB
/
puppy.nim
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
import puppy/common
when defined(nimdoc):
# Used to work around the doc generator.
proc internalFetch(req: Request): Response {.raises: [PuppyError].} =
discard
elif defined(windows) and not defined(puppyLibcurl):
# WinHTTP Windows
import puppy/platforms/win32/platform
elif defined(macosx) and not defined(puppyLibcurl):
# AppKit macOS
import puppy/platforms/macos/platform
else:
# LIBCURL Linux
import puppy/platforms/linux/platform
export common
proc addDefaultHeaders(req: Request) =
if req.headers["user-agent"] == "":
req.headers["user-agent"] = "Puppy"
if req.headers["accept-encoding"] == "":
# If there isn't a specific accept-encoding specified, enable gzip
req.headers["accept-encoding"] = "gzip"
proc fetch*(req: Request): Response {.raises: [PuppyError].} =
if req.url.scheme notin ["http", "https"]:
raise newException(
PuppyError, "Unsupported request scheme: " & req.url.scheme
)
req.addDefaultHeaders()
if req.timeout == 0:
req.timeout = 60
return internalFetch(req)
proc newRequest*(
url: string,
verb = "get",
headers = emptyHttpHeaders(),
timeout: float32 = 60
): Request =
## Allocates a new request object with defaults.
result = Request()
result.url = parseUrl(url)
result.verb = verb
result.headers = headers
result.timeout = timeout
proc get*(
url: string,
headers = emptyHttpHeaders(),
timeout: float32 = 60
): Response =
fetch(newRequest(url, "GET", headers, timeout))
proc post*(
url: string,
headers = emptyHttpHeaders(),
body: sink string = "",
timeout: float32 = 60
): Response =
let request = newRequest(url, "POST", headers, timeout)
request.body = body
fetch(request)
proc put*(
url: string,
headers = emptyHttpHeaders(),
body: sink string = "",
timeout: float32 = 60
): Response =
let request = newRequest(url, "PUT", headers, timeout)
request.body = body
fetch(request)
proc patch*(
url: string,
headers = emptyHttpHeaders(),
body: sink string = "",
timeout: float32 = 60
): Response =
let request = newRequest(url, "PATCH", headers, timeout)
request.body = body
fetch(request)
proc delete*(
url: string,
headers = emptyHttpHeaders(),
timeout: float32 = 60
): Response =
fetch(newRequest(url, "DELETE", headers, timeout))
proc head*(
url: string,
headers = emptyHttpHeaders(),
timeout: float32 = 60
): Response =
fetch(newRequest(url, "HEAD", headers, timeout))
proc fetch*(url: string, headers = emptyHttpHeaders()): string =
## Simple fetch that directly returns the GET response body.
## Raises an exception if anything goes wrong or if the response code
## is not 200. See get, post, put etc for similar calls that return
## a response object.
let res = get(url, headers)
if res.code == 200:
return res.body
raise newException(PuppyError,
"Non 200 response code: " & $res.code & "\n" & res.body
)