Skip to content

Commit 79d761b

Browse files
authored
airframe-http: #1355 Add Http.clientFor(serverAddress) (#1436)
1 parent 8d9a77f commit 79d761b

2 files changed

Lines changed: 38 additions & 10 deletions

File tree

airframe-http/.jvm/src/test/scala/wvlet/airframe/http/client/URLConnectionClientTest.scala

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* limitations under the License.
1313
*/
1414
package wvlet.airframe.http.client
15+
1516
import wvlet.airframe.Design
1617
import wvlet.airframe.codec.MessageCodec
1718
import wvlet.airframe.control.Retry.MaxRetryException
@@ -23,15 +24,22 @@ import wvlet.airspec.AirSpec
2324
/**
2425
*/
2526
object URLConnectionClientTest extends AirSpec {
26-
override protected def design: Design =
27+
28+
// Use a public REST test server
29+
private val PUBLIC_REST_SERVICE = "https://httpbin.org/"
30+
31+
override protected def design: Design = {
2732
Design.newDesign
28-
.bind[SyncClient].toInstance(
33+
.bind[SyncClient]
34+
.toInstance(
2935
Http.client
3036
.withRetryContext(_.withMaxRetry(1))
31-
.newSyncClient("https://httpbin.org/") // Using a public REST test server
37+
.newSyncClient(PUBLIC_REST_SERVICE)
3238
)
39+
}
3340

3441
case class Person(id: Int, name: String)
42+
3543
val p = Person(1, "leo")
3644
val pJson = MessageCodec.of[Person].toJson(p)
3745

@@ -45,6 +53,13 @@ object URLConnectionClientTest extends AirSpec {
4553
m("json") shouldBe Map("id" -> 1, "name" -> "leo")
4654
}
4755

56+
test("create a new sync client") {
57+
val m = Http
58+
.clientFor(PUBLIC_REST_SERVICE)
59+
.getOps[Person, Map[String, Any]]("/get", p)
60+
m("args") shouldBe Map("id" -> "1", "name" -> "leo")
61+
}
62+
4863
test("sync client") { client: SyncClient =>
4964
test("Read content with 200") {
5065
val resp = client.sendSafe(Http.GET("/get"))
@@ -54,7 +69,8 @@ object URLConnectionClientTest extends AirSpec {
5469
}
5570

5671
test("user-agent") {
57-
val resp = client.get[Map[String, String]]("/user-agent", _.withUserAgent("airframe-http"))
72+
val resp =
73+
client.get[Map[String, String]]("/user-agent", _.withUserAgent("airframe-http"))
5874
resp.get("user-agent") shouldBe Some("airframe-http")
5975
}
6076

airframe-http/src/main/scala/wvlet/airframe/http/Http.scala

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@ object Http {
2929
*/
3030
def client: HttpClientConfig = HttpClientConfig()
3131

32+
/**
33+
* Create the default HTTP sync client for the target server address
34+
*/
35+
def clientFor(serverAddress: String): SyncClient =
36+
client.newSyncClient(serverAddress)
37+
3238
/**
3339
* Create a new request
3440
*/
35-
def request(method: String, uri: String) = HttpMessage.Request.empty.withMethod(method).withUri(uri)
41+
def request(method: String, uri: String) =
42+
HttpMessage.Request.empty.withMethod(method).withUri(uri)
3643

3744
/**
3845
* Create a new request
@@ -101,7 +108,9 @@ object Http {
101108
macro HttpMacros.newServerExceptionWithCodecFactory[A]
102109

103110
private[http] def parseAcceptHeader(value: Option[String]): Seq[String] = {
104-
value.map(_.split(",").map(_.trim).filter(_.nonEmpty).toSeq).getOrElse(Seq.empty)
111+
value
112+
.map(_.split(",").map(_.trim).filter(_.nonEmpty).toSeq)
113+
.getOrElse(Seq.empty)
105114
}
106115
}
107116

@@ -121,8 +130,10 @@ trait HttpRequest[Req] {
121130
def contentType: Option[String] = adapter.contentTypeOf(toRaw)
122131
def contentBytes: Array[Byte] = adapter.contentBytesOf(toRaw)
123132
def contentString: String = adapter.contentStringOf(toRaw)
124-
def accept: Seq[String] = Http.parseAcceptHeader(header.get(HttpHeader.Accept))
125-
def acceptsMsgPack: Boolean = accept.contains(HttpHeader.MediaType.ApplicationMsgPack)
133+
def accept: Seq[String] =
134+
Http.parseAcceptHeader(header.get(HttpHeader.Accept))
135+
def acceptsMsgPack: Boolean =
136+
accept.contains(HttpHeader.MediaType.ApplicationMsgPack)
126137
}
127138

128139
/**
@@ -166,8 +177,9 @@ trait HttpRequestAdapter[Req] {
166177
def queryOf(request: Req): HttpMultiMap
167178
def headerOf(request: Req): HttpMultiMap
168179
def messageOf(request: Req): HttpMessage.Message
169-
def contentStringOf(request: Req): String = messageOf(request).toContentString
170-
def contentBytesOf(request: Req): Array[Byte] = messageOf(request).toContentBytes
180+
def contentStringOf(request: Req): String = messageOf(request).toContentString
181+
def contentBytesOf(request: Req): Array[Byte] =
182+
messageOf(request).toContentBytes
171183
def contentTypeOf(request: Req): Option[String]
172184
def pathComponentsOf(request: Req): IndexedSeq[String] = {
173185
pathOf(request).replaceFirst("/", "").split("/").toIndexedSeq

0 commit comments

Comments
 (0)