-
-
Save petehartnett/eaed9308af2d5edd05d157137164343c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json,requests | |
from pprint import pprint | |
def getTableRecords(instance,table,limit,offset,key,direction = 'asc'): | |
########################FUNCTION DESCRIPTION####################### | |
#This actually makes the api calls to get data from a table. | |
#returns the data and the count of records NOT JUST THE DATA | |
################################################################### | |
url = 'https://{}/api/v3/tables/{}}/records?limit={}&offset={}&sortBy=_sequenceNumber&sortDir={}&includeTotalCount=true&filterAggregator=all'.format(instance,table,str(limit),str(offset),direction) | |
headers = { | |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0", | |
"Accept-Encoding": "*", | |
"Connection": "keep-alive", | |
"accept": "application/json", | |
"Authorization": key, | |
"Content-Type": "application/json", | |
"filters.0.field":"ezwlf_linebeing_audited", | |
"filters.0.functionType":"equal", | |
"filters.0.arg":"Line 2" | |
} | |
r = requests.get(url, headers=headers) | |
try: | |
totalCount = int(r.headers['x-total-count']) | |
r = r.json() | |
#print("here",r) | |
return(r,totalCount) | |
except: | |
r = r.json() | |
return(r,0) | |
def addToTable(instance,table,data,key): | |
########################FUNCTION DESCRIPTION####################### | |
#This creates a record in a given table | |
################################################################### | |
url = 'https://{}}/api/v3/tables/{}}/records'.format(instance,table) | |
payload = data | |
headers = {"accept": "application/json", "Authorization": key,"Content-Type": "application/json"} | |
r = requests.post(url, data=json.dumps(payload), headers=headers) | |
def getColumns(instance,table,key): | |
########################FUNCTION DESCRIPTION####################### | |
#This function just returns each column in your table. Using this to get the unique keys | |
################################################################### | |
url = 'https://{}/api/v3/tables/{}'.format(instance,table) | |
headers = {"accept": "application/json", "Authorization": key,"Content-Type": "application/json"} | |
r = requests.get(url,headers=headers) | |
data = r.json() | |
columns = [] | |
for column in data['columns']: | |
if not column['hidden']: | |
columns.append({"name":column['name'],"type":column['dataType']['type']}) | |
return columns | |
def updateRecord(instance,table,data,key): | |
########################FUNCTION DESCRIPTION####################### | |
#This creates a record in a given table | |
################################################################### | |
url = 'https://{}/api/v3/tables/{}/records/{}'.format(instance,table,data['id']) | |
payload = data | |
headers = {"accept": "application/json", "Authorization": key,"Content-Type": "application/json"} | |
r = requests.put(url, data=json.dumps(payload), headers=headers) | |
print(r.content) | |
exampleData = { | |
'id': 'WI-0005', | |
'kzmxn_expected_time': 17.0, | |
} | |
KEY = 'Basic YXBpa2V5LjJfdXh6WTlhZDc1S3RYZWd...==' | |
data = updateRecord('petehartnett.tulip.co','RZdpY9NwDMPskoEE5',exampleData,KEY) | |
#print(getColumns('petehartnett.tulip.co','gECumneEAPwbLtbj5',peteKey)) | |
#print(records) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64,random,string | |
from apiExample import addToTable | |
def encodeImage(filepath): | |
prefixLookup = { | |
'/' : b'data:image/jpg;base64,', | |
'i' : b'data:image/png;base64,', | |
'R' : b'data:image/gif;base64,', | |
'U' : b'data:image/webp;base64,', | |
} | |
with open(filepath, "rb") as img_file: | |
b64_string = base64.b64encode(img_file.read()) | |
firstLetter = (b64_string.decode('ascii')[0]) | |
if firstLetter in prefixLookup.keys: | |
prefix = prefixLookup[firstLetter] | |
else: | |
prefix = b'data:image/jpg;base64,' | |
b64_string =prefix + b64_string | |
return b64_string.decode('ascii') | |
def getRecId(): | |
letters = string.ascii_letters | |
id = ( ''.join(random.choice(letters) for i in range(10)) ) | |
return id | |
id = getRecId() | |
data = encodeImage('img.png') | |
record = { | |
'id' : id, | |
'iyygy_example_img':data | |
} | |
addToTable("petehartnett.tulip.co","gECumneEAPwbLtbj5",record,"Basic .....") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment