Search code examples
pythonrestflask-restful

Best way to parse arguments in a REST api


I am building a Rest API based on flask Restful. And I'm troubled what would be the best way to parse the arguments I am waiting for.

The info:

Table Schema:

-----------------------------------------
| ID | NAME | IP | MAIL | OS | PASSWORD |
-----------------------------------------

Method that does the job:

def update_entry(data, id):
    ...
    ...
pass

The resource that handles the request:

def put(self, id):        
    json_data = request.get_json(force=True)
    update_entry(json_data, id)
    pass

Json format:

{'NAME': 'john', 'OS': 'windows'}

I have to mention that I do not know if all the above are relevant to my question.

Now what I would like to know is, where is the proper place to check if the client sent the arguments i want or the keys in his request are valid. I have thought a couple of alternatives but i have the feeling that i'm missing a best practice here.

  1. Pass whatever the client sends to the backend let an error happen and catch it.
  2. Create sth like a json template and validate the client's request with that before pass it back.

Ofc the first option is simplier, but the second doesn't create unnecessary load to my db although might become quite complex.

Any opinion for either of the above two or any other suggestion welcome.


Solution

  • Why you don't consider to use a library like marchmallow since the flask-restful documentation suggest it? It will answer your problems in a proper and non custom why like if you would right the validation from scratch.