Skip to content

Commit

Permalink
Setup and run Scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
lolgab committed Jun 30, 2022
1 parent 1b9df51 commit 78eba1c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 29 deletions.
1 change: 1 addition & 0 deletions .scalafmt.conf
@@ -1 +1,2 @@
version = "3.5.8"
runner.dialect = "scala213source3"
11 changes: 7 additions & 4 deletions httpclient/src/com/github/lolgab/httpclient/Request.scala
Expand Up @@ -24,9 +24,10 @@ class Request private (handle: Ptr[Byte]) {
import CurlImpl._
import CApi._
import CApiOps._
private [httpclient] var callback: Response => Unit = null
private [httpclient] val memory: Ptr[Memory] = malloc(sizeof[Memory]).asInstanceOf[Ptr[Memory]]
private [httpclient] var headersList: Ptr[CurlSList] = null
private[httpclient] var callback: Response => Unit = null
private[httpclient] val memory: Ptr[Memory] =
malloc(sizeof[Memory]).asInstanceOf[Ptr[Memory]]
private[httpclient] var headersList: Ptr[CurlSList] = null
memory._1 = malloc(0.toULong)
memory._2 = 0.toULong
curl_easy_setopt(
Expand All @@ -49,7 +50,9 @@ class Request private (handle: Ptr[Byte]) {
this
}
def url(value: String): Request = {
Zone { implicit z => curl_easy_setopt(handle, CURLOPT_URL, toCString(value)) }
Zone { implicit z =>
curl_easy_setopt(handle, CURLOPT_URL, toCString(value))
}
this
}
def body(value: String): Request = Zone { implicit z =>
Expand Down
Expand Up @@ -3,13 +3,14 @@ package com.github.lolgab.httpclient.internal
import scala.scalanative.unsafe._

@link("curl")
@extern private [httpclient] object CApi {
@extern private[httpclient] object CApi {
type CurlBuffer = CStruct2[CString, CSize]
type CurlOption = Int
type CurlRequest = CStruct4[Ptr[Byte], Long, Long, Int]
type CurlMessage = CStruct3[Int, Ptr[Byte], Ptr[Byte]]

type CurlDataCallback = CFuncPtr4[Ptr[Byte], CSize, CSize, Ptr[CurlBuffer], CSize]
type CurlDataCallback =
CFuncPtr4[Ptr[Byte], CSize, CSize, Ptr[CurlBuffer], CSize]
type CurlSocketCallback =
CFuncPtr5[Ptr[Byte], CInt, CInt, Ptr[Byte], Ptr[Byte], CInt]
type CurlTimerCallback = CFuncPtr3[Ptr[Byte], Long, Ptr[Byte], CInt]
Expand Down
Expand Up @@ -20,7 +20,7 @@ import scala.scalanative.loop.LibUVConstants._
import com.github.lolgab.httpclient._
import scala.annotation.tailrec

private [httpclient] object CurlImpl {
private[httpclient] object CurlImpl {
import CApi._
import CApiOps._

Expand Down Expand Up @@ -118,16 +118,21 @@ private [httpclient] object CurlImpl {
if (timeout_ms < 0) uv_timer_stop(timerHandle)
else {
val newTimeout = if (timeout_ms == 0) 1 else timeout_ms.toLong
uv_timer_start(timerHandle, (_: Ptr[Byte]) => {
val running_handles = stackalloc[CInt]()
uv_timer_start(
timerHandle,
(_: Ptr[Byte]) => {
val running_handles = stackalloc[CInt]()
curl_multi_socket_action(
curlHandle,
CURL_SOCKET_TIMEOUT,
0,
running_handles
)
checkMultiInfo()
}, newTimeout, 0)
checkMultiInfo()
},
newTimeout,
0
)
}
0
}
Expand Down
Expand Up @@ -11,7 +11,11 @@ private[httpclient] object HandleUtils {

@inline def getData[T <: Object](handle: Ptr[Byte]): T = {
val ptrOfPtr = stackalloc[Ptr[Byte]]()
curl_easy_getinfo(handle, CURLINFO_PRIVATE, ptrOfPtr.asInstanceOf[Ptr[Byte]])
curl_easy_getinfo(
handle,
CURLINFO_PRIVATE,
ptrOfPtr.asInstanceOf[Ptr[Byte]]
)
val dataPtr = !ptrOfPtr
if (dataPtr == null) null.asInstanceOf[T]
else {
Expand Down
47 changes: 30 additions & 17 deletions httpclient/test/src/HttpClientTests.scala
Expand Up @@ -6,33 +6,46 @@ import scala.concurrent.ExecutionContext.Implicits.global

object HttpClientTests extends TestSuite {
val tests = Tests {
test("get request") {
Request().method(Method.GET).url("http://httpbin.org/get").future().map { response =>
response.code ==> 200
assert(response.body.contains(""""url": "http://httpbin.org/get""""))
test("get request") {
Request().method(Method.GET).url("http://httpbin.org/get").future().map {
response =>
response.code ==> 200
assert(response.body.contains(""""url": "http://httpbin.org/get""""))
}
}
test("header") {
Request().url("http://httpbin.org/get").header("Foo: bar").future().map { response =>
assert(response.body.contains(""""Foo": "bar""""))
test("header") {
Request().url("http://httpbin.org/get").header("Foo: bar").future().map {
response =>
assert(response.body.contains(""""Foo": "bar""""))
}
}
test("post request") {
Request().method(Method.POST).url("http://httpbin.org/post").future().map { response =>
response.code ==> 200
assert(response.body.contains(""""url": "http://httpbin.org/post""""))
}
Request()
.method(Method.POST)
.url("http://httpbin.org/post")
.future()
.map { response =>
response.code ==> 200
assert(response.body.contains(""""url": "http://httpbin.org/post""""))
}
}
test("post body") {
val text = "Some text to send in the body"
Request().method(Method.POST).url("http://httpbin.org/post").header("Content-Type: text/plain").body(text).future().map { response =>
assert(response.body.contains(s""""data": "$text""""))
}
Request()
.method(Method.POST)
.url("http://httpbin.org/post")
.header("Content-Type: text/plain")
.body(text)
.future()
.map { response =>
assert(response.body.contains(s""""data": "$text""""))
}
}
test("put request") {
Request().method(Method.PUT).url("http://httpbin.org/put").future().map { response =>
response.code ==> 200
assert(response.body.contains(""""url": "http://httpbin.org/put""""))
Request().method(Method.PUT).url("http://httpbin.org/put").future().map {
response =>
response.code ==> 200
assert(response.body.contains(""""url": "http://httpbin.org/put""""))
}
}
}
Expand Down

0 comments on commit 78eba1c

Please sign in to comment.