HelloAmshterdam
4 years ago in Go
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
type tix struct {
Count int `json:"count"`
MaxPages int `json:"maxPages"`
Page int `json:"page"`
Tickets []tickets `json:"tickets"`
}
type tickets struct {
ID int `json:"id"`
HappinessRating interface{} `json:"happinessRating"`
HasAttachments bool `json:"hasAttachments"`
InboxID int `json:"inboxId"`
MergedToID int `json:"mergedToId"`
ReviewStatus string `json:"reviewStatus"`
Preview string `json:"preview"`
Priority string `json:"priority"`
Source string `json:"source"`
SpamScore int `json:"spam_score"`
Status string `json:"status"`
Subject string `json:"subject"`
Type string `json:"type"`
IsRead bool `json:"isRead"`
NumThreads int `json:"numThreads"`
NumActiveTasks int `json:"numActiveTasks"`
NumCompletedTasks int `json:"numCompletedTasks"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt interface{} `json:"deletedAt"`
DeletedByUserID interface{} `json:"deletedByUserId"`
Customer customer `json:"customer"`
ResponseTimes responseTimes `json:"responseTimes"`
AssignedTo assignedTo `json:"assignedTo"`
Tags []tags `json:"tags"`
CustomFields []customfields `json:"customFields"`
}
type customer struct {
ID int `json:"id"`
Email string `json:"email"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
}
type responseTimes struct {
AverageResponseTime int `json:"averageResponseTime"`
FirstResponseTime int `json:"firstResponseTime"`
ResponseCount int `json:"responseCount"`
ResolutionTime int `json:"resolutionTime"`
}
type assignedTo struct {
ID int `json:"id"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
}
type customfields struct {
ID int `json:"id"`
AgentLabel string `json:"agentLabel"`
CustomerLabel string `json:"customerLabel"`
Description string `json:"description"`
DefaultValue string `json:"defaultValue"`
Placeholder string `json:"placeholder"`
DisplayOrder int `json:"displayOrder"`
Kind string `json:"kind"`
Options []options `json:"options"`
Data string `json:"data"`
}
type options struct {
ID int `json:"id"`
Name string `json:"name"`
DisplayOrder int `json:"displayOrder"`
Selected bool `json:"selected"`
}
type tags struct {
ID int `json:"id"`
Name string `json:"name"`
}
const (
url string = "https://digitalcrew.teamwork.com/desk/v1/tickets/search.json?"
username string = "vjR2bMaFbEWE4G2JspXevAMWWXrmIdwhBOpvduHcyCioX8aFAX"
passw string = ""
)
var (
updated string = "lastUpdated=2019-08-01T18:40:00.000Z"
inboxes string = "inboxes[]=2035,1,62,61,2346,2370,55,38,49,43,41"
agents string = "assignedTo=164160,196551,191158,196550,198184,202179,202176,202181,202903,206337,206338,210334,216248,216271,216272,216274,216288,216289,216291,216292,181997,202180,34270,34270,131727,188450,213656"
pageSize string = "pageSize=5"
sortby string = "sortBy=CreatedAt"
sortdir string = "sortDir=asc"
params string
pagesLoop = 2
)
func main() {
p := []string{}
p = append(p, updated, inboxes, agents, pageSize, sortby, sortdir)
for x := 1; x <= pagesLoop; x++ {
y := strconv.Itoa(x) // convert x to a string so we can use it in the query
z := "page=" + y // z = page=y (which is x)
p = append(p, z)
params = strings.Join(p, "&") // Combine all variables above seperated by &
client := &http.Client{}
req, err := http.NewRequest("GET", url+params, nil)
req.SetBasicAuth(username, passw)
resp, err := client.Do(req)
if err != nil {
fmt.Println("Boo! Failed to load Page." + y)
log.Fatal(err)
}
i := 6
copy(p[i:], p[i+1:])
p[len(p)-1] = ""
p = p[:len(p)-1]
bytes, _ := ioutil.ReadAll(resp.Body)
var data tix
json.Unmarshal([]byte(bytes), &data)
csvExport(test)
pagesLoop = data.MaxPages
x = data.Page
/*
for _, tix := range data.Tickets {
// Remove Merged Into tickets
fmt.Printf("%v |\t\n%v |\t\n", tix.ID, tix.AssignedTo.FirstName)
}
*/
if pagesLoop <= 1 {
break
}
}
}
func csvExport(x *[]tickets) {
fmt.Println("========")
fmt.Println(x.Tickets)
fmt.Println("========")
file, err := os.OpenFile("test.csv", os.O_CREATE|os.O_WRONLY, 0777)
defer file.Close()
if err != nil {
os.Exit(1)
}
csvWriter := csv.NewWriter(file)
columnHeadings := []string{"Ticket ID", "InboxID", "AgentName", "Happiness Ratings", "CustomerID", "Tags", "CreatedAt", "ThreadNumber"}
strWrite := [][]string{columnHeadings, x.Tickets.ID}
csvWriter.WriteAll(strWrite)
}