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
using System;
using RestSharp;
using TS.Insightly.API.Exception;

namespace TS.Insightly.API.Controller
{
    /// <summary>
    /// Base Controller for API access.
    /// </summary>
    internal abstract class ControllerBase
    {
        #region Fields

        private const string BASE_URL = @"https://api.insight.ly/v2/";

        private string _apiKey;

        #endregion Fields

        /// <summary>
        /// Initializes a new instance of the <see cref="ControllerBase"/> class.
        /// </summary>
        /// <param name="apiKey">The API key.</param>
        protected ControllerBase(string apiKey)
        {
            _apiKey = apiKey;
        }

        /// <summary>
        /// Gets the REST client.
        /// </summary>
        /// <value>
        /// The REST client.
        /// </value>
        protected RestClient Client
        {
            get
            {
                var client = new RestClient();
                client.BaseUrl = BASE_URL;
                client.Authenticator = new HttpBasicAuthenticator(_apiKey, String.Empty);

                return client;
            }
        }

        /// <summary>
        /// Executes the specified request.
        /// </summary>
        /// <typeparam name="T">Return type of the data being requested.</typeparam>
        /// <param name="request">The request.</param>
        /// <returns>Data returned by the request.</returns>
        /// <exception cref="TS.Insightly.API.Exception.ResponseException">Error retrieving response.</exception>
        protected T Execute<T>(RestRequest request) where T : new()
        {
            var response = Client.Execute<T>(request);

            if (response.ErrorException != null)
            {
                throw new ResponseException("Error retrieving response.", response.ErrorException);
            }

            return response.Data;
        }
       
    }
}

Commits for insightly-api/trunk/Insightly/Controller/ControllerBase.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.