Merge: popcorn: some useful services for JSON related REST API
authorJean Privat <jean@pryen.org>
Tue, 6 Jun 2017 15:44:08 +0000 (11:44 -0400)
committerJean Privat <jean@pryen.org>
Tue, 6 Jun 2017 15:44:08 +0000 (11:44 -0400)
This PR introduces a way to validate and deserialize requests bodies with some shortcuts.

I'm not a big fan of the `new_body_object` but I don't see how to do this elegantly...

~~~nit
class MyJsonHandler
  super Handler

  # Validator used do validate the body
  redef var validator = new MyFormValidator

  # Define the kind of objects expected by the deserialization process
  redef type BODY: MyForm

  # Instanciate the object from a deserializer
  redef fun new_body_object(d) do return new MyForm.from_deserializer(d)

  redef fun post(req, res) do
      var post = validate_body(req, res)
      if post == null then return # Validation error: let popcorn return a HTTP 400
      var form = deserialize_body(req, res)
      if form == null then return # Deserialization error: let popcorn return a HTTP 400

      # TODO do something with the input
      print form.name
  end
end

class MyForm
  serialize

  var name: String
end

class MyFormValidator
  super ObjectValidator

  init do
      add new StringField("name", min_size=1, max_size=255)
  end
end
~~~

Also, I moved json related services to this module and updated the clients.

Pull-Request: #2465
Reviewed-by: Jean Privat <jean@pryen.org>


Trivial merge