Skip to content

Commit 1e422f9

Browse files
committedNov 22, 2016
better code; use non-default user-agent in example
1 parent ea4ebab commit 1e422f9

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed
 

Diff for: ‎README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ status_code | 302
1212
status_line | HTTP/1.1 302 Found
1313
headers | {"P3P": "policyref=\"/w3c/p3p.xml\", CP=\"NON DSP ADM DEV PSD IVDo OUR IND STP PHY PRE NAV UNI\"", "Date": "Sat, 29 Oct 2016 00:08:43 GMT", "Server": "nginx", "Expires": "Sat, 29 Oct 2016 00:08:44 GMT", "Location": "https://ya.ru/", "Connection": "keep-alive", "Set-Cookie": "yandexuid=182183081477699724; Expires=Tue, 27-Oct-2026 00:08:43 GMT; Domain=.ya.ru; Path=/", "Cache-Control": "no-cache,no-store,max-age=0,must-revalidate", "Last-Modified": "Sat, 29 Oct 2016 00:08:44 GMT", "Content-Length": "0"}
1414
body |
15-
body_jsonb |
1615
is_json | f
1716
```
1817

1918
Another example, showing work with REST API:
2019
```sql
2120
test=> with results as (
22-
select jsonb_array_elements(get.body_jsonb->'results') as r
23-
from http_client.get('http://pokeapi.co/api/v2/pokemon/')
21+
select jsonb_array_elements(get.body::jsonb->'results') as r
22+
from http_client.get('http://pokeapi.co/api/v2/pokemon/', '{"user-agent":"test-robot"}'::jsonb)
2423
)
2524
select r->>'url' as url, r->>'name' as pokename
2625
from results

Diff for: ‎install.sql

+15-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ create schema http_client;
33
do $$
44
begin
55
execute 'alter database '||current_database()||' set http_client.connect_timeout = 2;';
6+
execute 'alter database '||current_database()||' set http_client.default_headers = ''{}'';';
67
end;
78
$$ language plpgsql;
89

@@ -33,32 +34,32 @@ try:
3334
res['status_code'] = conn.getcode()
3435
res['url_received'] = conn.geturl()
3536
respHeaders = conn.info().dict
36-
res['headers'] = json.dumps(respHeaders)
37-
if 'content-type' in respHeaders and respHeaders['content-type'].find('application/json') >= 0:
38-
res['is_json'] = True
39-
#res['body_jsonb'] = json.loads(res['body']) # try/except
40-
else:
41-
res['is_json'] = False
4237
conn.close()
4338
except HTTPError as e:
4439
res['status_code'] = e.code
45-
res['headers'] = json.dumps(e.headers.dict) # undocumented http://stackoverflow.com/a/6402083/4677351
40+
respHeaders = e.headers.dict # undocumented http://stackoverflow.com/a/6402083/4677351
4641
res['body'] = e.read()
47-
48-
42+
res['headers'] = json.dumps(respHeaders)
43+
if 'content-type' in respHeaders and respHeaders['content-type'].find('application/json') >= 0:
44+
res['is_json'] = True
45+
else:
46+
res['is_json'] = False
4947
return res
5048
except Exception as e:
5149
msg = "Error in http_clien._get(): exception {0} occured."
5250
res = msg.format(e.__class__.__name__)
5351
return res
5452
$$ language plpython2u volatile;
5553

56-
create or replace function http_client._post(
54+
create or replace function http_client.get(query text, headers jsonb) returns http_client.response as $$
55+
select http_client._get(
56+
query,
57+
current_setting('http_client.connect_timeout')::integer,
58+
current_setting('http_client.default_headers')::jsonb || coalesce(headers, '{}'::jsonb)
59+
);
60+
$$ language sql volatile;
5761

5862
create or replace function http_client.get(query text) returns http_client.response as $$
59-
select http_client._get(query, current_setting('http_client.connect_timeout')::integer, null::jsonb);
63+
select http_client.get(query, '{}'::jsonb);
6064
$$ language sql volatile;
6165

62-
63-
64-

Diff for: ‎uninstall.sql

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
do $$
22
begin
33
execute 'alter database '||current_database()||' reset http_client.connect_timeout;';
4+
execute 'alter database '||current_database()||' reset http_client.default_headers;';
45
end;
56
$$ language plpgsql;
67

0 commit comments

Comments
 (0)
Please sign in to comment.