代码之家  ›  专栏  ›  技术社区  ›  bogus

如何使用API自动生成报表

  •  0
  • bogus  · 技术社区  · 7 年前

    我想用python自动化我在either中创建的一个报表,如何用eitherapi实现这一点?

    Namely 因为我没有足够的代表。

    1 回复  |  直到 7 年前
        1
  •  1
  •   bogus    7 年前

    下面是我编写的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