-
Notifications
You must be signed in to change notification settings - Fork 116
/
http--1.6.sql
131 lines (109 loc) · 3.76 KB
/
http--1.6.sql
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
129
130
131
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION http" to load this file. \quit
CREATE DOMAIN http_method AS text;
CREATE DOMAIN content_type AS text
CHECK (
VALUE ~ '^\S+\/\S+'
);
CREATE TYPE http_header AS (
field VARCHAR,
value VARCHAR
);
CREATE TYPE http_response AS (
status INTEGER,
content_type VARCHAR,
headers http_header[],
content VARCHAR
);
CREATE TYPE http_request AS (
method http_method,
uri VARCHAR,
headers http_header[],
content_type VARCHAR,
content VARCHAR
);
CREATE FUNCTION http_set_curlopt (curlopt VARCHAR, value VARCHAR)
RETURNS boolean
AS 'MODULE_PATHNAME', 'http_set_curlopt'
LANGUAGE 'c';
CREATE FUNCTION http_reset_curlopt ()
RETURNS boolean
AS 'MODULE_PATHNAME', 'http_reset_curlopt'
LANGUAGE 'c';
CREATE FUNCTION http_list_curlopt ()
RETURNS TABLE(curlopt text, value text)
AS 'MODULE_PATHNAME', 'http_list_curlopt'
LANGUAGE 'c';
CREATE FUNCTION http_header (field VARCHAR, value VARCHAR)
RETURNS http_header
AS $$ SELECT $1, $2 $$
LANGUAGE 'sql';
CREATE FUNCTION http(request @extschema@.http_request)
RETURNS http_response
AS 'MODULE_PATHNAME', 'http_request'
LANGUAGE 'c';
CREATE FUNCTION http_get(uri VARCHAR)
RETURNS http_response
AS $$ SELECT @extschema@.http(('GET', $1, NULL, NULL, NULL)::@extschema@.http_request) $$
LANGUAGE 'sql';
CREATE FUNCTION http_post(uri VARCHAR, content VARCHAR, content_type VARCHAR)
RETURNS http_response
AS $$ SELECT @extschema@.http(('POST', $1, NULL, $3, $2)::@extschema@.http_request) $$
LANGUAGE 'sql';
CREATE FUNCTION http_put(uri VARCHAR, content VARCHAR, content_type VARCHAR)
RETURNS http_response
AS $$ SELECT @extschema@.http(('PUT', $1, NULL, $3, $2)::@extschema@.http_request) $$
LANGUAGE 'sql';
CREATE FUNCTION http_patch(uri VARCHAR, content VARCHAR, content_type VARCHAR)
RETURNS http_response
AS $$ SELECT @extschema@.http(('PATCH', $1, NULL, $3, $2)::@extschema@.http_request) $$
LANGUAGE 'sql';
CREATE FUNCTION http_delete(uri VARCHAR)
RETURNS http_response
AS $$ SELECT @extschema@.http(('DELETE', $1, NULL, NULL, NULL)::@extschema@.http_request) $$
LANGUAGE 'sql';
CREATE FUNCTION http_delete(uri VARCHAR, content VARCHAR, content_type VARCHAR)
RETURNS http_response
AS $$ SELECT @extschema@.http(('DELETE', $1, NULL, $3, $2)::@extschema@.http_request) $$
LANGUAGE 'sql';
CREATE FUNCTION http_head(uri VARCHAR)
RETURNS http_response
AS $$ SELECT @extschema@.http(('HEAD', $1, NULL, NULL, NULL)::@extschema@.http_request) $$
LANGUAGE 'sql';
CREATE FUNCTION urlencode(string VARCHAR)
RETURNS TEXT
AS 'MODULE_PATHNAME'
LANGUAGE 'c'
IMMUTABLE STRICT;
CREATE FUNCTION urlencode(string BYTEA)
RETURNS TEXT
AS 'MODULE_PATHNAME'
LANGUAGE 'c'
IMMUTABLE STRICT;
CREATE FUNCTION urlencode(data JSONB)
RETURNS TEXT
AS 'MODULE_PATHNAME', 'urlencode_jsonb'
LANGUAGE 'c'
IMMUTABLE STRICT;
CREATE FUNCTION http_get(uri VARCHAR, data JSONB)
RETURNS http_response
AS $$
SELECT @extschema@.http(('GET', $1 || '?' || @extschema@.urlencode($2), NULL, NULL, NULL)::@extschema@.http_request)
$$
LANGUAGE 'sql';
CREATE FUNCTION http_post(uri VARCHAR, data JSONB)
RETURNS http_response
AS $$
SELECT @extschema@.http(('POST', $1, NULL, 'application/x-www-form-urlencoded', @extschema@.urlencode($2))::@extschema@.http_request)
$$
LANGUAGE 'sql';
CREATE FUNCTION text_to_bytea(data TEXT)
RETURNS BYTEA
AS 'MODULE_PATHNAME', 'text_to_bytea'
LANGUAGE 'c'
IMMUTABLE STRICT;
CREATE FUNCTION bytea_to_text(data BYTEA)
RETURNS TEXT
AS 'MODULE_PATHNAME', 'bytea_to_text'
LANGUAGE 'c'
IMMUTABLE STRICT;