Last active
August 29, 2015 14:23
-
-
Save gtopper/d8969d8c7e8e52f192ba to your computer and use it in GitHub Desktop.
Attempt at serializing HttpRequest to JSON, leaving out the URI.
This file contains hidden or 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
package com.supersonicads.utils.spray | |
import com.supersonicads.utils.OptionUtil._ | |
import play.api.libs.json.Json | |
import spray.http.HttpHeaders.RawHeader | |
import spray.http.{HttpMethods, HttpProtocols, HttpRequest} | |
case class LeanHttpRequest(entity: String, | |
headers: List[RawHeader], | |
protocol: String, | |
method: String) { | |
def toHttpRequest = HttpRequest( | |
method = HttpMethods.getForKey(method).toTry(s"HTTP method $method is invalid.").get, | |
headers = headers, | |
entity = entity, | |
protocol = HttpProtocols.getForKey(protocol).toTry(s"Protocol $protocol is invalid.").get | |
) | |
} | |
object LeanHttpRequest { | |
def fromHttpRequest(request: HttpRequest) = | |
LeanHttpRequest( | |
request.entity.asString, | |
request.headers.map(header => RawHeader(header.name, header.value)), | |
request.protocol.value, | |
request.method.value | |
) | |
implicit val fmt = { | |
implicit val rawHeaderFmt = Json.format[RawHeader] | |
Json.format[LeanHttpRequest] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment