This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
! httpx: flexibilize RequestBuilding and ResponseTransformation by ge…
…neralizing the `~>` operator Even though we do change the public API with this it should only in rare cases lead to the compiler errors. By factoring out all the logic for the `~>` operator into a dedicated `TransformerPipelineSupport` trait + object the request building side of a pipeline now also benefits from the full flexibility the operator (as a generic function "concatenator") brings. For example, you can now chain a function `HttpRequest => Future[HttpRequest]` into the pipeline and everything will work as expected.
- Loading branch information
Showing
4 changed files
with
84 additions
and
58 deletions.
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
61 changes: 61 additions & 0 deletions
61
spray-httpx/src/main/scala/spray/httpx/TransformerPipelineSupport.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,61 @@ | ||
/* | ||
* Copyright (C) 2011-2013 spray.io | ||
* | ||
* 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 spray.httpx | ||
|
||
import scala.concurrent.{ ExecutionContext, Future } | ||
import akka.event.{ Logging, LoggingAdapter } | ||
|
||
trait TransformerPipelineSupport { | ||
|
||
def logValue[T](log: LoggingAdapter, level: Logging.LogLevel = Logging.DebugLevel): T ⇒ T = | ||
logValue { value ⇒ log.log(level, value.toString) } | ||
|
||
def logValue[T](logFun: T ⇒ Unit): T ⇒ T = { response ⇒ | ||
logFun(response) | ||
response | ||
} | ||
|
||
implicit class WithTransformation[A](value: A) { | ||
def ~>[B](f: A ⇒ B): B = f(value) | ||
} | ||
|
||
implicit class WithTransformerConcatenation[A, B](f: A ⇒ B) extends (A ⇒ B) { | ||
def apply(input: A) = f(input) | ||
def ~>[AA, BB, R](g: AA ⇒ BB)(implicit aux: TransformerAux[A, B, AA, BB, R]) = | ||
new WithTransformerConcatenation[A, R](aux(f, g)) | ||
} | ||
} | ||
|
||
object TransformerPipelineSupport extends TransformerPipelineSupport | ||
|
||
trait TransformerAux[A, B, AA, BB, R] { | ||
def apply(f: A ⇒ B, g: AA ⇒ BB): A ⇒ R | ||
} | ||
|
||
object TransformerAux { | ||
implicit def aux1[A, B, C] = new TransformerAux[A, B, B, C, C] { | ||
def apply(f: A ⇒ B, g: B ⇒ C): A ⇒ C = f andThen g | ||
} | ||
implicit def aux2[A, B, C](implicit ec: ExecutionContext) = | ||
new TransformerAux[A, Future[B], B, C, Future[C]] { | ||
def apply(f: A ⇒ Future[B], g: B ⇒ C): A ⇒ Future[C] = f(_).map(g) | ||
} | ||
implicit def aux3[A, B, C](implicit ec: ExecutionContext) = | ||
new TransformerAux[A, Future[B], B, Future[C], Future[C]] { | ||
def apply(f: A ⇒ Future[B], g: B ⇒ Future[C]): A ⇒ Future[C] = f(_).flatMap(g) | ||
} | ||
} |
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