-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4abbc0e
commit 8d6bc83
Showing
2 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
create or replace function net.worker_restart() returns bool as $$ | ||
select pg_reload_conf(); | ||
select pg_terminate_backend(pid) | ||
from pg_stat_activity | ||
where backend_type ilike '%pg_net%'; | ||
$$ | ||
security definer | ||
language sql; | ||
|
||
create or replace function net.http_get( | ||
url text, | ||
params jsonb default '{}'::jsonb, | ||
headers jsonb default '{}'::jsonb, | ||
timeout_milliseconds int default 5000 | ||
) | ||
returns bigint | ||
strict | ||
volatile | ||
parallel safe | ||
language plpgsql | ||
as $$ | ||
declare | ||
request_id bigint; | ||
params_array text[]; | ||
begin | ||
select coalesce(array_agg(net._urlencode_string(key) || '=' || net._urlencode_string(value)), '{}') | ||
into params_array | ||
from jsonb_each_text(params); | ||
|
||
insert into net.http_request_queue(method, url, headers, timeout_milliseconds) | ||
values ( | ||
'GET', | ||
net._encode_url_with_params_array(url, params_array), | ||
headers, | ||
timeout_milliseconds | ||
) | ||
returning id | ||
into request_id; | ||
|
||
return request_id; | ||
end | ||
$$; | ||
|
||
create or replace function net.http_post( | ||
url text, | ||
body jsonb default '{}'::jsonb, | ||
params jsonb default '{}'::jsonb, | ||
headers jsonb default '{"Content-Type": "application/json"}'::jsonb, | ||
timeout_milliseconds int DEFAULT 5000 | ||
) | ||
returns bigint | ||
volatile | ||
parallel safe | ||
language plpgsql | ||
as $$ | ||
declare | ||
request_id bigint; | ||
params_array text[]; | ||
content_type text; | ||
begin | ||
|
||
select | ||
header_value into content_type | ||
from | ||
jsonb_each_text(coalesce(headers, '{}'::jsonb)) r(header_name, header_value) | ||
where | ||
lower(header_name) = 'content-type' | ||
limit | ||
1; | ||
|
||
if content_type is null then | ||
select headers || '{"Content-Type": "application/json"}'::jsonb into headers; | ||
end if; | ||
|
||
if content_type <> 'application/json' then | ||
raise exception 'Content-Type header must be "application/json"'; | ||
end if; | ||
|
||
select | ||
coalesce(array_agg(net._urlencode_string(key) || '=' || net._urlencode_string(value)), '{}') | ||
into | ||
params_array | ||
from | ||
jsonb_each_text(params); | ||
|
||
insert into net.http_request_queue(method, url, headers, body, timeout_milliseconds) | ||
values ( | ||
'POST', | ||
net._encode_url_with_params_array(url, params_array), | ||
headers, | ||
convert_to(body::text, 'UTF8'), | ||
timeout_milliseconds | ||
) | ||
returning id | ||
into request_id; | ||
|
||
return request_id; | ||
end | ||
$$; | ||
|
||
create or replace function net.http_delete( | ||
url text, | ||
params jsonb default '{}'::jsonb, | ||
headers jsonb default '{}'::jsonb, | ||
timeout_milliseconds int default 5000 | ||
) | ||
returns bigint | ||
strict | ||
volatile | ||
parallel safe | ||
language plpgsql | ||
as $$ | ||
declare | ||
request_id bigint; | ||
params_array text[]; | ||
begin | ||
select coalesce(array_agg(net._urlencode_string(key) || '=' || net._urlencode_string(value)), '{}') | ||
into params_array | ||
from jsonb_each_text(params); | ||
|
||
insert into net.http_request_queue(method, url, headers, timeout_milliseconds) | ||
values ( | ||
'DELETE', | ||
net._encode_url_with_params_array(url, params_array), | ||
headers, | ||
timeout_milliseconds | ||
) | ||
returning id | ||
into request_id; | ||
|
||
return request_id; | ||
end | ||
$$; |