diff options
author | adamw <adam@warski.org> | 2017-06-30 17:14:54 +0200 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-06-30 17:14:54 +0200 |
commit | 10edc9d4469bac8fa760befb91422cad96d838fe (patch) | |
tree | a1cf4ecbf6771d455d5af38473fc38c43c7c67b5 | |
parent | e0e1c4be8642c2bf3fb70ef6427565c6fcf4d3ef (diff) | |
download | sttp-10edc9d4469bac8fa760befb91422cad96d838fe.tar.gz sttp-10edc9d4469bac8fa760befb91422cad96d838fe.tar.bz2 sttp-10edc9d4469bac8fa760befb91422cad96d838fe.zip |
More methods
3 files changed, 31 insertions, 18 deletions
diff --git a/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala b/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala index 876fea8..7c36da6 100644 --- a/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala +++ b/akka-http-handler/src/main/scala/com/softwaremill/sttp/akkahttp/AkkaHttpSttpHandler.scala @@ -22,26 +22,33 @@ class AkkaHttpSttpHandler(actorSystem: ActorSystem) extends SttpStreamHandler[Fu import as.dispatcher override def send[T](r: Request, responseAs: ResponseAs[T]): Future[Response[T]] = { - requestToAkka(r).flatMap(setBody(r, r.body, _)).flatMap(Http().singleRequest(_)).flatMap { hr => + requestToAkka(r).flatMap(setBodyOnAkka(r, r.body, _)).flatMap(Http().singleRequest(_)).flatMap { hr => val code = hr.status.intValue() - bodyFromAkkaResponse(responseAs, hr).map(Response(code, _)) + bodyFromAkka(responseAs, hr).map(Response(code, _)) } } override def send(r: Request, responseAsStream: ResponseAsStream[Source[ByteString, Any]]): Future[Response[Source[ByteString, Any]]] = { - requestToAkka(r).flatMap(setBody(r, r.body, _)).flatMap(Http().singleRequest(_)).map { hr => + requestToAkka(r).flatMap(setBodyOnAkka(r, r.body, _)).flatMap(Http().singleRequest(_)).map { hr => val code = hr.status.intValue() Response(code, hr.entity.dataBytes) } } - private def convertMethod(m: Method): HttpMethod = m match { + private def methodToAkka(m: Method): HttpMethod = m match { case Method.GET => HttpMethods.GET + case Method.HEAD => HttpMethods.HEAD case Method.POST => HttpMethods.POST + case Method.PUT => HttpMethods.PUT + case Method.DELETE => HttpMethods.DELETE + case Method.OPTIONS => HttpMethods.OPTIONS + case Method.PATCH => HttpMethods.PATCH + case Method.CONNECT => HttpMethods.CONNECT + case Method.TRACE => HttpMethods.TRACE case _ => HttpMethod.custom(m.m) } - private def bodyFromAkkaResponse[T](rr: ResponseAs[T], hr: HttpResponse): Future[T] = { + private def bodyFromAkka[T](rr: ResponseAs[T], hr: HttpResponse): Future[T] = { def asByteArray = hr.entity.dataBytes .runFold(ByteString(""))(_ ++ _) .map(_.toArray[Byte]) @@ -60,7 +67,7 @@ class AkkaHttpSttpHandler(actorSystem: ActorSystem) extends SttpStreamHandler[Fu } private def requestToAkka(r: Request): Future[HttpRequest] = { - val ar = HttpRequest(uri = r.uri.toString, method = convertMethod(r.method)) + val ar = HttpRequest(uri = r.uri.toString, method = methodToAkka(r.method)) val parsed = r.headers.map(h => HttpHeader.parse(h._1, h._2)) val errors = parsed.collect { case ParsingResult.Error(e) => e @@ -76,7 +83,7 @@ class AkkaHttpSttpHandler(actorSystem: ActorSystem) extends SttpStreamHandler[Fu } } - private def setBody(r: Request, body: RequestBody, ar: HttpRequest): Future[HttpRequest] = body match { + private def setBodyOnAkka(r: Request, body: RequestBody, ar: HttpRequest): Future[HttpRequest] = body match { case NoBody => Future.successful(ar) case StringBody(b, encoding) => Future.successful(ar.withEntity(b)) // TODO case ByteArrayBody(b) => Future.successful(ar.withEntity(b)) @@ -84,14 +91,14 @@ class AkkaHttpSttpHandler(actorSystem: ActorSystem) extends SttpStreamHandler[Fu case InputStreamBody(b) => Future.successful(ar) //TODO case FileBody(b) => Future.successful(ar)//TODO case PathBody(b) => Future.successful(ar) //TODO - case sb@SerializableBody(_, _) => setSerializableBody(r, sb, ar) + case sb@SerializableBody(_, _) => setSerializableBodyOnAkka(r, sb, ar) } - private def setSerializableBody[T](r: Request, body: SerializableBody[T], ar: HttpRequest): Future[HttpRequest] = body match { + private def setSerializableBodyOnAkka[T](r: Request, body: SerializableBody[T], ar: HttpRequest): Future[HttpRequest] = body match { case SerializableBody(SourceBodySerializer, t) => getContentTypeOrOctetStream(r).map(ct => ar.withEntity(HttpEntity(ct, t))) - case SerializableBody(f, t) => setBody(r, f(t), ar) + case SerializableBody(f, t) => setBodyOnAkka(r, f(t), ar) } private def getContentTypeOrOctetStream(r: Request): Future[ContentType] = { diff --git a/core/src/main/scala/com/softwaremill/sttp/model/package.scala b/core/src/main/scala/com/softwaremill/sttp/model/package.scala index 1541398..e50426c 100644 --- a/core/src/main/scala/com/softwaremill/sttp/model/package.scala +++ b/core/src/main/scala/com/softwaremill/sttp/model/package.scala @@ -14,6 +14,8 @@ package object model { val DELETE = Method("DELETE") val OPTIONS = Method("OPTIONS") val PATCH = Method("PATCH") + val CONNECT = Method("CONNECT") + val TRACE = Method("TRACE") } /** diff --git a/core/src/main/scala/com/softwaremill/sttp/package.scala b/core/src/main/scala/com/softwaremill/sttp/package.scala index 67be14a..40b8d82 100644 --- a/core/src/main/scala/com/softwaremill/sttp/package.scala +++ b/core/src/main/scala/com/softwaremill/sttp/package.scala @@ -61,6 +61,10 @@ package object sttp { type Empty[X] = None.type def ignoreResponse: ResponseAs[Unit] = IgnoreResponse + /** + * Uses `utf-8` encoding. + */ + def responseAsString: ResponseAs[String] = responseAsString(Utf8) def responseAsString(encoding: String): ResponseAs[String] = ResponseAsString(encoding) def responseAsByteArray: ResponseAs[Array[Byte]] = ResponseAsByteArray def responseAsStream[S]: ResponseAsStream[S] = ResponseAsStream[S]() @@ -137,41 +141,41 @@ package object sttp { } /** - * If content type is not specified, will be set to `text/plain` with `utf-8` encoding. + * If content type is not yet specified, will be set to `text/plain` with `utf-8` encoding. */ def data(b: String): RequestTemplate[U] = data(b, Utf8) /** - * If content type is not specified, will be set to `text/plain` with the given encoding. + * If content type is not yet specified, will be set to `text/plain` with the given encoding. */ def data(b: String, encoding: String): RequestTemplate[U] = setContentTypeIfMissing(contentTypeWithEncoding(TextPlainContentType, encoding)).copy(body = StringBody(b, encoding)) /** - * If content type is not specified, will be set to `application/octet-stream`. + * If content type is not yet specified, will be set to `application/octet-stream`. */ def data(b: Array[Byte]): RequestTemplate[U] = setContentTypeIfMissing(ApplicationOctetStreamContentType).copy(body = ByteArrayBody(b)) /** - * If content type is not specified, will be set to `application/octet-stream`. + * If content type is not yet specified, will be set to `application/octet-stream`. */ def data(b: ByteBuffer): RequestTemplate[U] = setContentTypeIfMissing(ApplicationOctetStreamContentType).copy(body = ByteBufferBody(b)) /** - * If content type is not specified, will be set to `application/octet-stream`. + * If content type is not yet specified, will be set to `application/octet-stream`. */ def data(b: InputStream): RequestTemplate[U] = setContentTypeIfMissing(ApplicationOctetStreamContentType).copy(body = InputStreamBody(b)) /** - * If content type is not specified, will be set to `application/octet-stream`. + * If content type is not yet specified, will be set to `application/octet-stream`. */ def data(b: File): RequestTemplate[U] = setContentTypeIfMissing(ApplicationOctetStreamContentType).copy(body = FileBody(b)) /** - * If content type is not specified, will be set to `application/octet-stream`. + * If content type is not yet specified, will be set to `application/octet-stream`. */ def data(b: Path): RequestTemplate[U] = setContentTypeIfMissing(ApplicationOctetStreamContentType).copy(body = PathBody(b)) /** - * If content type is not specified, will be set to `application/octet-stream`. + * If content type is not yet specified, will be set to `application/octet-stream`. */ def data[T: BodySerializer](b: T): RequestTemplate[U] = setContentTypeIfMissing(ApplicationOctetStreamContentType).copy(body = SerializableBody(implicitly[BodySerializer[T]], b)) |