-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClient.cs
More file actions
240 lines (221 loc) · 6.87 KB
/
Client.cs
File metadata and controls
240 lines (221 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Runtime.InteropServices;
using AutoMapper;
using LeanKit.API.Client.Library;
using LeanKit.API.Client.Library.TransferObjects;
namespace LeanKit.API.ExcelHelper
{
[ClassInterface(ClassInterfaceType.AutoDual)]
public class Client
{
private ILeanKitApi _api;
private readonly ObjectCache _cache = MemoryCache.Default;
public Client()
{
Mappings.Initialize();
}
private void InitCheck()
{
if (_api == null) throw new ArgumentException("You must first call Initialize() to set account authentication");
}
public bool Initialize(string accountName, string emailAddress, string password)
{
try
{
var lkFactory = new LeanKitClientFactory();
var lkAuth = new LeanKitBasicAuth
{
Hostname = accountName,
Username = emailAddress,
Password = password
};
_api = lkFactory.Create(lkAuth);
// Test authentication
var boards = _api.GetBoards();
if (boards == null) return false;
_cache.Set("boards", boards.ToArray(), DateTimeOffset.Now.AddMinutes(5));
return true;
}
catch (Exception)
{
_api = null;
return false;
}
}
public LeanKitBoard GetBoard(long boardId)
{
InitCheck();
var board = _api.GetBoard(boardId);
return Mapper.Map<LeanKitBoard>(board);
}
public LeanKitBoardMetadata GetBoardMetadata(long boardId)
{
InitCheck();
var cacheKey = "boardmetadata-" + boardId;
var cachedMetadata = _cache.Get(cacheKey);
BoardIdentifiers identifiers;
if (cachedMetadata != null)
{
identifiers = (BoardIdentifiers) cachedMetadata;
}
else
{
identifiers = _api.GetBoardIdentifiers(boardId);
_cache.Set(cacheKey, identifiers, DateTimeOffset.Now.AddMinutes(5));
}
return Mapper.Map<LeanKitBoardMetadata>(identifiers);
}
public LeanKitBoardListItem[] GetBoards()
{
InitCheck();
var cachedBoards = _cache.Get("boards");
BoardListing[] boards;
if (cachedBoards != null)
{
boards = (BoardListing[]) cachedBoards;
}
else
{
Console.WriteLine("Getting boards from API");
boards = _api.GetBoards().ToArray();
_cache.Set("boards", boards, DateTimeOffset.Now.AddMinutes(5));
}
return Mapper.Map<LeanKitBoardListItem[]>(boards);
}
public LeanKitCard GetCard(long boardId, long cardId)
{
InitCheck();
var c = _api.GetCard(boardId, cardId);
return Mapper.Map<LeanKitCard>(c);
}
public LeanKitCard GetCardByExternalId(long boardId, string cardId)
{
InitCheck();
var c = _api.GetCardByExternalId(boardId, cardId);
return Mapper.Map<LeanKitCard>(c);
}
public long AddCardComment(long boardId, long cardId, string comment)
{
InitCheck();
var c = new Comment {Text = comment};
return _api.PostComment(boardId, cardId, c);
}
public long AddCard(long boardId, string title, string description, int priority, int size, int index, long laneId,
string cardType, string customIcon, long parentCardId, bool isBlocked, string blockReason,
string externalCardId, string externalSystemName, string externalSystemUrl,
string startDate, string dueDate, string tags, string assignedUsers)
{
InitCheck();
var card = new Card
{
Title = title,
Description = description,
Priority = priority,
Index = index,
LaneId = laneId,
IsBlocked = isBlocked,
BlockReason = blockReason,
ExternalCardID = externalCardId,
ExternalSystemName = externalSystemName,
ExternalSystemUrl = externalSystemUrl,
StartDate = startDate,
DueDate = dueDate,
Tags = tags
};
if (size > 0) card.Size = size;
if (parentCardId > 0) card.ParentCardId = parentCardId;
card.AssignedUserIds = MapAssignedUsers(boardId, assignedUsers);
card.TypeId = MapCardType(boardId, cardType);
card.ClassOfServiceId = MapCustomIcon(boardId, customIcon);
var result = _api.AddCard(boardId, card);
return result.CardId;
}
public long UpdateCard(long boardId, long cardId, string title, string description, int priority, int size, int index,
long laneId, string cardType, string customIcon, long parentCardId, bool isBlocked, string blockReason,
string externalCardId, string externalSystemName, string externalSystemUrl,
string startDate, string dueDate, string tags, string assignedUsers)
{
InitCheck();
var card = new Card
{
Id = cardId,
Title = title,
Description = description,
Priority = priority,
Index = index,
LaneId = laneId,
IsBlocked = isBlocked,
BlockReason = blockReason,
ExternalCardID = externalCardId,
ExternalSystemName = externalSystemName,
ExternalSystemUrl = externalSystemUrl,
StartDate = startDate,
DueDate = dueDate,
Tags = tags
};
if (size > 0) card.Size = size;
if (parentCardId > 0) card.ParentCardId = parentCardId;
card.AssignedUserIds = MapAssignedUsers(boardId, assignedUsers);
card.TypeId = MapCardType(boardId, cardType);
card.ClassOfServiceId = MapCustomIcon(boardId, customIcon);
var result = _api.UpdateCard(boardId, card);
return result.CardDTO.Id;
}
private long[] MapAssignedUsers(long boardId, string assignedUsers)
{
if (string.IsNullOrWhiteSpace(assignedUsers)) return new long[0];
var metaData = GetBoardMetadata(boardId);
var ids = new List<long>();
var emails = assignedUsers.Split(',');
foreach (var email in emails)
{
var user = metaData.BoardUsers.FirstOrDefault(u => u.Name.Equals(email.Trim(), StringComparison.OrdinalIgnoreCase));
if (user != null) ids.Add(user.Id);
}
return ids.ToArray();
}
private long MapCardType(long boardId, string typeName)
{
var cacheKey = string.Format("cardtype-{0}-{1}", boardId, typeName.Trim().ToLower());
var cachedObj = _cache.Get(cacheKey);
LeanKitIdentifier cardType;
if (cachedObj != null)
{
cardType = (LeanKitIdentifier) cachedObj;
}
else
{
var metaData = GetBoardMetadata(boardId);
cardType =
metaData.CardTypes.FirstOrDefault(x => x.Name.Equals(typeName.Trim(), StringComparison.OrdinalIgnoreCase)) ??
metaData.CardTypes[0];
_cache.Set(cacheKey, cardType, DateTimeOffset.Now.AddMinutes(5));
}
return cardType.Id;
}
private long MapCustomIcon(long boardId, string customIcon)
{
if (string.IsNullOrWhiteSpace(customIcon)) return 0;
var cacheKey = string.Format("custom-icon-{0}-{1}", boardId, customIcon.Trim().ToLower());
var cachedObj = _cache.Get(cacheKey);
LeanKitIdentifier cos;
if (cachedObj != null)
{
cos = (LeanKitIdentifier) cachedObj;
}
else
{
var metaData = GetBoardMetadata(boardId);
cos =
metaData.ClassesOfService.FirstOrDefault(
x => x.Name.Equals(customIcon.Trim(), StringComparison.OrdinalIgnoreCase)) ??
new LeanKitIdentifier {Id = 0, Name = customIcon.Trim().ToLower()};
_cache.Set(cacheKey, cos, DateTimeOffset.Now.AddMinutes(5));
}
return cos.Id;
}
}
}