Skip to content

Commit

Permalink
Allow dynamic choice of HTTP method (#94)
Browse files Browse the repository at this point in the history
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")
```
  • Loading branch information
lihaoyi authored Dec 10, 2021
1 parent a719f17 commit e983d00
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 20 deletions.
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ val r = requests.delete("http://httpbin.org/delete")
val r = requests.head("http://httpbin.org/head")

val r = requests.options("http://httpbin.org/get")

// dynamically choose what HTTP method to use
val r = requests.send("put")("http://httpbin.org/put", data = Map("key" -> "value"))

```

### Passing in Parameters
Expand Down
2 changes: 2 additions & 0 deletions requests/src/requests/Requester.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ trait BaseSession{
lazy val options = Requester("OPTIONS", this)
// unofficial
lazy val patch = Requester("PATCH", this)

def send(method: String) = Requester(method, this)
}

object BaseSession{
Expand Down
44 changes: 44 additions & 0 deletions requests/test/src-2/requests/Scala2RequestTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package requests

import utest._
import ujson._

object Scala2RequestTests extends TestSuite{
val tests = Tests{

test("params"){

test("post"){
for(chunkedUpload <- Seq(true, false)) {
val res1 = requests.post(
"https://httpbin.org/post",
data = Map("hello" -> "world", "foo" -> "baz"),
chunkedUpload = chunkedUpload
).text()
assert(read(res1).obj("form") == Obj("foo" -> "baz", "hello" -> "world"))
}
}
test("put") {
for (chunkedUpload <- Seq(true, false)) {
val res1 = requests.put(
"https://httpbin.org/put",
data = Map("hello" -> "world", "foo" -> "baz"),
chunkedUpload = chunkedUpload
).text()
assert(read(res1).obj("form") == Obj("foo" -> "baz", "hello" -> "world"))
}
}
test("send"){
requests.send("get")("https://httpbin.org/get?hello=world&foo=baz")

val res1 = requests.send("put")(
"https://httpbin.org/put",
data = Map("hello" -> "world", "foo" -> "baz"),
chunkedUpload = true
).text

assert(read(res1).obj("form") == Obj("foo" -> "baz", "hello" -> "world"))
}
}
}
}
20 changes: 0 additions & 20 deletions requests/test/src/requests/RequestTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,6 @@ object RequestTests extends TestSuite{
)
assert(read(res4).obj("args") == Obj("++-- lol" -> " !@#$%", "hello" -> "world"))
}
test("post"){
for(chunkedUpload <- Seq(true, false)) {
val res1 = requests.post(
"https://httpbin.org/post",
data = new RequestBlob.FormEncodedRequestBlob(Map("hello" -> "world", "foo" -> "baz")),
chunkedUpload = chunkedUpload
).text()
assert(read(res1).obj("form") == Obj("foo" -> "baz", "hello" -> "world"))
}
}
test("put") {
for (chunkedUpload <- Seq(true, false)) {
val res1 = requests.put(
"https://httpbin.org/put",
data = new RequestBlob.FormEncodedRequestBlob(Map("hello" -> "world", "foo" -> "baz")),
chunkedUpload = chunkedUpload
).text()
assert(read(res1).obj("form") == Obj("foo" -> "baz", "hello" -> "world"))
}
}
}
test("multipart"){
for(chunkedUpload <- Seq(true, false)) {
Expand Down

0 comments on commit e983d00

Please sign in to comment.