

insightly-api
@ 15
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 |
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.Interface; using TS.Insightly.API.Utility; namespace TS.Insightly.API.Controller { /// <summary> /// API access to contacts. /// </summary> internal class ContactController : ControllerBase, IContactController { /// <summary> /// Initializes a new instance of the <see cref="ContactController"/> class. /// </summary> /// <param name="apiKey">The API Key.</param> public ContactController(string apiKey) : base(apiKey) { } /// <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 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 InsightlyAPI.ContactAddResult AddNewContactIfNotExists(Contact newContact) { InsightlyAPI.ContactAddResult addResult = new InsightlyAPI.ContactAddResult { NewContactAdded = false, Contact = newContact }; if (newContact.CONTACTINFOS == null) { addResult = new InsightlyAPI.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 InsightlyAPI.ContactAddResult { NewContactAdded = false, Contact = contact }; existingFound = true; break; } } } if (existingFound) { break; } } if (!existingFound) { addResult = new InsightlyAPI.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 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; } } } |
Commits for insightly-api/trunk/Insightly/Controller/ContactController.cs
Revision | Author | Commited | Message |
---|---|---|---|
15 |
|
Tue 01 Oct, 2013 13:47:15 +0000 | Refactored, added Ninject to load controllers for each part of the API. |