Subversion Repository Public Repository

insightly-api

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
´╗┐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;

namespace TS.Insightly.API.Controller
{
    /// <summary>
    /// API access to organisations.
    /// </summary>
    internal class OrganisationController : ControllerBase, IOrganisationController
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="OrganisationController"/> class.
        /// </summary>
        /// <param name="apiKey">The API key.</param>
          public OrganisationController(string apiKey)
            : base(apiKey)
        {
            
        }

          /// <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 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 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;
          }
    }
}

Commits for insightly-api/trunk/Insightly/Controller/OrganisationController.cs

Diff revisions: vs.
Revision Author Commited Message
15 HadleyHope picture HadleyHope Tue 01 Oct, 2013 13:47:15 +0000

Refactored, added Ninject to load controllers for each part of the API.
InsightlyAPI class is now a true facade for the API.