The delivery service project is designed to find the most economical path for a delivery.
Given a map with routes, all that is needed is inform the origin, destination, autonomy and liter price, with these attributes the service will find the most economical path according to database.
- Install Java JDK 7 or newer
- Install Maven 3.3
- Install Sqlite3
After setting the prerequisites you can start the project:
- Clone the project at the command prompt:
$ git clone https://github.com/j133y/delivery-service-java.git
$ cd delivery-service-java
- Compile the project using maven:
$ mvn compile
- Run the tests:
$ mvn test
- Package and run the service:
$ mvn package
$ java -jar ./target/delivery-service-java-1.0-SNAPSHOT.jar
or using:
$ mvn exec:java
- After following the steps above, the project will bootup. The next section shows how to use the service.
The following examples uses the curl
library, if you don't have it installed yet, please install it before moving on.
This service is intented to be versioned, that requires each request having the correct version on headers.
By default, all requests receive the v1 version of the API. I encourage you to explicitly request this version via the Accept header:
Accept: application/vnd.delivery.v1
All API access is over HTTP, if you are on development enviroment, the API can be accessed from the http://localhost:8080/api/
. All data is sent and received as JSON.
There are three possible types of client errors on API calls that receive request bodies:
- Sending invalid JSON will result in a 400 Bad Request response.
HTTP/1.1 400 Bad Request
- Sending invalid fields will result in a 422 Unprocessable Entity response.
HTTP/1.1 422 Unprocessable Entity
- Requesting invalid entity will result in a 404 Not Found response.
HTTP/1.1 404 Not Found
Create a map resource with routes.
POST /maps
Parameters
Name | Type | Description |
---|---|---|
name | string | Required. The map name. |
routes | array | Required. An array of route objects. |
Route
Name | Type | Description |
---|---|---|
origin | string | Required. The origin name. |
destination | string | Required. The destination name. |
distance | integer | Required. The distance between origin and destination. |
Example
curl -XPOST -H "Accept: application/vnd.delivery.v1" -H "Accept: application/json" -H "Content-Type: application/json" -d '{ "name": "Mapa RS", "routes": [{ "origin": "Porto Alegre", "destination": "Torres", "distance": 100 }, { "origin": "Torres", "destination": "Tramandai", "distance": 50 }] }' http://localhost:8080/api/maps -v
Response
POST /api/maps HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.43.0
Accept: application/vnd.delivery.v1
Accept: application/json
Content-Type: application/json
HTTP/1.1 201 Created
Location: http://localhost:8080/api/maps/8
{
"id": 8,
"name": "Mapa RS",
"routes": [
{
"id": 16,
"origin": "Porto Alegre",
"destination": "Torres",
"distance": 100
},
{
"id": 17,
"origin": "Torres",
"destination": "Tramandai",
"distance": 50
}
]
}
Estimate the most econimical path for the given delivery information.
POST /maps/estimate_delivery
Parameters
Name | Type | Description |
---|---|---|
name | string | Required. The map name. |
origin | string | Required. The delivery origin. |
destination | string | Required. The delivery destination. |
liter_price | integer | Required. The fuel price per liter. |
autonomy | integer | Required. The autonomy of the transportation. |
Example
curl -XPOST -H "Accept: application/vnd.delivery.v1" -H "Accept: application/json" -H "Content-Type: application/json" -d '{ "name": "Mapa salvador", "origin": "Salvador", "destination": "Guarapari", "literPrice": 2.5, "autonomy": 10}' http://localhost:8080/api/maps/estimate_delivery -v
Response
POST /api/maps/estimate_delivery HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.43.0
Accept: application/vnd.delivery.v1
Accept: application/json
Content-Type: application/json
HTTP/1.1 200 OK
{
"routes": [
"salvador",
"ilheus",
"guarapari"
],
"cost": 162.5
}
List all maps resources with routes.
GET /maps
Example
curl -XGET -H "Accept: application/vnd.delivery.v1" -H "Accept: application/json" http://localhost:8080/api/maps -v
Reponse
GET /api/maps HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.43.0
Accept: application/vnd.delivery.v1
Accept: application/json
HTTP/1.1 200 OK
[
{
"id": 1,
"name": "Mapa Sp/Rj",
"routes": [
{
"id": 1,
"origin": "São Paulo",
"destination": "Rio de Janeiro",
"distance": 800
}
]
}
]