Skip to content

Commit e983d00

Browse files
authored
Allow dynamic choice of HTTP method (#94)
Fixes #67 Fixes #52 This was always possible in a roundabout way via ```scala requests.Requester("get", requests.Session()).apply("https://www.google.com") ``` But this PR adds a convenient alias and documents it for discoverability ```scala requests.send("get")("https://www.google.com") ```
1 parent a719f17 commit e983d00

File tree

4 files changed

+50
-20
lines changed

4 files changed

+50
-20
lines changed

readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ val r = requests.delete("http://httpbin.org/delete")
8181
val r = requests.head("http://httpbin.org/head")
8282

8383
val r = requests.options("http://httpbin.org/get")
84+
85+
// dynamically choose what HTTP method to use
86+
val r = requests.send("put")("http://httpbin.org/put", data = Map("key" -> "value"))
87+
8488
```
8589

8690
### Passing in Parameters

requests/src/requests/Requester.scala

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ trait BaseSession{
3434
lazy val options = Requester("OPTIONS", this)
3535
// unofficial
3636
lazy val patch = Requester("PATCH", this)
37+
38+
def send(method: String) = Requester(method, this)
3739
}
3840

3941
object BaseSession{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package requests
2+
3+
import utest._
4+
import ujson._
5+
6+
object Scala2RequestTests extends TestSuite{
7+
val tests = Tests{
8+
9+
test("params"){
10+
11+
test("post"){
12+
for(chunkedUpload <- Seq(true, false)) {
13+
val res1 = requests.post(
14+
"https://httpbin.org/post",
15+
data = Map("hello" -> "world", "foo" -> "baz"),
16+
chunkedUpload = chunkedUpload
17+
).text()
18+
assert(read(res1).obj("form") == Obj("foo" -> "baz", "hello" -> "world"))
19+
}
20+
}
21+
test("put") {
22+
for (chunkedUpload <- Seq(true, false)) {
23+
val res1 = requests.put(
24+
"https://httpbin.org/put",
25+
data = Map("hello" -> "world", "foo" -> "baz"),
26+
chunkedUpload = chunkedUpload
27+
).text()
28+
assert(read(res1).obj("form") == Obj("foo" -> "baz", "hello" -> "world"))
29+
}
30+
}
31+
test("send"){
32+
requests.send("get")("https://httpbin.org/get?hello=world&foo=baz")
33+
34+
val res1 = requests.send("put")(
35+
"https://httpbin.org/put",
36+
data = Map("hello" -> "world", "foo" -> "baz"),
37+
chunkedUpload = true
38+
).text
39+
40+
assert(read(res1).obj("form") == Obj("foo" -> "baz", "hello" -> "world"))
41+
}
42+
}
43+
}
44+
}

requests/test/src/requests/RequestTests.scala

-20
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,6 @@ object RequestTests extends TestSuite{
5757
)
5858
assert(read(res4).obj("args") == Obj("++-- lol" -> " !@#$%", "hello" -> "world"))
5959
}
60-
test("post"){
61-
for(chunkedUpload <- Seq(true, false)) {
62-
val res1 = requests.post(
63-
"https://httpbin.org/post",
64-
data = new RequestBlob.FormEncodedRequestBlob(Map("hello" -> "world", "foo" -> "baz")),
65-
chunkedUpload = chunkedUpload
66-
).text()
67-
assert(read(res1).obj("form") == Obj("foo" -> "baz", "hello" -> "world"))
68-
}
69-
}
70-
test("put") {
71-
for (chunkedUpload <- Seq(true, false)) {
72-
val res1 = requests.put(
73-
"https://httpbin.org/put",
74-
data = new RequestBlob.FormEncodedRequestBlob(Map("hello" -> "world", "foo" -> "baz")),
75-
chunkedUpload = chunkedUpload
76-
).text()
77-
assert(read(res1).obj("form") == Obj("foo" -> "baz", "hello" -> "world"))
78-
}
79-
}
8060
}
8161
test("multipart"){
8262
for(chunkedUpload <- Seq(true, false)) {

0 commit comments

Comments
 (0)