|
@@ -1,71 +1,37 @@ |
1 |
|
- |
using System; |
2 |
|
- |
using System.Collections.Generic; |
3 |
|
- |
using System.Net; |
4 |
|
- |
using RestSharp; |
|
1 |
+ |
using System.Collections.Generic; |
|
2 |
+ |
using Ninject; |
5 |
3 |
|
using TS.Insightly.API.Contract; |
6 |
|
- |
using TS.Insightly.API.Exception; |
7 |
|
- |
using TS.Insightly.API.Utility; |
|
4 |
+ |
using TS.Insightly.API.ICModule; |
|
5 |
+ |
using TS.Insightly.API.Interface; |
8 |
6 |
|
|
9 |
7 |
|
namespace TS.Insightly.API |
10 |
8 |
|
{ |
11 |
9 |
|
/// <summary> |
12 |
10 |
|
/// Facade for the Insightly API. |
13 |
11 |
|
/// Uses RestSharp - https://github.com/restsharp/RestSharp/wiki |
|
12 |
+ |
/// Uses Ninject - http://www.ninject.org/index.html |
14 |
13 |
|
/// </summary> |
15 |
|
- |
public class InsightlyAPI |
|
14 |
+ |
public class InsightlyAPI : IApi |
16 |
15 |
|
{ |
17 |
16 |
|
#region Fields |
18 |
17 |
|
|
19 |
|
- |
private const string BASE_URL = @"https://api.insight.ly/v2/"; |
20 |
|
- |
|
21 |
|
- |
private readonly string _apiKey; |
|
18 |
+ |
private readonly IContactController _contactController; |
|
19 |
+ |
private readonly IOrganisationController _organisationController; |
|
20 |
+ |
private readonly IProjectController _projectController; |
22 |
21 |
|
|
23 |
22 |
|
#endregion Fields |
24 |
23 |
|
|
25 |
24 |
|
/// <summary> |
26 |
|
- |
/// Gets the REST client. |
27 |
|
- |
/// </summary> |
28 |
|
- |
/// <value> |
29 |
|
- |
/// The REST client. |
30 |
|
- |
/// </value> |
31 |
|
- |
internal RestClient Client |
32 |
|
- |
{ |
33 |
|
- |
get |
34 |
|
- |
{ |
35 |
|
- |
var client = new RestClient(); |
36 |
|
- |
client.BaseUrl = BASE_URL; |
37 |
|
- |
client.Authenticator = new HttpBasicAuthenticator(_apiKey, String.Empty); |
38 |
|
- |
|
39 |
|
- |
return client; |
40 |
|
- |
} |
41 |
|
- |
} |
42 |
|
- |
|
43 |
|
- |
/// <summary> |
44 |
25 |
|
/// Initializes a new instance of the <see cref="InsightlyAPI"/> class. |
45 |
26 |
|
/// </summary> |
46 |
27 |
|
/// <param name="apiKey">The API key.</param> |
47 |
28 |
|
public InsightlyAPI(string apiKey) |
48 |
29 |
|
{ |
49 |
|
- |
_apiKey = apiKey; |
50 |
|
- |
} |
|
30 |
+ |
IKernel kernal = new StandardKernel(new ApiModule(apiKey)); |
51 |
31 |
|
|
52 |
|
- |
/// <summary> |
53 |
|
- |
/// Executes the specified request. |
54 |
|
- |
/// </summary> |
55 |
|
- |
/// <typeparam name="T">Return type of the data being requested.</typeparam> |
56 |
|
- |
/// <param name="request">The request.</param> |
57 |
|
- |
/// <returns>Data returned by the request.</returns> |
58 |
|
- |
/// <exception cref="TS.Insightly.API.Exception.ResponseException">Error retrieving response.</exception> |
59 |
|
- |
public T Execute<T>(RestRequest request) where T : new() |
60 |
|
- |
{ |
61 |
|
- |
var response = Client.Execute<T>(request); |
62 |
|
- |
|
63 |
|
- |
if (response.ErrorException != null) |
64 |
|
- |
{ |
65 |
|
- |
throw new ResponseException("Error retrieving response.", response.ErrorException); |
66 |
|
- |
} |
67 |
|
- |
|
68 |
|
- |
return response.Data; |
|
32 |
+ |
_contactController = kernal.Get<IContactController>(); |
|
33 |
+ |
_organisationController = kernal.Get<IOrganisationController>(); |
|
34 |
+ |
_projectController = kernal.Get<IProjectController>(); |
69 |
35 |
|
} |
70 |
36 |
|
|
71 |
37 |
|
#region Contacts |
|
@@ -79,11 +45,7 @@ |
79 |
45 |
|
/// <returns>The contact for the given id.</returns> |
80 |
46 |
|
public Contact GetContact(int contactId) |
81 |
47 |
|
{ |
82 |
|
- |
var request = new RestRequest(Method.GET); |
83 |
|
- |
request.RequestFormat = DataFormat.Json; |
84 |
|
- |
request.Resource = String.Format("contacts/{0}", contactId); |
85 |
|
- |
|
86 |
|
- |
return Execute<Contact>(request); |
|
48 |
+ |
return _contactController.GetContact(contactId); |
87 |
49 |
|
} |
88 |
50 |
|
|
89 |
51 |
|
/// <summary> |
|
@@ -93,11 +55,7 @@ |
93 |
55 |
|
/// <returns>List of contacts with a matching email address or empty list if no matches found.</returns> |
94 |
56 |
|
public List<Contact> GetContactsForEmail(string emailAddress) |
95 |
57 |
|
{ |
96 |
|
- |
var request = new RestRequest(Method.GET); |
97 |
|
- |
request.RequestFormat = DataFormat.Json; |
98 |
|
- |
request.Resource = String.Format("contacts?email={0}", emailAddress); |
99 |
|
- |
|
100 |
|
- |
return Execute<List<Contact>>(request); |
|
58 |
+ |
return _contactController.GetContactsForEmail(emailAddress); |
101 |
59 |
|
} |
102 |
60 |
|
|
103 |
61 |
|
/// <summary> |
|
@@ -107,20 +65,7 @@ |
107 |
65 |
|
/// <returns>The added contact if successful.</returns> |
108 |
66 |
|
public Contact AddNewContact(Contact newContact) |
109 |
67 |
|
{ |
110 |
|
- |
var request = new RestRequest(Method.POST); |
111 |
|
- |
request.RequestFormat = DataFormat.Json; |
112 |
|
- |
request.Resource = "contacts"; |
113 |
|
- |
request.AddBody(newContact); |
114 |
|
- |
|
115 |
|
- |
var response = Client.Execute<Contact>(request); |
116 |
|
- |
|
117 |
|
- |
if (response.StatusCode != HttpStatusCode.Created) |
118 |
|
- |
{ |
119 |
|
- |
string errorMsg = String.Format("Failed to create contact, response: {0}", response.StatusDescription); |
120 |
|
- |
throw new ContactException(newContact, errorMsg, response.ErrorException); |
121 |
|
- |
} |
122 |
|
- |
|
123 |
|
- |
return response.Data; |
|
68 |
+ |
return _contactController.AddNewContact(newContact); |
124 |
69 |
|
} |
125 |
70 |
|
|
126 |
71 |
|
/// <summary> |
|
@@ -131,48 +76,7 @@ |
131 |
76 |
|
/// <returns>Structure containing details of the add.</returns> |
132 |
77 |
|
public ContactAddResult AddNewContactIfNotExists(Contact newContact) |
133 |
78 |
|
{ |
134 |
|
- |
ContactAddResult addResult = new ContactAddResult { NewContactAdded = false, Contact = newContact }; |
135 |
|
- |
|
136 |
|
- |
if (newContact.CONTACTINFOS == null) |
137 |
|
- |
{ |
138 |
|
- |
addResult = new ContactAddResult { NewContactAdded = true, Contact = AddNewContact(newContact) }; |
139 |
|
- |
} |
140 |
|
- |
else |
141 |
|
- |
{ |
142 |
|
- |
bool existingFound = false; |
143 |
|
- |
|
144 |
|
- |
foreach (var contactInfo in newContact.CONTACTINFOS) |
145 |
|
- |
{ |
146 |
|
- |
if (contactInfo.TYPE.Equals(ContactInfo.InfoType.Email.GetDescription(), StringComparison.InvariantCultureIgnoreCase)) |
147 |
|
- |
{ |
148 |
|
- |
var existingContacts = GetContactsForEmail(contactInfo.DETAIL); |
149 |
|
- |
|
150 |
|
- |
foreach (var contact in existingContacts) |
151 |
|
- |
{ |
152 |
|
- |
if (contact.FIRST_NAME.Equals(newContact.FIRST_NAME, StringComparison.InvariantCultureIgnoreCase) |
153 |
|
- |
&& |
154 |
|
- |
contact.LAST_NAME.Equals(newContact.LAST_NAME, StringComparison.InvariantCultureIgnoreCase)) |
155 |
|
- |
{ |
156 |
|
- |
addResult = new ContactAddResult { NewContactAdded = false, Contact = contact }; |
157 |
|
- |
existingFound = true; |
158 |
|
- |
break; |
159 |
|
- |
} |
160 |
|
- |
} |
161 |
|
- |
} |
162 |
|
- |
|
163 |
|
- |
if (existingFound) |
164 |
|
- |
{ |
165 |
|
- |
break; |
166 |
|
- |
} |
167 |
|
- |
} |
168 |
|
- |
|
169 |
|
- |
if (!existingFound) |
170 |
|
- |
{ |
171 |
|
- |
addResult = new ContactAddResult { NewContactAdded = true, Contact = AddNewContact(newContact) }; |
172 |
|
- |
} |
173 |
|
- |
} |
174 |
|
- |
|
175 |
|
- |
return addResult; |
|
79 |
+ |
return _contactController.AddNewContactIfNotExists(newContact); |
176 |
80 |
|
} |
177 |
81 |
|
|
178 |
82 |
|
/// <summary> |
|
@@ -184,15 +88,7 @@ |
184 |
88 |
|
/// <returns><code>true</code> If the contact was deleted.</returns> |
185 |
89 |
|
public bool DeleteContact(int contactId) |
186 |
90 |
|
{ |
187 |
|
- |
var request = new RestRequest(Method.DELETE); |
188 |
|
- |
request.RequestFormat = DataFormat.Json; |
189 |
|
- |
request.Resource = String.Format("contacts/{0}", contactId); |
190 |
|
- |
|
191 |
|
- |
var response = Client.Execute(request); |
192 |
|
- |
|
193 |
|
- |
bool result = response.StatusCode == HttpStatusCode.Accepted; |
194 |
|
- |
|
195 |
|
- |
return result; |
|
91 |
+ |
return _contactController.DeleteContact(contactId); |
196 |
92 |
|
} |
197 |
93 |
|
|
198 |
94 |
|
/// <summary> |
|
@@ -226,11 +122,7 @@ |
226 |
122 |
|
/// <returns>The organisation for the given id.</returns> |
227 |
123 |
|
public Organisation GetOrganisation(int organisationId) |
228 |
124 |
|
{ |
229 |
|
- |
var request = new RestRequest(Method.GET); |
230 |
|
- |
request.RequestFormat = DataFormat.Json; |
231 |
|
- |
request.Resource = String.Format("organisations/{0}", organisationId); |
232 |
|
- |
|
233 |
|
- |
return Execute<Organisation>(request); |
|
125 |
+ |
return _organisationController.GetOrganisation(organisationId); |
234 |
126 |
|
} |
235 |
127 |
|
|
236 |
128 |
|
/// <summary> |
|
@@ -240,11 +132,7 @@ |
240 |
132 |
|
/// <returns>All organisations.</returns> |
241 |
133 |
|
public List<Organisation> GetOrganisations(bool fullDetails) |
242 |
134 |
|
{ |
243 |
|
- |
var request = new RestRequest(Method.GET); |
244 |
|
- |
request.RequestFormat = DataFormat.Json; |
245 |
|
- |
request.Resource = fullDetails ? "organisations" : "organisations?brief=true"; |
246 |
|
- |
|
247 |
|
- |
return Execute<List<Organisation>>(request); |
|
135 |
+ |
return _organisationController.GetOrganisations(fullDetails); |
248 |
136 |
|
} |
249 |
137 |
|
|
250 |
138 |
|
/// <summary> |
|
@@ -254,18 +142,7 @@ |
254 |
142 |
|
/// <returns>List of organisations (basic details) matching the given name.</returns> |
255 |
143 |
|
public List<Organisation> GetOrganisationsForName(string orgName) |
256 |
144 |
|
{ |
257 |
|
- |
List<Organisation> namedOrgs = new List<Organisation>(); |
258 |
|
- |
List<Organisation> allOrgs = GetOrganisations(false); |
259 |
|
- |
|
260 |
|
- |
foreach (var organisation in allOrgs) |
261 |
|
- |
{ |
262 |
|
- |
if (organisation.ORGANISATION_NAME.Equals(orgName, StringComparison.InvariantCultureIgnoreCase)) |
263 |
|
- |
{ |
264 |
|
- |
namedOrgs.Add(organisation); |
265 |
|
- |
} |
266 |
|
- |
} |
267 |
|
- |
|
268 |
|
- |
return namedOrgs; |
|
145 |
+ |
return _organisationController.GetOrganisationsForName(orgName); |
269 |
146 |
|
} |
270 |
147 |
|
|
271 |
148 |
|
/// <summary> |
|
@@ -275,20 +152,7 @@ |
275 |
152 |
|
/// <returns>The added organisation if successful.</returns> |
276 |
153 |
|
public Organisation AddNewOrganisation(Organisation newOrganisation) |
277 |
154 |
|
{ |
278 |
|
- |
var request = new RestRequest(Method.POST); |
279 |
|
- |
request.RequestFormat = DataFormat.Json; |
280 |
|
- |
request.Resource = "organisations"; |
281 |
|
- |
request.AddBody(newOrganisation); |
282 |
|
- |
|
283 |
|
- |
var response = Client.Execute<Organisation>(request); |
284 |
|
- |
|
285 |
|
- |
if (response.StatusCode != HttpStatusCode.Created) |
286 |
|
- |
{ |
287 |
|
- |
string errorMsg = String.Format("Failed to create organisation, response: {0}", response.StatusDescription); |
288 |
|
- |
throw new OrganisationException(newOrganisation, errorMsg, response.ErrorException); |
289 |
|
- |
} |
290 |
|
- |
|
291 |
|
- |
return response.Data; |
|
155 |
+ |
return _organisationController.AddNewOrganisation(newOrganisation); |
292 |
156 |
|
} |
293 |
157 |
|
|
294 |
158 |
|
/// <summary> |
|
@@ -300,15 +164,7 @@ |
300 |
164 |
|
/// <returns><code>true</code> If the organisation was deleted.</returns> |
301 |
165 |
|
public bool DeleteOrganisation(int organisationId) |
302 |
166 |
|
{ |
303 |
|
- |
var request = new RestRequest(Method.DELETE); |
304 |
|
- |
request.RequestFormat = DataFormat.Json; |
305 |
|
- |
request.Resource = String.Format("organisations/{0}", organisationId); |
306 |
|
- |
|
307 |
|
- |
var response = Client.Execute(request); |
308 |
|
- |
|
309 |
|
- |
bool result = response.StatusCode == HttpStatusCode.Accepted; |
310 |
|
- |
|
311 |
|
- |
return result; |
|
167 |
+ |
return _organisationController.DeleteOrganisation(organisationId); |
312 |
168 |
|
} |
313 |
169 |
|
|
314 |
170 |
|
#endregion Organisations |
|
@@ -324,11 +180,7 @@ |
324 |
180 |
|
/// <returns>The project for the given id.</returns> |
325 |
181 |
|
public Project GetProject(int projectId) |
326 |
182 |
|
{ |
327 |
|
- |
var request = new RestRequest(Method.GET); |
328 |
|
- |
request.RequestFormat = DataFormat.Json; |
329 |
|
- |
request.Resource = String.Format("projects/{0}", projectId); |
330 |
|
- |
|
331 |
|
- |
return Execute<Project>(request); |
|
183 |
+ |
return _projectController.GetProject(projectId); |
332 |
184 |
|
} |
333 |
185 |
|
|
334 |
186 |
|
/// <summary> |
|
@@ -338,11 +190,7 @@ |
338 |
190 |
|
/// <returns>All projects.</returns> |
339 |
191 |
|
public List<Project> GetProjects(bool fullDetails) |
340 |
192 |
|
{ |
341 |
|
- |
var request = new RestRequest(Method.GET); |
342 |
|
- |
request.RequestFormat = DataFormat.Json; |
343 |
|
- |
request.Resource = fullDetails ? "projects" : "projects?brief=true"; |
344 |
|
- |
|
345 |
|
- |
return Execute<List<Project>>(request); |
|
193 |
+ |
return _projectController.GetProjects(fullDetails); |
346 |
194 |
|
} |
347 |
195 |
|
|
348 |
196 |
|
#endregion Projects |