# Python

## HTTP Request

### Example Script

```python
import requests

# The Webhook URL for your API Product on Datacake
url = "https://api.datacake.co/integrations/api/85dd4a6f-f587-4e5d-a522-c871caaba059/"

# Serial for device identification on Datacake
serial = "pythondevice001"

# Some random sensor data (replace with real data)
temperature = 16.04
humidity = 53.23
pressure = 1015
light = 734
power_status = True
lat = 51.4356
lon = 6.953

# Create a dictionary containing payload for webhook call
payload = {
    "s": serial,
    "t": temperature,
    "h": humidity,
    "p": pressure,
    "l": light,
    "ps": power_status,
    "loc": "(" + str(lat) + "," + str(lon) + ")"
}

# Do a post request on the webhook including payload data
r = requests.post(url, json=payload)
```

### Decoder on Datacake

Below you will find the corresponding HTTP Payload decoder for the above Python Script that perform as HTTP Post Request.

```javascript
function Decoder(request) {

    // Parse JSON string in request body to real json
    var payload = JSON.parse(request.body);

    // Extract serial number in payload for device routing
    var serial = payload.s;
    
    // Return an array of dictionaries to Datacake
    // Each dictionary contains device serial, database identifier and value
    return [
        {
            device: serial, // Serial number of device
            field: "TEMPERATURE", // Identifier of database field
            value: payload.t // Actual data value for field
        },
        {
            device: serial,
            field: "HUMIDITY",
            value: payload.h
        },
        {
            device: serial,
            field: "PRESSURE",
            value: payload.p
        },
        {
            device: serial,
            field: "LIGHT",
            value: payload.l
        },
        {
            device: serial,
            field: "POWER_STATUS",
            value: payload.ps
        },
        {
            device: serial,
            field: "LOCATION",
            value: payload.loc
        },
    ];
}
```

### Routing via Serial Number

#### Python

```python
# Serial for device identification on Datacake
serial = "pythondevice001"
```

#### Datacake Decoder

```javascript
// Extract serial number in payload for device routing
var serial = payload.s; // "pythondevice001"

// Return an array of dictionaries to Datacake
// Each dictionary contains device serial, database identifier and value
return [
    {
        device: serial, // Serial number of device "pythondevice001"
        field: ...
        value: ...
    },
    ...
]
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.datacake.de/guides/python.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
