· 5 years ago · Aug 19, 2020, 05:06 AM
1/*
2 * TrelloAPI.cs
3 * Interact directly with the Trello API using MiniJSON and uploads cards.
4 *
5 * Original by bfollington
6 * https://github.com/bfollington/Trello-Cards-Unity
7 *
8 * by Adam Carballo under GPLv3 license.
9 * https://github.com/AdamEC/Unity-Trello
10 */
11
12using System.Collections.Generic;
13using UnityEngine;
14using MiniJSON;
15
16namespace Trello {
17 public class TrelloAPI {
18
19 private string _token;
20 private string _key;
21 private List<object> _boards;
22 private List<object> _lists;
23 private const string _memberBaseUrl = "https://api.trello.com/1/members/me";
24 private const string _boardBaseUrl = "https://api.trello.com/1/boards/";
25 private const string _cardBaseUrl = "https://api.trello.com/1/cards/";
26 private string _currentBoardId = null;
27 private string _currentListId = null;
28
29
30 /// <summary>
31 /// Generate new Trello API instance.
32 /// </summary>
33 /// <param name="key">Trello API key, keep it private.</param>
34 /// <param name="token">Trello API token, keep it private.</param>
35 public TrelloAPI(string key, string token) {
36
37 _key = key;
38 _token = token;
39 }
40
41#pragma warning disable CS0618 // Type or member is obsolete
42 /// <summary>
43 /// Checks if a WWW object returned an error, and if so throws an exception.
44 /// </summary>
45 /// <param name="errorMessage">Error message to display.</param>
46 /// <param name="www">The WWW request object.</param>
47 private void CheckWwwStatus(string errorMessage, WWW www)
48 {
49 if (!string.IsNullOrEmpty(www.error)) {
50 throw new TrelloException(errorMessage + ": " + www.error);
51 }
52 }
53
54 /// <summary>
55 /// Download the list of available boards for the user and store them.
56 /// </summary>
57 /// <returns>Downloaded boards.</returns>
58 public List<object> PopulateBoards() {
59
60 _boards = null;
61 WWW www = new WWW(string.Format("{0}?key={1}&token={2}&boards=all", _memberBaseUrl, _key, _token));
62
63 // Wait for request to return
64 while (!www.isDone) {
65 CheckWwwStatus("The Trello servers did not respond.", www);
66 }
67
68 var dict = Json.Deserialize(www.text) as Dictionary<string,object>;
69
70 _boards = (List<object>)dict["boards"];
71 return _boards;
72 }
73
74 /// <summary>
75 /// Sets the given board to search for lists in.
76 /// </summary>
77 /// <param name="name">Name of the board.</param>
78 public void SetCurrentBoard(string name) {
79
80 if (_boards == null) {
81 throw new TrelloException("There are no boards available. Either the user does not have access to a board or PopulateBoards() wasn't called.");
82 }
83
84 for (int i = 0; i < _boards.Count; i++) {
85 var board = (Dictionary<string, object>)_boards[i];
86 if ((string)board["name"] == name) {
87 _currentBoardId = (string)board["id"];
88 return;
89 }
90 }
91
92 _currentBoardId = null;
93 throw new TrelloException("A board with the name " + name + " was not found.");
94 }
95
96 /// <summary>
97 /// Download all the lists of the selected board and store them.
98 /// </summary>
99 /// <returns>Downloaded list.</returns>
100 public List<object> PopulateLists() {
101
102 _lists = null;
103
104 if (_currentBoardId == null) {
105 throw new TrelloException("Cannot retreive the lists, there isn't a selected board yet.");
106 }
107
108 WWW www = new WWW(string.Format("{0}{1}?key={2}&token={3}&lists=all", _boardBaseUrl, _currentBoardId, _key, _token));
109
110 // Wait for request to return
111 while (!www.isDone) {
112 CheckWwwStatus("Connection to the Trello servers was not possible", www);
113 }
114
115 var dict = Json.Deserialize(www.text) as Dictionary<string,object>;
116
117 _lists = (List<object>)dict["lists"];
118 return _lists;
119 }
120
121 /// <summary>
122 /// Sets the given list to upload cards to.
123 /// </summary>
124 /// <param name="name">Name of the list.</param>
125 public void SetCurrentList(string name) {
126
127 if (_lists == null) {
128 throw new TrelloException("There are no lists available. Either the board does not contain lists or PopulateLists() wasn't called.");
129 }
130
131 for (int i = 0; i < _lists.Count; i++) {
132 var list = (Dictionary<string, object>)_lists[i];
133 if ((string)list["name"] == name) {
134 _currentListId = (string)list["id"];
135 return;
136 }
137 }
138
139 _currentListId = null;
140 throw new TrelloException("A list with the name " + name + " was not found.");
141 }
142
143 /// <summary>
144 /// Returns the selected Trello list id.
145 /// </summary>
146 /// <returns>The list id.</returns>
147 public string GetCurrentListId() {
148
149 if (_currentListId == null) {
150 throw new TrelloException("A list has not been selected. Call SetCurrentList() first.");
151 }
152 return _currentListId;
153 }
154
155 /// <summary>
156 /// Given an exception object, a TrelloCard is created and populated with the relevant information from the exception. This is then uploaded to the Trello server.
157 /// </summary>
158 /// <returns>The exception card.</returns>
159 /// <param name="e">E.</param>
160 /*public TrelloCard uploadExceptionCardd(Exception e) {
161
162 TrelloCard card = new TrelloCard();
163 card.name = e.GetType().ToString();
164 card.due = DateTime.Now.ToString();
165 card.desc = e.Message;
166 card.idList = _currentListId;
167
168 return UploadCard(card);
169 }*/
170
171 /// <summary>
172 /// Uploads a given TrelloCard object to the Trello server.
173 /// </summary>
174 /// <returns>Trello card uploaded.</returns>
175 /// <param name="card">Trello card to upload.</param>
176 public TrelloCard UploadCard(TrelloCard card) {
177
178 WWWForm post = new WWWForm();
179 post.AddField("name", card.name);
180 post.AddField("desc", card.desc);
181 post.AddField("due", card.due);
182 post.AddField("idList", card.idList);
183 post.AddField("urlSource", card.urlSource);
184 if (card.fileSource != null && card.fileName != null) {
185 post.AddBinaryData("fileSource", card.fileSource, card.fileName);
186 }
187
188 WWW www = new WWW(string.Format("{0}?key={1}&token={2}", _cardBaseUrl, _key, _token), post);
189
190 // Wait for request to return
191 while (!www.isDone) {
192 CheckWwwStatus("Could not upload the Trello card.", www);
193 }
194
195 Debug.Log("Trello card sent!");
196 return card;
197 }
198#pragma warning restore CS0618 // Type or member is obsolete
199 }
200}