Skip to content

Commit

Permalink
Renamed scommons-api-dom module to scommons-api-xhr, fixed #2
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-podzigun committed May 25, 2020
1 parent e1a34a7 commit d9c902b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ libraryDependencies ++= Seq(
"org.scommons.api" %%% "scommons-api-joda-time" % scommonsApiVer,

// client/js only
"org.scommons.api" %%% "scommons-api-dom" % scommonsApiVer,
"org.scommons.api" %%% "scommons-api-xhr" % scommonsApiVer,

// server/jvm only
"org.scommons.api" %% "scommons-api-play-ws" % scommonsApiVer
Expand Down
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ lazy val `scommons-api` = (project in file("."))
`scommons-api-joda-time-jvm`,
`scommons-api-joda-time-js`,
`scommons-api-play-ws`,
`scommons-api-dom`
`scommons-api-xhr`
)

lazy val `scommons-api-core-jvm` = ApiCore.jvm
Expand All @@ -32,4 +32,4 @@ lazy val `scommons-api-admin-js` = ApiAdmin.js
lazy val `scommons-api-joda-time-jvm` = ApiJodaTime.jvm
lazy val `scommons-api-joda-time-js` = ApiJodaTime.js
lazy val `scommons-api-play-ws` = ApiPlayWs.definition
lazy val `scommons-api-dom` = ApiDom.definition
lazy val `scommons-api-xhr` = ApiXhr.definition
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import sbt.Keys._
import sbt._
import scoverage.ScoverageKeys._

