Skip to content

Create Transport

Daniel Cronqvist edited this page Dec 11, 2020 · 1 revision

Create Transport - HTTP POST

Endpoint: https://co2.dcronqvist.se/transports/create

Example cURL POST:

$ curl -X POST -d '{ "_id": 100, "type": "transport", "type_description": "train", "impact": {"co2_per_kg_km":0.005, "measurement_error": 0.11, "distance_travelled": 14000, "energy_sources":["nuclear", "coal"]}, "start_lat": 27.19, "start_lon": 113.69, "end_lat": 27.49, "end_lon": 119.66, "max_weight": 3219}' -H "Content-Type: application/json" 127.0.0.1:5000/transports/create

Expected payload format (EPF)

The following rules are applied to all keys in the payload using the EPF as a sample:

  1. Keys with arrays of specified types specify which types that are allowed for that specific key.
  2. Keys with specific values only allow that specific value in the payload.
  3. Keys that contain objects are recursively checked using rule 1 and 2.
{
    '_id': ["int"],  # e.g. 1
    'type': "transport",  # will always be transport
    'type_description': ["str"],  # e.g. "train"
    'impact': {
        'co2_per_kg_km': ['int', 'float'],
        'measurement_error': ['int', 'float'],
        'distance_travelled': ['int', 'float'],
        'energy_sources': ['list:str'],
    },
    'start_lat': ["float", "int"],
    'start_lon': ["float", "int"],
    'end_lat': ["float", "int"],
    'end_lon': ["float", "int"],
    'max_weight': ["float", "int"]
}

Responses

200 OK

If it succeeds in creating the specified transport then it will return the queried transport, which is exactly what gets inserted into the database.

{
    "response": { 
        "_id": 100, 
        "type": "transport", 
        "type_description": "train", 
        "impact": {
            "co2_per_kg_km":0.005, 
            "measurement_error": 0.11, 
            "distance_travelled": 14000, 
            "energy_sources":["nuclear", "coal"]
        }, 
        "start_lat": 27.19, 
        "start_lon": 113.69, 
        "end_lat": 27.49, 
        "end_lon": 119.66, 
        "max_weight": 3219
    },
    "status_code": 200,
    "status": "200 OK"
}

400 Bad Request

If you receive a 400 BAD REQUEST, you either have missed a key or have specified a disallowed type for a specific key.

{
    "response": "ERROR: Missing key 'distance_travelled'",
    "status_code": 400,
    "status": "400 BAD REQUEST"
}

405 Method Not Allowed

If you receive a 405 METHOD NOT ALLOWED, you are trying to access this POST endpoint by using some other HTTP method than POST.

{
    "response": null, 
    "status": "405 METHOD NOT ALLOWED", 
    "status_code": 405
}