Skip to content
This repository has been archived by the owner on Apr 10, 2019. It is now read-only.

Commit

Permalink
#150, refactor: Allow custom headers for empty responses
Browse files Browse the repository at this point in the history
  • Loading branch information
slavaschmidt committed Jun 21, 2016
1 parent a844de1 commit 159f1f7
Show file tree
Hide file tree
Showing 23 changed files with 128 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ trait WrappedBodyParsersBase {
}

trait ResultWrapper[ResultT] {
val emptyByteString = akka.util.CompactByteString.empty
def statusCode: Int
def result: ResultT
def writer: String => Option[Writeable[ResultT]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ trait {{controller.base}} extends Controller with PlayBodyParsing {{if controlle
case class {{m.result_class_prefix}}{{result_type.code}}(result: {{result_type.type}})(implicit val writer: String => Option[Writeable[{{result_type.type}}]]) extends {{m.result_class_prefix}}Type[{{result_type.type}}] { val statusCode = {{result_type.code}} }
{{/for}}
{{for result_type in m.empty_result_types}}
case object {{m.result_class_prefix}}{{result_type.code}} extends EmptyReturn({{result_type.code}})
case class {{m.result_class_prefix}}{{result_type.code}}(headers: Seq[(String, String)] = Nil) extends EmptyReturn({{result_type.code}}, headers)
{{/for}}
{{for mapping in m.error_mappings}}
case class {{m.result_class_prefix}}{{mapping.simple_exception_name}}(result: {{mapping.exception_name}})(implicit val writer: String => Option[Writeable[java.lang.Exception]]) extends {{m.result_class_prefix}}Type[{{mapping.exception_name}}] { val statusCode = {{mapping.exception_code}} }
Expand Down Expand Up @@ -115,7 +115,7 @@ def {{m.action}}[T] = (f: {{m.action_type}}[T]) =>{{if m.non_body_params}} ({{/i
}
}
{{/for}}
abstract class EmptyReturn(override val statusCode: Int = 204) extends ResultWrapper[Results.EmptyContent] {{for m in controller.methods}} with {{m.result_class_prefix}}Type[Results.EmptyContent]{{/for}} { val result = Results.EmptyContent(); val writer = (x: String) => Some(new DefaultWriteables{}.writeableOf_EmptyContent); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.NoContent) }
abstract class EmptyReturn(override val statusCode: Int, headers: Seq[(String, String)]) extends ResultWrapper[Result] {{for m in controller.methods}} with {{m.result_class_prefix}}Type[Result]{{/for}} { val result = Results.Status(204).withHeaders(headers:_*); val writer = (x: String) => Some(new Writeable((_:Any) => emptyByteString, None)); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.Status(204)) }
case object NotImplementedYet extends ResultWrapper[Results.EmptyContent] {{for m in controller.methods}} with {{m.result_class_prefix}}Type[Results.EmptyContent]{{/for}} { val statusCode = 501; val result = Results.EmptyContent(); val writer = (x: String) => Some(new DefaultWriteables{}.writeableOf_EmptyContent); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.NotImplemented) }
}
{{/for}}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import de.zalando.play.controllers.ResponseWriters
trait BasicAuthApiYamlBase extends Controller with PlayBodyParsing with BasicAuthApiYamlSecurity {
sealed trait GetType[T] extends ResultWrapper[T]

case object Get200 extends EmptyReturn(200)
case class Get200(headers: Seq[(String, String)] = Nil) extends EmptyReturn(200, headers)


private type getActionRequestType = (Unit)
Expand All @@ -46,6 +46,6 @@ def getAction[T] = (f: getActionType[T]) => getActionConstructor { request =>
Results.NotAcceptable
}
}
abstract class EmptyReturn(override val statusCode: Int = 204) extends ResultWrapper[Results.EmptyContent] with GetType[Results.EmptyContent] { val result = Results.EmptyContent(); val writer = (x: String) => Some(new DefaultWriteables{}.writeableOf_EmptyContent); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.NoContent) }
abstract class EmptyReturn(override val statusCode: Int, headers: Seq[(String, String)]) extends ResultWrapper[Result] with GetType[Result] { val result = Results.Status(204).withHeaders(headers:_*); val writer = (x: String) => Some(new Writeable((_:Any) => emptyByteString, None)); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.Status(204)) }
case object NotImplementedYet extends ResultWrapper[Results.EmptyContent] with GetType[Results.EmptyContent] { val statusCode = 501; val result = Results.EmptyContent(); val writer = (x: String) => Some(new DefaultWriteables{}.writeableOf_EmptyContent); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.NotImplemented) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ def postAction[T] = (f: postActionType[T]) => postActionConstructor(BodyParsers.
Results.NotAcceptable
}
}
abstract class EmptyReturn(override val statusCode: Int = 204) extends ResultWrapper[Results.EmptyContent] with PostType[Results.EmptyContent] { val result = Results.EmptyContent(); val writer = (x: String) => Some(new DefaultWriteables{}.writeableOf_EmptyContent); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.NoContent) }
abstract class EmptyReturn(override val statusCode: Int, headers: Seq[(String, String)]) extends ResultWrapper[Result] with PostType[Result] { val result = Results.Status(204).withHeaders(headers:_*); val writer = (x: String) => Some(new Writeable((_:Any) => emptyByteString, None)); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.Status(204)) }
case object NotImplementedYet extends ResultWrapper[Results.EmptyContent] with PostType[Results.EmptyContent] { val statusCode = 501; val result = Results.EmptyContent(); val writer = (x: String) => Some(new DefaultWriteables{}.writeableOf_EmptyContent); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.NotImplemented) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import de.zalando.play.controllers.ResponseWriters
trait EchoHandlerBase extends Controller with PlayBodyParsing {
sealed trait MethodType[T] extends ResultWrapper[T]

case object Method200 extends EmptyReturn(200)
case class Method200(headers: Seq[(String, String)] = Nil) extends EmptyReturn(200, headers)


private type methodActionRequestType = (Unit)
Expand All @@ -48,7 +48,7 @@ def methodAction[T] = (f: methodActionType[T]) => methodActionConstructor { requ
Results.NotAcceptable
}
}
abstract class EmptyReturn(override val statusCode: Int = 204) extends ResultWrapper[Results.EmptyContent] with MethodType[Results.EmptyContent] { val result = Results.EmptyContent(); val writer = (x: String) => Some(new DefaultWriteables{}.writeableOf_EmptyContent); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.NoContent) }
abstract class EmptyReturn(override val statusCode: Int, headers: Seq[(String, String)]) extends ResultWrapper[Result] with MethodType[Result] { val result = Results.Status(204).withHeaders(headers:_*); val writer = (x: String) => Some(new Writeable((_:Any) => emptyByteString, None)); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.Status(204)) }
case object NotImplementedYet extends ResultWrapper[Results.EmptyContent] with MethodType[Results.EmptyContent] { val statusCode = 501; val result = Results.EmptyContent(); val writer = (x: String) => Some(new DefaultWriteables{}.writeableOf_EmptyContent); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.NotImplemented) }
}
trait EchoApiYamlBase extends Controller with PlayBodyParsing {
Expand Down Expand Up @@ -98,7 +98,7 @@ def postAction[T] = (f: postActionType[T]) => postActionConstructor { request =>
}
sealed trait Gettest_pathByIdType[T] extends ResultWrapper[T]

case object Gettest_pathById200 extends EmptyReturn(200)
case class Gettest_pathById200(headers: Seq[(String, String)] = Nil) extends EmptyReturn(200, headers)


private type gettest_pathByIdActionRequestType = (String)
Expand Down Expand Up @@ -131,6 +131,6 @@ def gettest_pathByIdAction[T] = (f: gettest_pathByIdActionType[T]) => (id: Strin
Results.NotAcceptable
}
}
abstract class EmptyReturn(override val statusCode: Int = 204) extends ResultWrapper[Results.EmptyContent] with PostType[Results.EmptyContent] with Gettest_pathByIdType[Results.EmptyContent] { val result = Results.EmptyContent(); val writer = (x: String) => Some(new DefaultWriteables{}.writeableOf_EmptyContent); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.NoContent) }
abstract class EmptyReturn(override val statusCode: Int, headers: Seq[(String, String)]) extends ResultWrapper[Result] with PostType[Result] with Gettest_pathByIdType[Result] { val result = Results.Status(204).withHeaders(headers:_*); val writer = (x: String) => Some(new Writeable((_:Any) => emptyByteString, None)); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.Status(204)) }
case object NotImplementedYet extends ResultWrapper[Results.EmptyContent] with PostType[Results.EmptyContent] with Gettest_pathByIdType[Results.EmptyContent] { val statusCode = 501; val result = Results.EmptyContent(); val writer = (x: String) => Some(new DefaultWriteables{}.writeableOf_EmptyContent); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.NotImplemented) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ def getschemaModelAction[T] = (f: getschemaModelActionType[T]) => getschemaModel
Results.NotAcceptable
}
}
abstract class EmptyReturn(override val statusCode: Int = 204) extends ResultWrapper[Results.EmptyContent] with GetschemaModelType[Results.EmptyContent] { val result = Results.EmptyContent(); val writer = (x: String) => Some(new DefaultWriteables{}.writeableOf_EmptyContent); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.NoContent) }
abstract class EmptyReturn(override val statusCode: Int, headers: Seq[(String, String)]) extends ResultWrapper[Result] with GetschemaModelType[Result] { val result = Results.Status(204).withHeaders(headers:_*); val writer = (x: String) => Some(new Writeable((_:Any) => emptyByteString, None)); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.Status(204)) }
case object NotImplementedYet extends ResultWrapper[Results.EmptyContent] with GetschemaModelType[Results.EmptyContent] { val statusCode = 501; val result = Results.EmptyContent(); val writer = (x: String) => Some(new DefaultWriteables{}.writeableOf_EmptyContent); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.NotImplemented) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def addPetAction[T] = (f: addPetActionType[T]) => addPetActionConstructor(BodyPa
}
sealed trait DeletePetType[T] extends ResultWrapper[T]