object ApiDom extends ApiModule {
object ApiXhr extends ApiModule {

override val id: String = "scommons-api-dom"
override val id: String = "scommons-api-xhr"

override val base: File = file("dom")
override val base: File = file("xhr")

override def definition: Project = {
super.definition
.enablePlugins(ScalaJSPlugin)
.settings(
description := "Common Scala ApiHttpClient implementation using JavaScript XMLHttpRequest",
description := "Scala Commons ApiHttpClient implementation using JavaScript XMLHttpRequest",

// disable scoverage, until the following issue is fixed:
// https://github.com/scoverage/scalac-scoverage-plugin/issues/196
coverageEnabled := false,
coverageExcludedPackages := "scommons.api.http.dom.raw",
coverageExcludedPackages := "scommons.api.http.xhr.raw",

//Opt-in @ScalaJSDefined by default
scalacOptions += "-P:scalajs:sjsDefinedByDefault"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scommons.api.http.dom
package scommons.api.http.xhr

import scommons.api.http.ApiHttpData._
import scommons.api.http.dom.DomApiHttpClient._
import scommons.api.http.xhr.XhrApiHttpClient._
import scommons.api.http.{ApiHttpClient, ApiHttpData, ApiHttpResponse}

import scala.concurrent.duration._
Expand All @@ -10,10 +10,10 @@ import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
import scala.scalajs.js
import scala.scalajs.js.typedarray._

class DomApiHttpClient(baseUrl: String, defaultTimeout: FiniteDuration = 30.seconds)
class XhrApiHttpClient(baseUrl: String, defaultTimeout: FiniteDuration = 30.seconds)
extends ApiHttpClient(baseUrl, defaultTimeout) {

protected[dom] def execute(method: String,
protected[xhr] def execute(method: String,
targetUrl: String,
params: List[(String, String)],
headers: List[(String, String)],
Expand Down Expand Up @@ -49,7 +49,7 @@ class DomApiHttpClient(baseUrl: String, defaultTimeout: FiniteDuration = 30.seco
}
}

private[dom] def createRequest(): raw.XMLHttpRequest = new raw.XMLHttpRequest()
private[xhr] def createRequest(): raw.XMLHttpRequest = new raw.XMLHttpRequest()

private def execute(req: raw.XMLHttpRequest, body: Option[String]): Future[raw.XMLHttpRequest] = {
val promise = Promise[raw.XMLHttpRequest]()
Expand All @@ -69,7 +69,7 @@ class DomApiHttpClient(baseUrl: String, defaultTimeout: FiniteDuration = 30.seco
}
}

object DomApiHttpClient {
object XhrApiHttpClient {

private val headersLineRegex = """[\r\n]+""".r
private val headersValueRegex = """: """.r
Expand All @@ -81,15 +81,15 @@ object DomApiHttpClient {
}.toMap
}

private[dom] def getBodyAsBytes(response: js.Any): Seq[Byte] = {
private[xhr] def getBodyAsBytes(response: js.Any): Seq[Byte] = {
if (response == null || js.isUndefined(response)) Nil
else {
//TODO: handle Blob response as well
new Int8Array(response.asInstanceOf[ArrayBuffer]).toArray
}
}

private[dom] def getFullUrl(url: String, params: List[(String, String)]): String = {
private[xhr] def getFullUrl(url: String, params: List[(String, String)]): String = {

def enc(p: String) = js.URIUtils.encodeURIComponent(p)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package scommons.api.http.dom.raw
package scommons.api.http.xhr.raw

import scala.scalajs.js
import scala.scalajs.js.annotation.JSGlobal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package scommons.api.http.dom
package scommons.api.http.xhr

import org.scalamock.scalatest.AsyncMockFactory
import org.scalatest.{Assertion, AsyncFlatSpec, Inside, Matchers}
import scommons.api.http.ApiHttpData.{StringData, UrlEncodedFormData}
import scommons.api.http.dom.DomApiHttpClient._
import scommons.api.http.dom.DomApiHttpClientSpec.MockXMLHttpRequest
import scommons.api.http.xhr.XhrApiHttpClient._
import scommons.api.http.xhr.XhrApiHttpClientSpec.MockXMLHttpRequest
import scommons.api.http.{ApiHttpData, ApiHttpResponse}

import scala.concurrent.ExecutionContext
Expand All @@ -14,7 +14,7 @@ import scala.scalajs.js
import scala.scalajs.js.annotation.JSExportAll
import scala.scalajs.js.typedarray._

class DomApiHttpClientSpec extends AsyncFlatSpec
class XhrApiHttpClientSpec extends AsyncFlatSpec
with Matchers
with Inside
with AsyncMockFactory {
Expand All @@ -23,9 +23,9 @@ class DomApiHttpClientSpec extends AsyncFlatSpec

private val baseUrl = "http://test.api.client"

private class TestDomClient(req: MockXMLHttpRequest) extends DomApiHttpClient(baseUrl) {
private class TestXhrClient(req: MockXMLHttpRequest) extends XhrApiHttpClient(baseUrl) {

override private[dom] def createRequest(): raw.XMLHttpRequest = req.asInstanceOf[raw.XMLHttpRequest]
override private[xhr] def createRequest(): raw.XMLHttpRequest = req.asInstanceOf[raw.XMLHttpRequest]
}

private val params = List("p1" -> "1", "p2" -> "2")
Expand All @@ -39,7 +39,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec
val respHeaders = Map("test_header" -> Seq("test header value"))
val expectedResult = ApiHttpResponse(targetUrl, 200, respHeaders, "some resp body")
val req = stub[MockXMLHttpRequest]
val client = new TestDomClient(req)
val client = new TestXhrClient(req)

(req.open _).when(*, *).returns(())
(req.timeout_= _).when(*).returns(())
Expand Down Expand Up @@ -77,7 +77,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec
val respHeaders = Map("test_header" -> Seq("test header value"))
val expectedResult = ApiHttpResponse(targetUrl, 200, respHeaders, "some resp body")
val req = stub[MockXMLHttpRequest]
val client = new TestDomClient(req)
val client = new TestXhrClient(req)

(req.open _).when(*, *).returns(())
(req.timeout_= _).when(*).returns(())
Expand Down Expand Up @@ -115,7 +115,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec
val respHeaders = Map("test_header" -> Seq("test header value"))
val expectedResult = ApiHttpResponse(targetUrl, 200, respHeaders, "some resp body")
val req = stub[MockXMLHttpRequest]
val client = new TestDomClient(req)
val client = new TestXhrClient(req)

(req.open _).when(*, *).returns(())
(req.timeout_= _).when(*).returns(())
Expand Down Expand Up @@ -155,7 +155,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec
val respHeaders = Map("test_header" -> Seq("test header value"))
val expectedResult = ApiHttpResponse(targetUrl, 200, respHeaders, "some resp body")
val req = stub[MockXMLHttpRequest]
val client = new TestDomClient(req)
val client = new TestXhrClient(req)

(req.open _).when(*, *).returns(())
(req.timeout_= _).when(*).returns(())
Expand Down Expand Up @@ -189,7 +189,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec
//given
val targetUrl = s"$baseUrl/api/get/url"
val req = stub[MockXMLHttpRequest]
val client = new TestDomClient(req)
val client = new TestXhrClient(req)

(req.open _).when(*, *).returns(())
(req.timeout_= _).when(*).returns(())
Expand Down Expand Up @@ -253,7 +253,7 @@ class DomApiHttpClientSpec extends AsyncFlatSpec
}
}

object DomApiHttpClientSpec {
object XhrApiHttpClientSpec {

@JSExportAll
trait MockXMLHttpRequest {
Expand Down

0 comments on commit d9c902b

Please sign in to comment.