Create a simple command-line REST client called restful.py
able to GET
and POST
from JSONPlaceholder.
Usage $ chmod +x restful.py
for a Linux script executable
- Written in Python 3 using
requests
library (no framework). - Should include
requirements.txt
file forpip3 install -r requirements.txt
into avenv
virtual environment. restful.py
should be an executable Linux script. Usage should be./restful.py
notpython3 restful.py
.- Script should use
argparse
library to process the command-line arguments and options. - Error-handling: script should always display the response's HTTP status code. If the response code is not
2XX
, the script should exit with an error message and non-zero exit code, and not perform any additional action. - Object-oriented: all functionality after argument parsing should be performed by class methods, rather than in procedural-style functions.
- Positional argument
METHOD
a choice ofget
orpost
- Positional argument
ENDPOINT
is any URI fragment, i.e.,/posts/1
- Option
-o OUTFILE, --output OUTFILE
should write response to a JSON file ifOUTFILE
ends with.json
, or to a CSV file ifOUTFILE
ends with.csv
. IfOUTFILE
not provided, the default behavior should be to dump the response JSON tostdout
.
We use Git for version control. Once the challenge is completed, please push your code to a new repository in your personal GitHub or Bitbucket account, then email us the URL so that we can clone and review your submission.
Getting help:
$ ./restful.py -h
usage: restful.py [-h] [-d DATA] [-o OUTPUT] {get,post} endpoint
positional arguments:
{get,post} Request method
endpoint Request endpoint URI fragment
optional arguments:
-h, --help show this help message and exit
-d DATA, --data DATA Data to send with request
-o OUTPUT, --output OUTPUT
Output to .json or .csv file (default: dump to stdout)
GET all posts and dump to console:
$ ./restful.py get /posts
GET all posts and dump to JSON file:
$ ./restful.py get /posts -o test.json
GET all posts and dump to CSV file:
$ ./restful.py get /posts -o test.csv
GET one post and dump to console:
$ ./restful.py get /posts/1
POST a new dummy post and dump response to console:
$ ./restful.py post /posts --data '{title: "CIELO Rocks!", body: "It really really rocks.", userId: 1}'
POST a new dummy post and write response to JSON file:
$ ./restful.py post /posts -d '{title: "CIELO Rocks!", body: "It really really rocks.", userId: 1}' -o test.json