Skip to content

Create Product

Daniel Cronqvist edited this page Dec 11, 2020 · 5 revisions

Create Product - HTTP POST

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

Example cURL POST:

$ curl -X POST -d '{ "_id": "IKEA-BILLY", "kg_per_unit": 25, "unit": "piece", "type": "product", "tags": ["Furniture"], "type_description": "Assembly", "prod_name": "IKEA Billy Shelf", "benchmark": { "self_impact": { "co2": 5.1, "measurement_error": 0.05, "energy_sources": ["Solar", "Nuclear", "Wind"] }, "date": "2020-12-01-00:05:32", "sub_products": [] } }' -H "Content-Type: application/json" https://co2.dcronqvist.se/products/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': ["str"],  # e.g. "IKEA-BILLY-5"
    'type': "product",  # will always be product
    'tags': ["list:str"],  # e.g. ["furniture", "shelf", "wood"]
    'type_description': ["str"],  # "Assembly"
    'prod_name': ["str"],  # e.g. "Billy Shelf"
    'kg_per_unit': ["float", "int"],  # e.g. 12.81
    'unit': ["str"],
    'benchmark': {
        "self_impact": {
            "co2": ["float", "int"],  # e.g. 292.62
            "measurement_error": ["float"],  # e.g. 0.05 -> 5% error margin
            "energy_sources": ["list:str"]  # e.g. ["Solar", "Nuclear", "Wind"]
        },
        "date": ["str"],  # e.g. "2020-11-30-22:50:51"
        "sub_products": [
            {
                "product": ["str"],
                "unit_amount": ["float", "int"],
                "transport": ["int"]
            }
        ]
    }
}

Responses

200 OK

On successfully creating a new product, you will be met with the exact same object as you requested, together with a 200 OK.

{
    "response": {
        '_id': ["str"],  # e.g. "IKEA-BILLY-5"
        'type': "product",  # will always be product
        'tags': ["list:str"],  # e.g. ["furniture", "shelf", "wood"]
        'type_description': ["str"],  # "Assembly"
        'prod_name': ["str"],  # e.g. "Billy Shelf"
        'kg_per_unit': ["float", "int"],  # e.g. 12.81
        'unit': ["str"],
        'benchmark': {
            "self_impact": {
                "co2": ["float", "int"],  # e.g. 292.62
                "measurement_error": ["float"],  # e.g. 0.05 -> 5% error margin
                "energy_sources": ["list:str"]  # e.g. ["Solar", "Nuclear", "Wind"]
            },
            "date": ["str"],  # e.g. "2020-11-30-22:50:51"
            "sub_products": [
                {
                    "product": ["str"],
                    "unit_amount": ["float", "int"],
                    "transport": ["int"]
                }
            ]
        }
    },
    "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 '_id'",
    "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
}