#MyRetail REST API
MyRetail RESTful service provides the client application ability to:
1. Retrieve Product and Price information by Product Id
2. Send request to modify the price information in the database
###Input: The client application does a GET request at the path "/products/{id}" for a product
###Internal Working: When the API receives the request, it sends a request to "redsky.target.com" and retrieves the product information. This product information doesn't contain price that is needed by the user. The price is retrieved from a data store. The price information is now combined with the required product information to provide only the required product information to the user.
###Output: For a product with product id '13860428', the sample JSON output is as shown below
{"id":13860428,"name":"The Big Lebowski (Blu-ray) (Widescreen)","current_price":{"value": 13.49,"currency_code":"USD"}}
###Errors/Validations: Appropriate error messages are provided after validating the data. More information is available in the below sections. The client application can use the message in the response to display the same to the user appropriately.
###Input: The user/client application can do a PUT request with input similar to the response received in GET and should be able to modify the price in the datastore. The request is done at the same path "/products/{id}"
####Sample Input: JSON Body - {"id":13860428,"name":"The Big Lebowski (Blu-ray) (Widescreen)","current_price":{"value": 15.67,"currency_code":"USD"}}
###Internal Working: When the API receives PUT request, it does some validations to see if the product is available. If it is, it ensures that the id in the URL and the JSON body is similar and if that looks same, the price for the product is modified in the data store.
###Output: Success message is returned if the price modification is done.
###Errors/Validations: Appropriate error messages are provided after validating the data. More information is available in the below sections. The client application can use the message in the response to display the same to the user appropriately.
- Spring Boot - https://projects.spring.io/spring-boot/
- MongoDB - https://www.mongodb.com/
- Swagger - http://swagger.io/
- Gradle - https://gradle.org
- Install MongoDB in your system - https://docs.mongodb.com/manual/installation/
- Install Gradle - https://gradle.org/gradle-download/
- Run MongoDB - Run 'mongod.exe' in order to start Mongodb
- Clone the code from git repository - https://github.com/Gayathri16/MyRetail.git
- Make sure you are in the MyRetail directory
- Run the following command to start
./gradlew bootRun
- Open browser and visit Swagger.
http://localhost:8080/swagger-ui.html
- Swagger documentation explains the expected request and response for GET and PUT requests.
The testcases are present in the folder 'src\test\java\myretail\controllers'.
The test cases can be executed by running the command './gradlew test'
Swagger displays the following information for an API method by default. Please refer to images 'Default_GetProductInfo.png' and 'Default_PutRequest.png' to see how the default information for an API method looks like in Swagger.
- Type of request(GET/PUT..) and the path of request
- Status and format of the response
- Response Content Type
- Parameters list
- Possible Failure Responses with HTTP code
The user can modify the values in the fields provided and can do "Try it out!" at the bottom. Please refer to the images 'Sample_GET_Success.png' and 'Sample_PUT_Success.png' to see what a sample GET and PUT requests look like.
More information about the API methods and the responses is provided below.
Following PUT request will store information of productID:13860428 in NOSQL database
###Request:
curl -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{"id":13860428,"name":"The Big Lebowski (Blu-ray) (Widescreen)","current_price":{"value": 13.49,"currency_code":"USD"}} \ ' 'http://localhost:8080/products/13860428'
###Response:
Status code: 201 { "response": "success" }
- When productId in request url and body is different it will return 400 Bad Request
###Request:
curl -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{"id":13860428,"name":"The Big Lebowski (Blu-ray) (Widescreen)","current_price":{"value": 13.49,"currency_code":"USD"}} \ ' 'http://localhost:8080/products/15117729'
###Response:
{ "timestamp": 1478668717076, "status": 400, "error": "Bad Request", "exception": "myretail.exception.ProductMisMatchException", "message": "ProductId in request header and body doesn't match.", "path": "/products/15117729" }
curl -X GET --header 'Accept: application/json' 'http://localhost:8080/products/13860428'
{ "productId": "13860428", "title": "The Big Lebowski (Blu-ray)", "current_price": { "value": "13.49", "currency_code": "USD" } }
- When you give a productID that doesn't exist you will get 404 Product not found.
curl -X GET --header 'Accept: application/json' 'http://localhost:8080/products/235345436'
{ "timestamp": 1478669015840, "status": 404, "error": "Not Found", "exception": "myretail.exception.ProductNotFoundException", "message": "Product not found", "path": "/products/235345436" }