Skip to content
This repository has been archived by the owner on Sep 12, 2021. It is now read-only.

[WIP] Add implementation for RequestBuilder and Response #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ object Build extends Build {
base = file("silhouette-akka-http")
)

val silhouetteAkkaHttpClient = Project(
id = "silhouette-akka-http-client",
base = file("silhouette-akka-http-client"),
dependencies = Seq(silhouetteAkkaHttp)
)

val root = Project(
id = "root",
base = file("."),
aggregate = Seq(
silhouetteAkkaHttp
silhouetteAkkaHttp,
silhouetteAkkaHttpClient
),
settings = Defaults.coreDefaultSettings ++
APIDoc.settings ++
Expand Down
24 changes: 24 additions & 0 deletions silhouette-akka-http-client/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Licensed to the Minutemen Group under one or more contributor license
* agreements. See the COPYRIGHT file distributed with this work for
* additional information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Dependencies._

libraryDependencies ++= Seq(
Library.Specs2.core % "test"
)

enablePlugins(Doc)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Licensed to the Minutemen Group under one or more contributor license
* agreements. See the COPYRIGHT file distributed with this work for
* additional information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package silhouette.akka.http.client

import akka.actor.ActorSystem
import akka.stream.Materializer
import silhouette.http.client.HttpClient

class AkkaHttpClient()(implicit system: ActorSystem, fm: Materializer) extends HttpClient[AkkaHttpRequestBuilder] {

override def requestBuilder: AkkaHttpRequestBuilder = AkkaHttpRequestBuilder()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Licensed to the Minutemen Group under one or more contributor license
* agreements. See the COPYRIGHT file distributed with this work for
* additional information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package silhouette.akka.http.client

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.stream.Materializer
import silhouette.akka.http.AkkaHttpRequestPipeline
import silhouette.http.client.{ Body, RequestBuilder, Response, ContentType => SContentType }
import scala.concurrent.duration._
import scala.concurrent.{ ExecutionContext, Future }
import scala.io.Codec

/**
* The request implementation based on the `akka.http.scaladsl.model.HttpRequest`.
*
* @param request The request this pipeline handles.
*/
case class AkkaHttpRequestBuilder(
request: HttpRequest = HttpRequest()
)(implicit system: ActorSystem, fm: Materializer) extends RequestBuilder {

override type Self = AkkaHttpRequestBuilder

implicit val ec: ExecutionContext = system.dispatcher

private val akkaHttpRequestPipeline = AkkaHttpRequestPipeline(request, sessionName = "session")

override def withUrl(url: String): Self = {
copy(request = akkaHttpRequestPipeline.request.withUri(url))
}

override def withMethod(method: String): Self = {
copy(request = akkaHttpRequestPipeline.request.withMethod(HttpMethods.getForKey(method).get))
}

override def withHeaders(headers: (String, String)*): Self = {
copy(request = akkaHttpRequestPipeline.withHeaders(headers: _*).request)
}

override def withQueryParams(params: (String, String)*): Self = {
copy(request = akkaHttpRequestPipeline.withQueryParams(params: _*).request)
}

override def withBody(body: Body): Self = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akkie We should change Body visibility I guess

[error] /Users/merle/works/scala/silhouette-akka-http/silhouette-akka-http-client/src/main/scala/silhouette/akka/http/client/AkkaHttpRequestBuilder.scala:61: method withBody in class AkkaHttpRequestBuilder references private[silhouette] class Body.
[error] Classes which cannot access Body may be unable to override withBody.
[error]   override def withBody(body: Body): Self = {
[error]                ^
[error] one error found

import scala.collection.JavaConverters._
val mediaType = MediaType.parse(body.contentType.value).getOrElse(MediaTypes.`application/json`)
val charset = HttpCharset(body.codec.charSet.name())(body.codec.charSet.aliases().asScala.toList)
val contentType = ContentType(mediaType, () => charset)
val entity = HttpEntity(contentType, body.data)
copy(request = akkaHttpRequestPipeline.request.withEntity(entity))
}

override def execute: Future[Response] = {
Http().singleRequest(request).flatMap { response =>
// TODO: get timeout from configuration
response.entity.toStrict(10.seconds).map { entity =>
val contentType = SContentType(entity.contentType.value)
val codec = entity.contentType.charsetOption.map(c => Codec(c.value)).getOrElse(Body.DefaultCodec)
AkkaHttpResponse(response, Body(contentType, codec, entity.data.toArray))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Licensed to the Minutemen Group under one or more contributor license
* agreements. See the COPYRIGHT file distributed with this work for
* additional information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package silhouette.akka.http.client

import akka.http.scaladsl.model.HttpResponse
import silhouette.akka.http.AkkaHttpResponsePipeline
import silhouette.http.client.{ Body, Response }

/**
* The response implementation based on the [[akka.http.scaladsl.model.HttpResponse]].
*
* @param response The response this pipeline handles.
*/
case class AkkaHttpResponse(response: HttpResponse, body: Body) extends Response {

val akkaHttpResponsePipeline = AkkaHttpResponsePipeline(response, sessionName = "session")

override def header: Map[String, Seq[String]] = akkaHttpResponsePipeline.headers

override def status: Int = akkaHttpResponsePipeline.response.status.intValue()

}
Loading