cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

Python & Key Value Datatabse: Add Variable

Python & Key Value Datatabse: Add Variable

Reto
New Poster

Hi

I am trying to add a variable to the datalayer using the node red example as a staring point. However, I would like to implement the request in Python instead.

import requests

class getData:

    def __init__(self):
        self.data = []

    Auth_URL = "https://localhost/identity-manager/api/v2/auth/token"
    ServerVar_URL = "https://localhost/automation/api/v2/nodes/" + "samples/kvd/implicit/xyz/abcd"

    def makeVarables(opcVar: dict, user: str, pwd: str):
        # Make the datalayer entries. Key Value database app must be installed!

        # Authorisation
        session = requests.Session()
        session.verify = False
        print(getData.ServerVar_URL)
        response = session.post(getData.Auth_URL, json={"name": user, "password": pwd}).json()
        token = response['access_token']
        data_token = {"Authorization": f"Bearer {token}"}
        payload = "{'type':'int32','value': 5}"
        r = session.put(url=getData.ServerVar_URL, data={'type':'int32','value':0}, headers=data_token)
        print('Status Code=%s'%(r.status_code))
        print(r.json())

 

Unfortunately, I can not figure out the correct syntax of the data to be sent. I get the following response:

Status Code=400
{'type': 'about:blank', 'title': 'DL_TYPE_MISMATCH', 'status': 400, 'detail': "[json.exception.parse_error.101] parse error at line 1, column 2: syntax error while parsing value - invalid literal; last read: 'ty'", 'instance': 'samples/kvd/implicit/xyz/abcd', 'severity': 'Error'}


Thanks for your help
Reto

1 REPLY 1

Sgilk
Frequent Contributor

Hi @Reto ,

Modified your example slightly below. The data argument of requests.put() needs to be passed as a JSON string. I just used json.dumps() from the json library.

import requests
import json

class getData:

    def __init__(self):
        self.data = []

    Auth_URL = "https://localhost/identity-manager/api/v2/auth/token"
    ServerVar_URL = "https://localhost/automation/api/v2/nodes/" + "samples/kvd/implicit/xyz/g"

    def makeVarables(user: str, pwd: str):
        # Make the datalayer entries. Key Value database app must be installed!

        # Authorisation
        session = requests.Session()
        session.verify = False
        print(getData.ServerVar_URL)
        response = session.post(getData.Auth_URL, json={"name": user, "password": pwd}).json()
        token = response['access_token']
        data_token = {"Authorization": f"Bearer {token}"}
        payload = {
            "type": "int32", 
            "value": 315
        }
        r = session.put(url=getData.ServerVar_URL, data=json.dumps(payload), headers=data_token)
        print('Status Code=%s' % (r.status_code))
        print(r.json())

test = getData
test.makeVarables('boschrexroth', 'boschrexroth')
exit(-1)
Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist