This repository has been archived by the owner on Sep 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
[WIP] Add implementation for RequestBuilder and Response #8
Open
datalek
wants to merge
1
commit into
minutemen:master
Choose a base branch
from
datalek:http-client-impl
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
28 changes: 28 additions & 0 deletions
28
silhouette-akka-http-client/src/main/scala/silhouette/akka/http/client/AkkaHttpClient.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
} |
80 changes: 80 additions & 0 deletions
80
...-akka-http-client/src/main/scala/silhouette/akka/http/client/AkkaHttpRequestBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = { | ||
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)) | ||
} | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...ouette-akka-http-client/src/main/scala/silhouette/akka/http/client/AkkaHttpResponse.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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