Restful API for SmartCar which makes HTTP requests to the GM(General Motors) API and returns the information in a cleaner format.
- Make sure you have python3 installed.
- Now we install pip as it makes it easier to download the latest version of Django globally. Execute the following command.
$ sudo apt-get install python3-pip
- Now we install Django using pip3. To do this execute the following command.
$ sudo pip3 install django
- We also need to install django rest framework. To do this execute the following command.
$ sudo pip3 install djangorestframework
- Last step in the setup is cloning this repository.
$ git clone <remote-url>
$ cd SmartCar-API
- We need to go in the smartcar directory and run the manage.py file.
- Make migrations before running the server.
$ cd smartcar/
$ python3 manage.py makemigrations
$ python3 manage.py migrate
$ python3 manage.py runserver
- Your server must now be up and running without any errors.
If all the previous steps have been completed successfully your server must be up and running. Now we need to make requests to the SmartCar API and check the responses.
Open any internet browser and type in the following URL.
http://127.0.0.1:8000
If you see the word Localhost in the browser you have the server up and running. Available IDs which can be used for making API calls to SmartCar endpoints are : 1234, 1235
GET /vehicles/:id
{
"vin": "1213231",
"color": "Metallic Silver",
"doorCount": 4,
"driveTrain": "v8"
}
GET /vehicles/:id/doors
[
{
"location": "frontLeft",
"locked": true
},
{
"location": "frontRight",
"locked": true
}
]
GET /vehicles/:id/fuel
{
"percent": 30
}
GET /vehicles/:id/battery
{
"percent": 50
}
POST /vehicles/:id/engine
Content-Type: application/json
{
"action": "START|STOP"
}
{
"status": "success|error"
}
Now you can make the following requests to the API and check the responses. Execute the following commands in the terminal.
$ curl http://127.0.0.1:8000/vehicles/1234
$ curl http://127.0.0.1:8000/vehicles/1234/doors
$ curl http://127.0.0.1:8000/vehicles/1234/fuel
$ curl http://127.0.0.1:8000/vehicles/1234/battery
You could also run these URLs in the browser and see the responses.
For the post request you could type in the following command in the terminal.
$ curl http://127.0.0.1:8000/vehicles/1234/engine -H 'Content-Type: application/json' -d '{"action": "START|STOP"}'
Following are some files and modules which form the core base of this Restful API implementation.
- utilities - This folder is inside the smartcar/webapp directory. It contains two main utility files namely GeneralMotors.py and SmartCar.py. Functionalities of each file have been explained in the file itself.
- models.py - Contains models for each type of request. Each models contains the variables necessary for creating a response using the serializer, according to the specifications.
- serializers.py - Contains serializers for each type of model. Each serializer creates a json response which contain the fields specified in the fields[] list.
- views.py - Contains views for each GET/POST request to the SmartCar API. Whenever the SmartCar API is called, one of the following views is triggered which calls the corresponding method in the utilities/SmartCar.py file. The returned dictionary is used to create an object of the model corresponding to the request. This object is passed to the serializer to get the json response which is then returned by the API.
Tests have been provided to check the functioning of all modules. These are written in tests.py file inside webapp/ directory. There are a total of 15 tests which have been written for testing the SmartCar API and are organized into 3 main classes.
- GeneralMotorsUtilityTests - Tests for checking whether the GM API responds correctly to requests
- SmartCarUtilityTests - Tests for checking functionality of all the methods in utilities/SmartCar.py
- SmartCarApiTests - Tests for checking whether the SmartCar API responds correctly to requests
- In order to run the tests first we make sure the server is running. If not, then execute the following command.
$ python3 manage.py runserver
- Now that we have our server running we can run the tests by executing the following command.
$ python3 manage.py test
- After running all the tests the following message should be displayed.
Ran 15 tests in 21.398s
OK
This indicates that all the test cases have been passed.
Else, you'll get FAILED message indicating some/all tests have failed.