How to Automate a Report from Namely using API

369 views Asked by At

I want to automate a report I created in Namely using python, how can I do this with the Namely API?

1

There are 1 answers

0
bogus On BEST ANSWER

Here's a python script I made that should cover it:

#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))

Just replace:

namelyDomain with your company's namely domain

csvName with the absolute path of where you want to write the csv report

reportID with the id of the report you want to generate

APIkey with the personal access token from namely

Useful Link: https://developers.namely.com/1.0/reports/show-report