Welcome to the REST API for Baker Street! You can use our API to create view and edit products on Baker Street Online Store. Baker Street is inspired from Sherlock Homes :)
Baker Street API is built using REST principles and without any framework in mind. This API follows HTTP rules, enabling a wide range of HTTP clients can be used to interact with the API.
URL is https://bakerstreetwala.herokuapp.com I could not get bakerstreet on heroku
All Baker Street API calls requires a minimum of one mandatory header.
Authorization token - Authentication request header.
All Baker Street API calls need to be authenticated using an authtoken.
You can obtain an authtoken by the following way:
Submit an HTTP POST request to the below URL.
https://bakerstreetwala.herokuapp.com/authtoken/create.
Below are the mandatory case sensitive fields to be passed in POST request.
Example:
parameter | value |
---|---|
username | admin |
password | admin |
###Example
curl -i "https://bakerstreetwala.herokuapp.com/authtoken/create"\
-d "username=admin&password=admin"
###Response
{
"content": {
"token": "58712b8969a5a"
}
}
###POINTS TO NOTE
-
every time you create a new token, previous token will be invalidated.
-
all requests should have token as authentication field.
Baker Street API uses appropriate HTTP verbs for every action.
Method | Description |
---|---|
GET | Used for retrieving resources and SEARCHing through records. |
POST | Used for creating resources and performing resource actions |
PUT | Used for updating resources. |
DELETE | Used for deleting resources. |
All timestamps are returned in the ISO 8601 format - YYYY-MM-DDThh:mm:ssTZD.
Example: 2016-06-11T17:38:06-0700
Baker Street API uses HTTP status codes to indicate success or failure of an API call. In general, status codes in the 2xx range means success, 4xx range means there was an error in the provided information, and those in the 5xx range indicates server side errors. Commonly used HTTP status codes are listed below.
Status Code | Description |
---|---|
200 | OK |
201 | CREATED |
400 | Bad request |
401 | Unauthorized (Invalid AuthToken) |
404 | URL Not Found |
405 | Method Not Allowed (Method you have called is not supported for the invoked API) |
500 | Internal Error |
A product refers to the item present in the Baker Street Store.
Attribute | type |
---|---|
id | string - unique id generated by the server for the product |
name | string - name of the product |
description | string - description for the product |
status | string - If a product is active |
created_at | date - Unixdate time format |
updated_at | data - Last time when product was updated |
{
"id": "1",
"name": "Cake",
"description": "Eggless Dark Choclate",
"status": "active",
"created_time": "2016-06-05T17:38:06-0700",
"updated_time": "2016-06-05T20:09:23-0700"
}
Creating a product
POST /products
POST attributes:
Attribute | type |
---|---|
name | string - name of the product |
description | string - description for the product |
curl -i "https://bakerstreetwala.herokuapp.com/products"\
-d "name=cake&description=test"\
-H "authtoken: 58712b8969a5a"
NOTE : name and description are required fields
###Response
HTTP/1.1 200 OK
{
"product":
{
"id": "1",
"name": "Cake",
"description": "Eggless Dark Choclate",
"status": "active",
"created_time": "2016-06-05T17:38:06-0700",
"updated_time": "2016-06-05T20:09:23-0700"
}
}
GET /products/{productid}
curl -i "https://bakerstreetwala.herokuapp.com/products/1" \
-H "authtoken: 58712b8969a5a"
###Response HTTP/1.1 200 OK
{
"product":
{
"id": "1",
"name": "Cake",
"description": "Eggless Dark Choclate",
"status": "active",
"created_time": "2016-06-05T17:38:06-0700",
"updated_time": "2016-06-05T20:09:23-0700"
}
}
GET /products
curl -i "https://bakerstreetwala.herokuapp.com/products" \
-H "authtoken: 58712b8969a5a"
HTTP/1.1 200 OK
{
"products":
[
{
"id": "1",
"name": "Cake",
"description": "Eggless Dark Choclate",
"status": "active",
"created_time": "2016-06-05T17:38:06-0700",
"updated_time": "2016-06-05T20:09:23-0700"
},
{
"id": "2",
"name": "Cake new one",
"description": "Eggless Dark Choclate",
"status": "active",
"created_time": "2016-06-05T17:38:06-0700",
"updated_time": "2016-06-05T20:09:23-0700"
}
]
}
PUT /products/{productid}
curl -i "https://bakerstreetwala.herokuapp.com/products/1"\
-X "PUT" \
-d "name=newname&description=new+description&status=deleted"\
-H "authtoken: 58712b8969a5a"
NOTE : name and description are required fields
HTTP/1.1 200 OK
{
"product":
{
"id": "1",
"name": "newname",
"description": "new description",
"status": "deleted",
"created_time": "2016-06-05T17:38:06-0700",
"updated_time": "2016-06-05T20:09:23-0700"
}
}
DELETE /products/1
curl -i "https://bakerstreetwala.herokuapp.com/products/19" \
-X "DELETE" \
-H "authtoken: 58712b8969a5a"
HTTP/1.1 200 OK
{
"status":"done"
}
GET /products?name=cake
curl -i "https://bakerstreetwala.herokuapp.com/products?name=latest" \
-H "authtoken: 58712b8969a5a"
Note: Currently search is available for name only
HTTP/1.1 200 OK
{
"products":
{
"id": "20",
"name": "latest cake",
"description": "new cake",
"status": "active",
"created_time": "2016-06-05T17:38:06-0700",
"updated_time": "2016-06-05T20:09:23-0700"
}
}
Install the Heroku Toolbelt.
Get the code from GitHub
$ git clone https://github.com/anuragmaher/bakerstreet.git # or clone your own fork
$ cd bakerstreet
Heroku login and creating an app
$ heroku login
$ heroku create herokuappname
Now create a mysql instance
$ heroku addons:create cleardb:ignite
Make sure CLEARDB_DATABASE_URL ENV is set to the mysql URI
Adding heroku as remote for pushing and deploying
$ heroku git:remote -a herokuappname
Pushing code to heroku directory
$ git push heroku master
Running Database migrations, creating of tables and creating admin user
$ heroku open migrate/db
$ heroku open tests/start
####Automated Testing
###Test Cases for Authentication:
Test 1- username: usernotpresent and password: junk
Test case passed UserNotFoundException is thrown
Test 2- username: admin and password: junk
Test case passed PasswordNotMatchException is thrown
Test 3- username: admin and password: admin
Test case passed
All tests for authtication passed New Token: 587280c834e05
Now all the tests will use this token : 587280c834e05 for authentication
Test 4 : Get products without authtication GET /products
Test case passed 401 Unauthorized recieved
Test 5 : Get products without authtication GET /products and wrong token
Test case passed 401 Unauthorized recieved
Test 6 : Get products without authtication GET /products and correct token: 587280c834e05
Test case passed
###Test Cases for Products:
Test 7 : POST /products with token and name and description
Test case passed Product created with id: 2
Test 8 : GET /products/2 with token and productid
Test case passed Product
Test 9 : PUT /products/2 with token and name and description
Test case passed
Test 10 : DELETE /products/{productid} with token
Test case passed
"All Tests Passed"
####Assumptions:
-
currently token is not expiring, we should have a expiry time for the token.
-
URL does not have multiple slash together like ///