

insightly-api
@ 13
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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
using System; using System.Collections.Generic; using System.Net; using RestSharp; using TS.Insightly.API.Contract; using TS.Insightly.API.Exception; using TS.Insightly.API.Utility; namespace TS.Insightly.API { /// <summary> /// Facade for the Insightly API. /// Uses RestSharp - https://github.com/restsharp/RestSharp/wiki /// </summary> public class InsightlyAPI { #region Fields private const string BASE_URL = @"https://api.insight.ly/v2/"; private readonly string _apiKey; #endregion Fields /// <summary> /// Initializes a new instance of the <see cref="InsightlyAPI"/> class. /// </summary> /// <param name="apiKey">The API key.</param> public InsightlyAPI(string apiKey) { _apiKey = apiKey; } /// <summary> /// Executes the specified request. /// </summary> /// <typeparam name="T">Return type of the data being requested.</typeparam> /// <param name="request">The request.</param> /// <returns>Data returned by the request.</returns> /// <exception cref="TS.Insightly.API.Exception.ResponseException">Error retrieving response.</exception> public T Execute<T>(RestRequest request) where T : new() { var client = new RestClient(); client.BaseUrl = BASE_URL; client.Authenticator = new HttpBasicAuthenticator(_apiKey, String.Empty); var response = client.Execute<T>(request); if (response.ErrorException != null) { throw new ResponseException("Error retrieving response.", response.ErrorException); } return response.Data; } #region Contacts /// <summary> /// Gets the contact for the unique contact ID. /// Note that the Id is not the Id shown for the contact in the web browser, but /// the contact Id that is unique to insightly. /// </summary> /// <param name="contactId">The contact unique identifier.</param> /// <returns>The contact for the given id.</returns> public Contact GetContact(int contactId) { var request = new RestRequest(Method.GET); request.RequestFormat = DataFormat.Json; request.Resource = String.Format("contacts/{0}", contactId); return Execute<Contact>(request); } /// <summary> /// Gets the contacts for email address. /// </summary> /// <param name="emailAddress">The email address.</param> /// <returns>List of contacts with a matching email address or empty list if no matches found.</returns> public List<Contact> GetContactsForEmail(string emailAddress) { var request = new RestRequest(Method.GET); request.RequestFormat = DataFormat.Json; request.Resource = String.Format("contacts?email={0}", emailAddress); return Execute<List<Contact>>(request); } /// <summary> /// Adds the new contact. /// </summary> /// <param name="newContact">The new contact.</param> /// <returns>The added contact if successful.</returns> public Contact AddNewContact(Contact newContact) { var client = new RestClient(); client.BaseUrl = BASE_URL; client.Authenticator = new HttpBasicAuthenticator(_apiKey, String.Empty); var request = new RestRequest(Method.POST); request.RequestFormat = DataFormat.Json; request.Resource = "contacts"; request.AddBody(newContact); var response = client.Execute<Contact>(request); if (response.StatusCode != HttpStatusCode.Created) { string errorMsg = String.Format("Failed to create contact, response: {0}", response.StatusDescription); throw new ContactException(newContact, errorMsg, response.ErrorException); } return response.Data; } /// <summary> /// Adds the new contact if a contact does not already exist for the same email, /// first and last names as the given contact. /// </summary> /// <param name="newContact">The new contact.</param> /// <returns>Structure containing details of the add.</returns> public ContactAddResult AddNewContactIfNotExists(Contact newContact) { ContactAddResult addResult = new ContactAddResult { NewContactAdded = false, Contact = newContact }; if (newContact.CONTACTINFOS == null) { addResult = new ContactAddResult { NewContactAdded = true, Contact = AddNewContact(newContact) }; } else { bool existingFound = false; foreach (var contactInfo in newContact.CONTACTINFOS) { if (contactInfo.TYPE.Equals(ContactInfo.InfoType.Email.GetDescription(), StringComparison.InvariantCultureIgnoreCase)) { var existingContacts = GetContactsForEmail(contactInfo.DETAIL); foreach (var contact in existingContacts) { if (contact.FIRST_NAME.Equals(newContact.FIRST_NAME, StringComparison.InvariantCultureIgnoreCase) && contact.LAST_NAME.Equals(newContact.LAST_NAME, StringComparison.InvariantCultureIgnoreCase)) { addResult = new ContactAddResult { NewContactAdded = false, Contact = contact }; existingFound = true; break; } } } if (existingFound) { break; } } if (!existingFound) { addResult = new ContactAddResult { NewContactAdded = true, Contact = AddNewContact(newContact) }; } } return addResult; } /// <summary> /// Deletes the contact. /// Note that the Id is not the Id shown for the contact in the web browser, but /// the contact Id that is unique to insightly. /// </summary> /// <param name="contactId">The contact unique identifier.</param> /// <returns><code>true</code> If the contact was deleted.</returns> public bool DeleteContact(int contactId) { var client = new RestClient(); client.BaseUrl = BASE_URL; client.Authenticator = new HttpBasicAuthenticator(_apiKey, String.Empty); var request = new RestRequest(Method.DELETE); request.RequestFormat = DataFormat.Json; request.Resource = String.Format("contacts/{0}", contactId); var response = client.Execute(request); bool result = response.StatusCode == HttpStatusCode.Accepted; return result; } /// <summary> /// Result for conditional contact add. /// </summary> public struct ContactAddResult { /// <summary> /// <code>true</code> If the condition check resulted in a new contact added. /// Note if there was an add failure <see cref="Contact"/> may not be valid. /// </summary> public bool NewContactAdded; /// <summary> /// The contact, if <see cref="NewContactAdded"/> was <code>true</code> this will /// be the newly added contact, otherwise it will be the existing contact. /// </summary> public Contact Contact; } #endregion Contacts #region Organisations /// <summary> /// Gets the organisation for the unique organisation ID. /// Note that the Id is not the Id shown for the organisation in the web browser, but /// the organisation Id that is unique to insightly. /// </summary> /// <param name="organisationId">The organisation unique identifier.</param> /// <returns>The organisation for the given id.</returns> public Organisation GetOrganisation(int organisationId) { var request = new RestRequest(Method.GET); request.RequestFormat = DataFormat.Json; request.Resource = String.Format("organisations/{0}", organisationId); return Execute<Organisation>(request); } /// <summary> /// Gets organisations. /// </summary> /// <param name="fullDetails">if set to <c>true</c> return full details.</param> /// <returns>All organisations.</returns> public List<Organisation> GetOrganisations(bool fullDetails) { var request = new RestRequest(Method.GET); request.RequestFormat = DataFormat.Json; request.Resource = fullDetails ? "organisations" : "organisations?brief=true"; return Execute<List<Organisation>>(request); } /// <summary> /// Gets organisations matching the given name. /// </summary> /// <param name="orgName">Name of the org to find.</param> /// <returns>List of organisations (basic details) matching the given name.</returns> public List<Organisation> GetOrganisationsForName(string orgName) { List<Organisation> namedOrgs = new List<Organisation>(); List<Organisation> allOrgs = GetOrganisations(false); foreach (var organisation in allOrgs) { if (organisation.ORGANISATION_NAME.Equals(orgName, StringComparison.InvariantCultureIgnoreCase)) { namedOrgs.Add(organisation); } } return namedOrgs; } /// <summary> /// Adds the new organisation. /// </summary> /// <param name="newOrganisation">The new organisation.</param> /// <returns>The added organisation if successful.</returns> public Organisation AddNewOrganisation(Organisation newOrganisation) { var client = new RestClient(); client.BaseUrl = BASE_URL; client.Authenticator = new HttpBasicAuthenticator(_apiKey, String.Empty); var request = new RestRequest(Method.POST); request.RequestFormat = DataFormat.Json; request.Resource = "organisations"; request.AddBody(newOrganisation); var response = client.Execute<Organisation>(request); if (response.StatusCode != HttpStatusCode.Created) { string errorMsg = String.Format("Failed to create organisation, response: {0}", response.StatusDescription); throw new OrganisationException(newOrganisation, errorMsg, response.ErrorException); } return response.Data; } /// <summary> /// Deletes the organisation. /// Note that the Id is not the Id shown for the organisation in the web browser, but /// the organisation Id that is unique to insightly. /// </summary> /// <param name="organisationId">The organisation unique identifier.</param> /// <returns><code>true</code> If the organisation was deleted.</returns> public bool DeleteOrganisation(int organisationId) { var client = new RestClient(); client.BaseUrl = BASE_URL; client.Authenticator = new HttpBasicAuthenticator(_apiKey, String.Empty); var request = new RestRequest(Method.DELETE); request.RequestFormat = DataFormat.Json; request.Resource = String.Format("organisations/{0}", organisationId); var response = client.Execute(request); bool result = response.StatusCode == HttpStatusCode.Accepted; return result; } #endregion Organisations #region Projects /// <summary> /// Gets the project for the unique organisation ID. /// Note that the Id is not the Id shown for the project in the web browser, but /// the project Id that is unique to insightly. /// </summary> /// <param name="projectId">The project unique identifier.</param> /// <returns>The project for the given id.</returns> public Project GetProject(int projectId) { var request = new RestRequest(Method.GET); request.RequestFormat = DataFormat.Json; request.Resource = String.Format("projects/{0}", projectId); return Execute<Project>(request); } /// <summary> /// Gets projects. /// </summary> /// <param name="fullDetails">if set to <c>true</c> return full details.</param> /// <returns>All projects.</returns> public List<Project> GetProjects(bool fullDetails) { var request = new RestRequest(Method.GET); request.RequestFormat = DataFormat.Json; request.Resource = fullDetails ? "projects" : "projects?brief=true"; return Execute<List<Project>>(request); } #endregion Projects } } |
Commits for insightly-api/trunk/Insightly/InsightlyAPI.cs
Revision | Author | Commited | Message |
---|---|---|---|
13
![]() |
|
Mon 30 Sep, 2013 15:02:03 +0000 | Added basics for Project. |
10
![]() |
|
Tue 24 Sep, 2013 12:06:30 +0000 | Added basic organisation functionality, at the moment cannot link organisation to contact. |
9
![]() |
|
Tue 24 Sep, 2013 10:12:34 +0000 | Added AddNewContactIfNotExists that checks for existing email, first and last names before adding a contact. |
4
![]() |
|
Fri 20 Sep, 2013 15:08:23 +0000 | Added code and tests to add a basic contact with email address. |
3 |
|
Thu 19 Sep, 2013 14:44:24 +0000 | Initial working API retrieving a contact by it’s id and email address. |