Subversion Repository Public Repository

insightly-api

Diff Revisions 13 vs 14 for /trunk/Insightly/InsightlyAPI.cs

Diff revisions: vs.
  @@ -23,6 +23,24 @@
23 23 #endregion Fields
24 24
25 25 /// <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>
26 44 /// Initializes a new instance of the <see cref="InsightlyAPI"/> class.
27 45 /// </summary>
28 46 /// <param name="apiKey">The API key.</param>
  @@ -40,11 +58,7 @@
40 58 /// <exception cref="TS.Insightly.API.Exception.ResponseException">Error retrieving response.</exception>
41 59 public T Execute<T>(RestRequest request) where T : new()
42 60 {
43 - var client = new RestClient();
44 - client.BaseUrl = BASE_URL;
45 - client.Authenticator = new HttpBasicAuthenticator(_apiKey, String.Empty);
46 -
47 - var response = client.Execute<T>(request);
61 + var response = Client.Execute<T>(request);
48 62
49 63 if (response.ErrorException != null)
50 64 {
  @@ -93,16 +107,12 @@
93 107 /// <returns>The added contact if successful.</returns>
94 108 public Contact AddNewContact(Contact newContact)
95 109 {
96 - var client = new RestClient();
97 - client.BaseUrl = BASE_URL;
98 - client.Authenticator = new HttpBasicAuthenticator(_apiKey, String.Empty);
99 -
100 110 var request = new RestRequest(Method.POST);
101 111 request.RequestFormat = DataFormat.Json;
102 112 request.Resource = "contacts";
103 113 request.AddBody(newContact);
104 114
105 - var response = client.Execute<Contact>(request);
115 + var response = Client.Execute<Contact>(request);
106 116
107 117 if (response.StatusCode != HttpStatusCode.Created)
108 118 {
  @@ -174,15 +184,11 @@
174 184 /// <returns><code>true</code> If the contact was deleted.</returns>
175 185 public bool DeleteContact(int contactId)
176 186 {
177 - var client = new RestClient();
178 - client.BaseUrl = BASE_URL;
179 - client.Authenticator = new HttpBasicAuthenticator(_apiKey, String.Empty);
180 -
181 187 var request = new RestRequest(Method.DELETE);
182 188 request.RequestFormat = DataFormat.Json;
183 189 request.Resource = String.Format("contacts/{0}", contactId);
184 190
185 - var response = client.Execute(request);
191 + var response = Client.Execute(request);
186 192
187 193 bool result = response.StatusCode == HttpStatusCode.Accepted;
188 194
  @@ -269,16 +275,12 @@
269 275 /// <returns>The added organisation if successful.</returns>
270 276 public Organisation AddNewOrganisation(Organisation newOrganisation)
271 277 {
272 - var client = new RestClient();
273 - client.BaseUrl = BASE_URL;
274 - client.Authenticator = new HttpBasicAuthenticator(_apiKey, String.Empty);
275 -
276 278 var request = new RestRequest(Method.POST);
277 279 request.RequestFormat = DataFormat.Json;
278 280 request.Resource = "organisations";
279 281 request.AddBody(newOrganisation);
280 282
281 - var response = client.Execute<Organisation>(request);
283 + var response = Client.Execute<Organisation>(request);
282 284
283 285 if (response.StatusCode != HttpStatusCode.Created)
284 286 {
  @@ -298,15 +300,11 @@
298 300 /// <returns><code>true</code> If the organisation was deleted.</returns>
299 301 public bool DeleteOrganisation(int organisationId)
300 302 {
301 - var client = new RestClient();
302 - client.BaseUrl = BASE_URL;
303 - client.Authenticator = new HttpBasicAuthenticator(_apiKey, String.Empty);
304 -
305 303 var request = new RestRequest(Method.DELETE);
306 304 request.RequestFormat = DataFormat.Json;
307 305 request.Resource = String.Format("organisations/{0}", organisationId);
308 306
309 - var response = client.Execute(request);
307 + var response = Client.Execute(request);
310 308
311 309 bool result = response.StatusCode == HttpStatusCode.Accepted;
312 310