|
| 1 | +# Rest Api |
| 2 | + |
| 3 | +Implement a RESTful API for tracking IOUs. |
| 4 | + |
| 5 | +Four roommates have a habit of borrowing money from each other frequently, and have trouble remembering who owes whom, and how much. |
| 6 | + |
| 7 | +Your task is to implement a simple [RESTful API](https://en.wikipedia.org/wiki/Representational_state_transfer) that receives [IOU](https://en.wikipedia.org/wiki/IOU)s as POST requests, and can deliver specified summary information via GET requests. |
| 8 | + |
| 9 | +### API Specification |
| 10 | + |
| 11 | +#### User object |
| 12 | +```json |
| 13 | +{ |
| 14 | + "name": "Adam", |
| 15 | + "owes": { |
| 16 | + "Bob": 12.0, |
| 17 | + "Chuck": 4.0, |
| 18 | + "Dan": 9.5 |
| 19 | + }, |
| 20 | + "owed_by": { |
| 21 | + "Bob": 6.5, |
| 22 | + "Dan": 2.75, |
| 23 | + }, |
| 24 | + "balance": "<(total owed by other users) - (total owed to other users)>" |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +#### Methods |
| 29 | + |
| 30 | +| Description | HTTP Method | URL | Payload Format | Response w/o Payload | Response w/ Payload | |
| 31 | +| --- | --- | --- | --- | --- | --- | |
| 32 | +| List of user information | GET | /users | `{"users":["Adam","Bob"]}` | `{"users":<List of all User objects>}` | {"users":\<List of User objects for \<users\>\>} | |
| 33 | +| Create user | POST | /add | {"user":\<name of new user (unique)} | N/A | \<User object for new user\> | |
| 34 | +| Create IOU | POST | /iou | {"lender":\<name of lender\>,"borrower":\<name of borrower>,"amount":5.25} | N/A | {"users":\<updated User objects for \<lender\> and \<borrower\>\>} | |
| 35 | + |
| 36 | +### Other Resources: |
| 37 | +- https://restfulapi.net/ |
| 38 | +- Example RESTful APIs |
| 39 | + - [GitHub](https://developer.github.com/v3/) |
| 40 | + - [Reddit](https://www.reddit.com/dev/api/) |
| 41 | +## Exception messages |
| 42 | + |
| 43 | +Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to |
| 44 | +indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not |
| 45 | +every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include |
| 46 | +a message. |
| 47 | + |
| 48 | +To raise a message with an exception, just write it as an argument to the exception type. For example, instead of |
| 49 | +`raise Exception`, you should write: |
| 50 | + |
| 51 | +```python |
| 52 | +raise Exception("Meaningful message indicating the source of the error") |
| 53 | +``` |
| 54 | + |
| 55 | +## Running the tests |
| 56 | + |
| 57 | +To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)): |
| 58 | + |
| 59 | +- Python 2.7: `py.test rest_api_test.py` |
| 60 | +- Python 3.4+: `pytest rest_api_test.py` |
| 61 | + |
| 62 | +Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version): |
| 63 | +`python -m pytest rest_api_test.py` |
| 64 | + |
| 65 | +### Common `pytest` options |
| 66 | + |
| 67 | +- `-v` : enable verbose output |
| 68 | +- `-x` : stop running tests on first failure |
| 69 | +- `--ff` : run failures from previous test before running other test cases |
| 70 | + |
| 71 | +For other options, see `python -m pytest -h` |
| 72 | + |
| 73 | +## Submitting Exercises |
| 74 | + |
| 75 | +Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/rest-api` directory. |
| 76 | + |
| 77 | +You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`. |
| 78 | + |
| 79 | +For more detailed information about running tests, code style and linting, |
| 80 | +please see [Running the Tests](http://exercism.io/tracks/python/tests). |
| 81 | + |
| 82 | +## Submitting Incomplete Solutions |
| 83 | + |
| 84 | +It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
0 commit comments