Skip to content

Commit 01d986c

Browse files
committed
fix: 0.2
1 parent f7820ad commit 01d986c

File tree

6 files changed

+76
-4
lines changed

6 files changed

+76
-4
lines changed

docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ from
153153
"Content-Length": "18",
154154
"Content-Type": "application/json",
155155
"Host": "httpbin.org",
156-
"User-Agent": "pg_net/0.1",
156+
"User-Agent": "pg_net/0.2",
157157
"X-Amzn-Trace-Id": "Root=1-61031a5c-7e1afeae69bffa8614d8e48e"
158158
},
159159
"json": {

docs/contributing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ select net.http_get('http://localhost:3000/projects');
4242
-- > GET /projects HTTP/1.1
4343
-- Host: localhost:3000
4444
-- Accept: */*
45-
-- User-Agent: pg_net/0.1
45+
-- User-Agent: pg_net/0.2
4646
--
4747
-- * Mark bundle as not supporting multiuse
4848
-- < HTTP/1.1 200 OK

pg_net.control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
comment = 'Async HTTP'
2-
default_version = '0.1'
2+
default_version = '0.2'
33
relocatable = false

sql/pg_net--0.1--0.2.sql

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
create or replace function net.http_post(
2+
-- url for the request
3+
url text,
4+
-- body of the POST request
5+
body jsonb default '{}'::jsonb,
6+
-- key/value pairs to be url encoded and appended to the `url`
7+
params jsonb default '{}'::jsonb,
8+
-- key/values to be included in request headers
9+
headers jsonb default '{"Content-Type": "application/json"}'::jsonb,
10+
-- the maximum number of milliseconds the request may take before being cancelled
11+
timeout_milliseconds int DEFAULT 1000
12+
)
13+
-- request_id reference
14+
returns bigint
15+
volatile
16+
parallel safe
17+
language plpgsql
18+
as $$
19+
declare
20+
request_id bigint;
21+
params_array text[];
22+
content_type text;
23+
begin
24+
25+
-- Exctract the content_type from headers
26+
select
27+
header_value into content_type
28+
from
29+
jsonb_each_text(coalesce(headers, '{}'::jsonb)) r(header_name, header_value)
30+
where
31+
lower(header_name) = 'content-type'
32+
limit
33+
1;
34+
35+
-- If the user provided new headers and omitted the content type
36+
-- add it back in automatically
37+
if content_type is null then
38+
select headers || '{"Content-Type": "application/json"}'::jsonb into headers;
39+
end if;
40+
41+
-- Confirm that the content-type is set as "application/json"
42+
if content_type <> 'application/json' then
43+
raise exception 'Content-Type header must be "application/json"';
44+
end if;
45+
46+
-- Confirm body is set since http method switches on if body exists
47+
if body is null then
48+
raise exception 'body must not be null';
49+
end if;
50+
51+
select
52+
coalesce(array_agg(net._urlencode_string(key) || '=' || net._urlencode_string(value)), '{}')
53+
into
54+
params_array
55+
from
56+
jsonb_each_text(params);
57+
58+
-- Add to the request queue
59+
insert into net.http_request_queue(method, url, headers, body, timeout_milliseconds)
60+
values (
61+
'POST',
62+
net._encode_url_with_params_array(url, params_array),
63+
headers,
64+
convert_to(body::text, 'UTF8'),
65+
timeout_milliseconds
66+
)
67+
returning id
68+
into request_id;
69+
70+
return request_id;
71+
end
72+
$$;
File renamed without changes.

src/worker.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ static int init(CURLM *cm, char *method, char *url, struct curl_slist *reqHeader
156156
cdata->headers = headers;
157157
}
158158

159-
reqHeaders = curl_slist_append(reqHeaders, "User-Agent: pg_net/0.1");
159+
reqHeaders = curl_slist_append(reqHeaders, "User-Agent: pg_net/0.2");
160160

161161
if (strcasecmp(method, "GET") == 0) {
162162
if (reqBody) {

0 commit comments

Comments
 (0)