如何使用API​​自动执行报告

时间:2018-10-19 15:35:23

标签: python api

我想自动化我使用python在Namely中创建的报告,如何使用Namely API做到这一点?

注意:由于没有足够的代表,因此我无法添加Namely标签。

1 个答案:

答案 0 :(得分:0)

这是我制作的一个Python脚本,应该对此进行覆盖:

#Imports
import http.client
import json
import os
import time
import smtplib

#Constants
namelyDomain = "company.namely.com" #change this to your company's namely
csvName = "C:\\Path\\To_Write\\Your_CSV\\Report.csv" #absolute path to write csv
reportID = "0a12bac7-eac4-4bae-b18f-63ea3173gbb4" #report ID (find in URL)
APIkey = "yuIo4fH7f4z4dgabsSqXzxm9IMbW1ixLhjP0eh8jPuIo9vUI1nij9qZmG822al54" #get this from Namely>API>Personal Access Tokens
server = smtplib.SMTP()

#Variables
line = ""
columnCount = 0

#run report with get request
conn = http.client.HTTPSConnection(namelyDomain)
payload = "{}"
headers = { 'authorization': "Bearer " + APIkey }
conn.request("GET", "/api/v1/reports/" + reportID + ".json", payload,     headers)
res = conn.getresponse()
if(res.status != 200):
   print("failed to connect")
   exit()

data = res.read() #returns json object

#Delete if it exists (overwrite)
if os.path.exists(csvName):
   os.remove(csvName)

#make the csv
f = open(csvName,"w")

#get objects to loop from
dataHeader = dataRow = json.loads(data)

#Print headers to CSV
for data in dataHeader['reports'][0]['columns']:
   columnCount = columnCount + 1
   line = line + str(data['label']) + ","
line = line.rstrip(",")
f.write(line + chr(10))

#Print rows to CSV
for data in dataRow['reports'][0]['content']:
   line = '"'
   for ndx in range(0,columnCount):
      line = line + str(data[ndx]) + '","'
   line = line.replace("None","").replace('\u202d','').replace('\u202c','').rstrip('"').rstrip(",")
   f.write(line + chr(10))

只需替换:

namelyDomain与您公司的域名

csvName以及您要编写csv报告的位置的绝对路径

reportID以及您要生成的报告的ID

APIkey,带有来自的个人访问令牌

有用的链接:https://developers.namely.com/1.0/reports/show-report