Subversion Repository Public Repository

insightly-api

Diff Revisions 3 vs 4 for /trunk/Insightly/InsightlyAPI.cs

Diff revisions: vs.
  @@ -1,5 +1,6 @@
1 1 using System;
2 2 using System.Collections.Generic;
3 + using System.Net;
3 4 using RestSharp;
4 5 using TS.Insightly.API.Contract;
5 6 using TS.Insightly.API.Exception;
  @@ -8,6 +9,7 @@
8 9 {
9 10 /// <summary>
10 11 /// Facade for the Insightly API.
12 + /// Uses RestSharp - https://github.com/restsharp/RestSharp/wiki
11 13 /// </summary>
12 14 public class InsightlyAPI
13 15 {
  @@ -45,7 +47,7 @@
45 47
46 48 if (response.ErrorException != null)
47 49 {
48 - throw new ResponseException("Error retrieving response.", response.ErrorException, request.ToString());
50 + throw new ResponseException("Error retrieving response.", response.ErrorException);
49 51 }
50 52
51 53 return response.Data;
  @@ -61,7 +63,8 @@
61 63 public Contact GetContact(int contactId)
62 64 {
63 65 var request = new RestRequest(Method.GET);
64 - request.Resource = String.Format("/contacts/{0}", contactId);
66 + request.RequestFormat = DataFormat.Json;
67 + request.Resource = String.Format("contacts/{0}", contactId);
65 68
66 69 return Execute<Contact>(request);
67 70 }
  @@ -74,9 +77,61 @@
74 77 public List<Contact> GetContactsForEmail(string emailAddress)
75 78 {
76 79 var request = new RestRequest(Method.GET);
77 - request.Resource = String.Format("/contacts?email={0}", emailAddress);
80 + request.RequestFormat = DataFormat.Json;
81 + request.Resource = String.Format("contacts?email={0}", emailAddress);
78 82
79 83 return Execute<List<Contact>>(request);
80 84 }
85 +
86 + /// <summary>
87 + /// Adds the new contact.
88 + /// </summary>
89 + /// <param name="newContact">The new contact.</param>
90 + /// <returns>The added contact if successful.</returns>
91 + public Contact AddNewContact(Contact newContact)
92 + {
93 + var client = new RestClient();
94 + client.BaseUrl = BASE_URL;
95 + client.Authenticator = new HttpBasicAuthenticator(_apiKey, String.Empty);
96 +
97 + var request = new RestRequest(Method.POST);
98 + request.RequestFormat = DataFormat.Json;
99 + request.Resource = "contacts";
100 + request.AddBody(newContact);
101 +
102 + var response = client.Execute<Contact>(request);
103 +
104 + if (response.StatusCode != HttpStatusCode.Created)
105 + {
106 + string errorMsg = String.Format("Failed to create contact, response: {0}", response.StatusDescription);
107 + throw new ContactException(newContact, errorMsg, response.ErrorException);
108 + }
109 +
110 + return response.Data;
111 + }
112 +
113 + /// <summary>
114 + /// Deletes the contact.
115 + /// Note that the Id is not the Id shown for the contact in the web browser, but
116 + /// the contact Id that is unique to insightly.
117 + /// </summary>
118 + /// <param name="contactId">The contact unique identifier.</param>
119 + /// <returns><code>true</code> If the contact was deleted.</returns>
120 + public bool DeleteContact(int contactId)
121 + {
122 + var client = new RestClient();
123 + client.BaseUrl = BASE_URL;
124 + client.Authenticator = new HttpBasicAuthenticator(_apiKey, String.Empty);
125 +
126 + var request = new RestRequest(Method.DELETE);
127 + request.RequestFormat = DataFormat.Json;
128 + request.Resource = String.Format("contacts/{0}", contactId);
129 +
130 + var response = client.Execute(request);
131 +
132 + bool result = response.StatusCode == HttpStatusCode.Accepted;
133 +
134 + return result;
135 + }
81 136 }
82 137 }