Skip to content

Commit ac95cd2

Browse files
committed
feature: shortcut calling for requests APIs
1 parent 4ab72df commit ac95cd2

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed

lib/resty/requests.lua

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,102 @@
33
local util = require "resty.requests.util"
44
local session = require "resty.requests.session"
55

6+
7+
local is_tab = util.is_tab
8+
local error = error
9+
610
local _M = { _VERSION = "0.7.2" }
711

812

13+
local function request_shortcut(method, opts)
14+
method = method or opts.method
15+
if not method then
16+
error("no specified HTTP method")
17+
end
18+
19+
local url = opts.url
20+
local s = session.new()
21+
return s:request(method, url, opts)
22+
end
23+
24+
925
local function request(method, url, opts)
26+
if not url and is_tab(method) then
27+
-- shortcut type
28+
return request_shortcut(nil, method)
29+
end
30+
1031
local s = session.new()
1132
return s:request(method, url, opts)
1233
end
1334

1435

1536
local function get(url, opts)
37+
if not opts and is_tab(url) then
38+
-- shortcut type
39+
return request_shortcut("GET", url)
40+
end
41+
1642
return request("GET", url, opts)
1743
end
1844

1945

2046
local function head(url, opts)
47+
if not opts and is_tab(url) then
48+
-- shortcut type
49+
return request_shortcut("HEAD", url)
50+
end
51+
2152
return request("HEAD", url, opts)
2253
end
2354

2455

2556
local function post(url, opts)
57+
if not opts and is_tab(url) then
58+
-- shortcut type
59+
return request_shortcut("POST", url)
60+
end
61+
2662
return request("POST", url, opts)
2763
end
2864

2965

3066
local function put(url, opts)
67+
if not opts and is_tab(url) then
68+
-- shortcut type
69+
return request_shortcut("PUT", url)
70+
end
71+
3172
return request("PUT", url, opts)
3273
end
3374

3475

3576
local function delete(url, opts)
77+
if not opts and is_tab(url) then
78+
-- shortcut type
79+
return request_shortcut("DELETE", url)
80+
end
81+
3682
return request("DELETE", url, opts)
3783
end
3884

3985

4086
local function options(url, opts)
87+
if not opts and is_tab(url) then
88+
-- shortcut type
89+
return request_shortcut("OPTIONS", url)
90+
end
91+
4192
return request("OPTIONS", url, opts)
4293
end
4394

4495

4596
local function patch(url, opts)
97+
if not opts and is_tab(url) then
98+
-- shortcut type
99+
return request_shortcut("PATCH", url)
100+
end
101+
46102
return request("PATCH", url, opts)
47103
end
48104

t/08-shortcut.t

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
use Test::Nginx::Socket::Lua;
2+
use Cwd qw(cwd);
3+
4+
my $pwd = cwd();
5+
6+
repeat_each(1);
7+
plan tests => repeat_each() * (blocks() * 3);
8+
9+
our $http_config = << 'EOC';
10+
lua_package_path "lib/?.lua;;";
11+
12+
server {
13+
listen 10088;
14+
location = /t1 {
15+
content_by_lua_block {
16+
ngx.print("10088 virtual server")
17+
}
18+
}
19+
location = /t2 {
20+
content_by_lua_block {
21+
ngx.req.read_body()
22+
ngx.print(ngx.req.get_body_data())
23+
}
24+
}
25+
}
26+
EOC
27+
28+
no_long_string();
29+
run_tests();
30+
31+
__DATA__
32+
33+
34+
=== TEST 1: shortcut GET request
35+
36+
--- http_config eval: $::http_config
37+
38+
--- config
39+
location = /t {
40+
content_by_lua_block {
41+
local requests = require "resty.requests"
42+
local url = "http://127.0.0.1:10088/t1"
43+
local r, err = requests.get { url = url, stream = false }
44+
if not r then
45+
ngx.log(ngx.ERR, err)
46+
end
47+
48+
ngx.say(r.content)
49+
}
50+
}
51+
52+
--- request
53+
GET /t
54+
55+
--- response_body
56+
10088 virtual server
57+
--- no_error_log
58+
[error]
59+
60+
61+
62+
=== TEST 2: shortcut request without specified HTTP method
63+
64+
--- http_config eval: $::http_config
65+
66+
--- config
67+
location = /t {
68+
content_by_lua_block {
69+
local requests = require "resty.requests"
70+
local url = "http://127.0.0.1:10088/t1"
71+
local ok, err = pcall(requests.request, { url = url, stream = false })
72+
ngx.say(err)
73+
}
74+
}
75+
76+
--- request
77+
GET /t
78+
79+
--- response_body_like
80+
no specified HTTP method
81+
--- no_error_log
82+
[error]
83+
84+
85+
86+
=== TEST 3: shortcut HEAD request
87+
88+
--- http_config eval: $::http_config
89+
90+
--- config
91+
location = /t {
92+
content_by_lua_block {
93+
local requests = require "resty.requests"
94+
local url = "http://127.0.0.1:10088/t1"
95+
local r, err = requests.head { url = url, stream = false }
96+
if not r then
97+
ngx.log(ngx.ERR, err)
98+
end
99+
100+
ngx.say("ok")
101+
}
102+
}
103+
104+
--- request
105+
GET /t
106+
107+
--- response_body
108+
ok
109+
--- no_error_log
110+
[error]
111+
112+
113+
114+
=== TEST 4: shortcut POST request
115+
116+
--- http_config eval: $::http_config
117+
118+
--- config
119+
location = /t {
120+
content_by_lua_block {
121+
local requests = require "resty.requests"
122+
local url = "http://127.0.0.1:10088/t2"
123+
local r, err = requests.post { url = url, stream = false, body = "hello world" }
124+
if not r then
125+
ngx.log(ngx.ERR, err)
126+
end
127+
128+
ngx.say(r.content)
129+
}
130+
}
131+
132+
--- request
133+
GET /t
134+
135+
--- response_body
136+
hello world
137+
--- no_error_log
138+
[error]

0 commit comments

Comments
 (0)