case object DeletePet204 extends EmptyReturn(204)
case class DeletePet204(headers: Seq[(String, String)] = Nil) extends EmptyReturn(204, headers)

case class DeletePetNoSuchElementException(result: java.util.NoSuchElementException)(implicit val writer: String => Option[Writeable[java.lang.Exception]]) extends DeletePetType[java.util.NoSuchElementException] { val statusCode = 404 }

Expand Down Expand Up @@ -137,6 +137,6 @@ def deletePetAction[T] = (f: deletePetActionType[T]) => (id: Long) => deletePetA
Results.NotAcceptable
}
}
abstract class EmptyReturn(override val statusCode: Int = 204) extends ResultWrapper[Results.EmptyContent] with FindPetsType[Results.EmptyContent] with AddPetType[Results.EmptyContent] with DeletePetType[Results.EmptyContent] { val result = Results.EmptyContent(); val writer = (x: String) => Some(new DefaultWriteables{}.writeableOf_EmptyContent); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.NoContent) }
abstract class EmptyReturn(override val statusCode: Int, headers: Seq[(String, String)]) extends ResultWrapper[Result] with FindPetsType[Result] with AddPetType[Result] with DeletePetType[Result] { val result = Results.Status(204).withHeaders(headers:_*); val writer = (x: String) => Some(new Writeable((_:Any) => emptyByteString, None)); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.Status(204)) }
case object NotImplementedYet extends ResultWrapper[Results.EmptyContent] with FindPetsType[Results.EmptyContent] with AddPetType[Results.EmptyContent] with DeletePetType[Results.EmptyContent] { val statusCode = 501; val result = Results.EmptyContent(); val writer = (x: String) => Some(new DefaultWriteables{}.writeableOf_EmptyContent); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.NotImplemented) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,6 @@ def postbothAction[T] = (f: postbothActionType[T]) => postbothActionConstructor
Results.NotAcceptable
}
}
abstract class EmptyReturn(override val statusCode: Int = 204) extends ResultWrapper[Results.EmptyContent] with PostmultipartType[Results.EmptyContent] with Posturl_encodedType[Results.EmptyContent] with PostbothType[Results.EmptyContent] { val result = Results.EmptyContent(); val writer = (x: String) => Some(new DefaultWriteables{}.writeableOf_EmptyContent); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.NoContent) }
abstract class EmptyReturn(override val statusCode: Int, headers: Seq[(String, String)]) extends ResultWrapper[Result] with PostmultipartType[Result] with Posturl_encodedType[Result] with PostbothType[Result] { val result = Results.Status(204).withHeaders(headers:_*); val writer = (x: String) => Some(new Writeable((_:Any) => emptyByteString, None)); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.Status(204)) }
case object NotImplementedYet extends ResultWrapper[Results.EmptyContent] with PostmultipartType[Results.EmptyContent] with Posturl_encodedType[Results.EmptyContent] with PostbothType[Results.EmptyContent] { val statusCode = 501; val result = Results.EmptyContent(); val writer = (x: String) => Some(new DefaultWriteables{}.writeableOf_EmptyContent); override def toResult(mimeType: String): Option[play.api.mvc.Result] = Some(Results.NotImplemented) }
}
Loading

0 comments on commit 159f1f7

Please sign in to comment.