Skip to content

Instantly share code, notes, and snippets.

@nickborysov
Created May 29, 2018 13:38
Show Gist options
  • Select an option

  • Save nickborysov/5395d669be4b943de1c5e47e08f92494 to your computer and use it in GitHub Desktop.

Select an option

Save nickborysov/5395d669be4b943de1c5e47e08f92494 to your computer and use it in GitHub Desktop.
package trello
import "encoding/json"
type Cards []Card
func UnmarshalCards(data []byte) (Cards, error) {
var r Cards
err := json.Unmarshal(data, &r)
return r, err
}
func (r *Cards) Marshal() ([]byte, error) {
return json.Marshal(r)
}
type Card struct {
ID string `json:"id"`
CheckItemStates interface{} `json:"checkItemStates"`
Closed bool `json:"closed"`
DateLastActivity string `json:"dateLastActivity"`
Desc string `json:"desc"`
DescData interface{} `json:"descData"`
IDBoard string `json:"idBoard"`
IDList string `json:"idList"`
IDMembersVoted []interface{} `json:"idMembersVoted"`
IDShort int64 `json:"idShort"`
IDAttachmentCover interface{} `json:"idAttachmentCover"`
IDLabels []interface{} `json:"idLabels"`
ManualCoverAttachment bool `json:"manualCoverAttachment"`
Name string `json:"name"`
Pos int64 `json:"pos"`
ShortLink string `json:"shortLink"`
Badges Badges `json:"badges"`
DueComplete bool `json:"dueComplete"`
Due interface{} `json:"due"`
IDChecklists []string `json:"idChecklists"`
IDMembers []interface{} `json:"idMembers"`
Labels []interface{} `json:"labels"`
ShortURL string `json:"shortUrl"`
Subscribed bool `json:"subscribed"`
URL string `json:"url"`
}
type Badges struct {
Votes int64 `json:"votes"`
AttachmentsByType AttachmentsByType `json:"attachmentsByType"`
ViewingMemberVoted bool `json:"viewingMemberVoted"`
Subscribed bool `json:"subscribed"`
Fogbugz string `json:"fogbugz"`
CheckItems int64 `json:"checkItems"`
CheckItemsChecked int64 `json:"checkItemsChecked"`
Comments int64 `json:"comments"`
Attachments int64 `json:"attachments"`
Description bool `json:"description"`
Due interface{} `json:"due"`
DueComplete bool `json:"dueComplete"`
}
type AttachmentsByType struct {
Trello Trello `json:"trello"`
}
type Trello struct {
Board int64 `json:"board"`
Card int64 `json:"card"`
}
package trello
import "encoding/json"
func UnmarshalChecklist(data []byte) (Checklist, error) {
var r Checklist
err := json.Unmarshal(data, &r)
return r, err
}
func (r *Checklist) Marshal() ([]byte, error) {
return json.Marshal(r)
}
type Checklist struct {
ID string `json:"id"`
Name string `json:"name"`
IDBoard string `json:"idBoard"`
IDCard string `json:"idCard"`
Pos int64 `json:"pos"`
CheckItems []CheckItem `json:"checkItems"`
}
type CheckItem struct {
State string `json:"state"`
IDChecklist string `json:"idChecklist"`
ID string `json:"id"`
Name string `json:"name"`
NameData interface{} `json:"nameData"`
Pos int64 `json:"pos"`
}
package trello
import (
"fmt"
"io/ioutil"
"net/http"
)
import . "../model"
var baseUrl = "https://api.trello.com/1/"
var boardId = "boardId"
var questionListId = "questionListId"
var key = "key"
var token = "token"
var authParams = "key=" + key + "&token=" + token
func getQuestionsFromTrello() []Question {
cards := getCards(questionListId)
var questions []Question = make([]Question, 0)
for _, card := range cards {
question := getQuestion(card)
questions = append(questions, question)
}
return questions
}
func getQuestion(card Card) Question {
question := Question{card.ID, card.Name, "0", make(Answers, 0)}
answers := getAnswers(question.Id, card.IDChecklists[0])
question.Answers = answers
return question
}
func getAnswers(questionId string, checklistId string) Answers {
checklist := getChecklist(checklistId)
var answers Answers
for _, item := range checklist.CheckItems {
is_correct := item.State == "complete"
answer := Answer{item.ID, questionId, item.Name, "", is_correct}
answers = append(answers, answer)
}
return answers
}
func getChecklist(checklistId string) Checklist {
url := baseUrl + "/checklists/" + checklistId + "?" + authParams
body := fetchBody(url)
checlist, err := UnmarshalChecklist(body)
if err != nil {
panic(err)
}
return checlist
}
func fetchBody(url string) []byte {
resp, err := http.Get(url)
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Failed to get bytes from body")
}
return body
}
func getCards(listId string) Cards {
url := baseUrl + "/lists/" + listId + "/cards?" + authParams
body := fetchBody(url)
cards, err := UnmarshalCards(body)
if err != nil {
fmt.Println("Failed to get model from body")
}
return cards
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment