Webservices example

Hello, yes spending (a lot of) time with ST:

this is an working example for appending row from command line:

import requests
import json
import sys
import os
from seatable_api import Base, context

SERVER_URL=os.environ['SERVER_URL']
USERNAME=os.environ['USERNAME']
PASSWORD=os.environ['PASSWORD']
WORKSPACE=os.environ['WORKSPACE']
BASE=os.environ['BASE']
TABLE=os.environ['TABLE']

args=sys.argv
row={"#": "" ,
     "hostname": args[1],
     "result": args[2],
     "comments": args[3],
     "report": "```" + args[4] + "\n```"
     }

## get API TOKEN
payload = {'username':USERNAME, 'password': PASSWORD}
ret = requests.post(SERVER_URL + '/api2/auth-token/',data=payload)

auth_token=json.loads(ret.content)
auth_token=auth_token["token"]
#print(auth_token)

## get workspace id
payload = {}
headers1={'Accept':'application/json; charset=utf-8; indent=4','Authorization':'Token ' + auth_token}
ret = requests.get(SERVER_URL + '/api/v2.1/workspaces/?        detail=false',data=payload,headers=headers1)
workspaces=json.loads(ret.content)
#print(workspaces["workspace_list"])
for k in workspaces["workspace_list"]:
    if k["name"]==WORKSPACE:
        workspaceid=k["id"]

## get base token
payload = {}
headers1={'Accept':'application/json; charset=utf-8; indent=4','Authorization':'Token ' + auth_token}
ret = requests.get(SERVER_URL + '/api/v2.1/workspace/' + str(workspaceid) + '/dtable/' + BASE + '/access-token/',data=payload,headers=headers1)
base_token = json.loads(ret.content)
#dtable_uuid = base_token["dtable_uuid"]
#dtable_uuid=dtable_uuid.replace("-","")
base_token = base_token["access_token"]

## base auth
base = Base(base_token, SERVER_URL)
base.auth()

# get rows
#rows = base.list_rows(TABLE)
#print(rows)


## append row


ret=base.append_row(TABLE, row)
print(ret)

Thanks for the example - I’m no expert in Python but I think this looks good!

Just for anyone who’s interested: We have a programming manual for Python, too:
Introduction - SeaTable Scripts Programming Manual

1 Like