Subversion Repository Public Repository

insightly-api

Diff Revisions 9 vs 10 for /trunk/Insightly/InsightlyAPI.cs

Diff revisions: vs.
  @@ -211,6 +211,107 @@
211 211
212 212 #region Organisations
213 213
214 + /// <summary>
215 + /// Gets the organisation for the unique organisation ID.
216 + /// Note that the Id is not the Id shown for the organisation in the web browser, but
217 + /// the organisation Id that is unique to insightly.
218 + /// </summary>
219 + /// <param name="organisationId">The organisation unique identifier.</param>
220 + /// <returns>The organisation for the given id.</returns>
221 + public Organisation GetOrganisation(int organisationId)
222 + {
223 + var request = new RestRequest(Method.GET);
224 + request.RequestFormat = DataFormat.Json;
225 + request.Resource = String.Format("organisations/{0}", organisationId);
226 +
227 + return Execute<Organisation>(request);
228 + }
229 +
230 + /// <summary>
231 + /// Gets organisations.
232 + /// </summary>
233 + /// <param name="fullDetails">if set to <c>true</c> return full details.</param>
234 + /// <returns>All organisations.</returns>
235 + public List<Organisation> GetOrganisations(bool fullDetails)
236 + {
237 + var request = new RestRequest(Method.GET);
238 + request.RequestFormat = DataFormat.Json;
239 + request.Resource = fullDetails ? "organisations" : "organisations?brief=true";
240 +
241 + return Execute<List<Organisation>>(request);
242 + }
243 +
244 + /// <summary>
245 + /// Gets organisations matching the given name.
246 + /// </summary>
247 + /// <param name="orgName">Name of the org to find.</param>
248 + /// <returns>List of organisations (basic details) matching the given name.</returns>
249 + public List<Organisation> GetOrganisationsForName(string orgName)
250 + {
251 + List<Organisation> namedOrgs = new List<Organisation>();
252 + List<Organisation> allOrgs = GetOrganisations(false);
253 +
254 + foreach (var organisation in allOrgs)
255 + {
256 + if (organisation.ORGANISATION_NAME.Equals(orgName, StringComparison.InvariantCultureIgnoreCase))
257 + {
258 + namedOrgs.Add(organisation);
259 + }
260 + }
261 +
262 + return namedOrgs;
263 + }
264 +
265 + /// <summary>
266 + /// Adds the new organisation.
267 + /// </summary>
268 + /// <param name="newOrganisation">The new organisation.</param>
269 + /// <returns>The added organisation if successful.</returns>
270 + public Organisation AddNewOrganisation(Organisation newOrganisation)
271 + {
272 + var client = new RestClient();
273 + client.BaseUrl = BASE_URL;
274 + client.Authenticator = new HttpBasicAuthenticator(_apiKey, String.Empty);
275 +
276 + var request = new RestRequest(Method.POST);
277 + request.RequestFormat = DataFormat.Json;
278 + request.Resource = "organisations";
279 + request.AddBody(newOrganisation);
280 +
281 + var response = client.Execute<Organisation>(request);
282 +
283 + if (response.StatusCode != HttpStatusCode.Created)
284 + {
285 + string errorMsg = String.Format("Failed to create organisation, response: {0}", response.StatusDescription);
286 + throw new OrganisationException(newOrganisation, errorMsg, response.ErrorException);
287 + }
288 +
289 + return response.Data;
290 + }
291 +
292 + /// <summary>
293 + /// Deletes the organisation.
294 + /// Note that the Id is not the Id shown for the organisation in the web browser, but
295 + /// the organisation Id that is unique to insightly.
296 + /// </summary>
297 + /// <param name="organisationId">The organisation unique identifier.</param>
298 + /// <returns><code>true</code> If the organisation was deleted.</returns>
299 + public bool DeleteOrganisation(int organisationId)
300 + {
301 + var client = new RestClient();
302 + client.BaseUrl = BASE_URL;
303 + client.Authenticator = new HttpBasicAuthenticator(_apiKey, String.Empty);
304 +
305 + var request = new RestRequest(Method.DELETE);
306 + request.RequestFormat = DataFormat.Json;
307 + request.Resource = String.Format("organisations/{0}", organisationId);
308 +
309 + var response = client.Execute(request);
310 +
311 + bool result = response.StatusCode == HttpStatusCode.Accepted;
312 +
313 + return result;
314 + }
214 315
215 316 #endregion Organisations
216 317