11using System ;
22using RestSharp ;
3- using CallfireApiClient . Api . Common . Model . Request ;
43using RestSharp . Authenticators ;
54using System . Collections . Generic ;
65using System . Configuration ;
109using System . Collections ;
1110using System . Text ;
1211using System . IO ;
13- using System . Reflection ;
14- using System . Diagnostics ;
1512using System . Net ;
1613
1714namespace CallfireApiClient
@@ -21,25 +18,35 @@ namespace CallfireApiClient
2118 /// </summary>
2219 public class RestApiClient
2320 {
24- private const string DEFAULT_FILE_CONTENT_TYPE = "application/octet-stream" ;
25-
26- private readonly Logger Logger = new Logger ( ) ;
2721 private readonly ISerializer JsonSerializer ;
2822 private readonly IDeserializer JsonDeserializer ;
29- private static KeyValueConfigurationCollection ClientConfig ;
23+ private static Logger Logger = new Logger ( ) ;
24+
25+ private static KeyValueConfigurationCollection ApplicationConfig ;
26+
27+ private ClientConfig _ClientConfig = new ClientConfig ( ) ;
28+
29+ public ClientConfig ClientConfig
30+ {
31+ get
32+ {
33+ return _ClientConfig ;
34+ }
35+
36+ set
37+ {
38+ _ClientConfig = value ;
39+ RestClient . BaseUrl = ! string . IsNullOrWhiteSpace ( value . ApiBasePath ) ? new Uri ( value . ApiBasePath ) : RestClient . BaseUrl ;
40+ SetUpRestClientProxy ( ) ;
41+ }
42+ }
3043
3144 /// <summary>
3245 /// RestSharp client configured to query Callfire API
3346 /// <summary>/
3447 /// <returns>RestSharp client interface</returns>
3548 public IRestClient RestClient { get ; set ; }
3649
37- /// <summary>
38- /// Returns base URL path for all Callfire's API 2.0 endpoints
39- /// <summary>/
40- /// <returns>string representation of base URL path</returns>
41- public string ApiBasePath { get ; private set ; }
42-
4350 /// <summary>
4451 /// Returns HTTP request filters associated with API client
4552 /// </summary>
@@ -50,16 +57,16 @@ public class RestApiClient
5057 /// loads client configuration
5158 /// </summary>
5259 static RestApiClient ( ) {
53- ClientConfig = LoadAppSettings ( ) ;
60+ ApplicationConfig = LoadAppSettings ( ) ;
5461 }
5562
5663 /// <summary>
5764 /// Get client configuration
5865 /// </summary>
5966 /// <value>configuration properties collection</value>
60- public static KeyValueConfigurationCollection getClientConfig ( )
67+ public static KeyValueConfigurationCollection getApplicationConfig ( )
6168 {
62- return ClientConfig ;
69+ return ApplicationConfig ;
6370 }
6471
6572 /// <summary>
@@ -70,60 +77,100 @@ public static KeyValueConfigurationCollection getClientConfig()
7077 /// </param>
7178 public RestApiClient ( IAuthenticator authenticator )
7279 {
73- ApiBasePath = ClientConfig [ ClientConstants . CONFIG_API_BASE_PATH ] . Value ;
80+ SetAppSettings ( ) ;
81+
7482 JsonSerializer = new CallfireJsonConverter ( ) ;
7583 JsonDeserializer = JsonSerializer as IDeserializer ;
7684
77- RestClient = new RestClient ( ApiBasePath ) ;
85+ RestClient = new RestClient ( _ClientConfig . ApiBasePath ) ;
7886 RestClient . Authenticator = authenticator ;
7987 RestClient . UserAgent = this . GetType ( ) . Assembly . GetName ( ) . Name + "-csharp-" + this . GetType ( ) . Assembly . GetName ( ) . Version ;
8088 RestClient . AddHandler ( "application/json" , JsonDeserializer ) ;
8189
90+ Filters = new SortedSet < RequestFilter > ( ) ;
91+
92+ SetUpRestClientProxy ( ) ;
93+ }
94+
95+ /// <summary>
96+ /// Loads client's app settings config section
97+ /// </summary>
98+ public static KeyValueConfigurationCollection LoadAppSettings ( )
99+ {
100+ var path = typeof ( RestApiClient ) . Assembly . Location ;
101+ var config = ConfigurationManager . OpenExeConfiguration ( path ) ;
102+ var appSettings = ( AppSettingsSection ) config . GetSection ( "appSettings" ) ;
103+ return appSettings . Settings ;
104+ }
105+
106+ /// <summary>
107+ /// Set up client's app config parameters from app settings
108+ /// </summary>
109+ private void SetAppSettings ( )
110+ {
111+ //basePath
112+ var basePath = ApplicationConfig [ ClientConstants . CONFIG_API_BASE_PATH ] ;
82113
83- String proxyAddress = ClientConfig [ ClientConstants . PROXY_ADDRESS_PROPERTY ] ? . Value ;
84- String proxyCredentials = ClientConfig [ ClientConstants . PROXY_CREDENTIALS_PROPERTY ] ? . Value ;
114+ if ( basePath == null || string . IsNullOrWhiteSpace ( basePath . Value ) )
115+ {
116+ _ClientConfig . ApiBasePath = ClientConstants . API_BASE_PATH_DEFAULT_VALUE ;
117+ }
118+ else
119+ {
120+ _ClientConfig . ApiBasePath = basePath . Value ;
121+ }
122+
123+ //proxy
124+ String proxyAddress = ApplicationConfig [ ClientConstants . PROXY_ADDRESS_PROPERTY ] ? . Value ;
125+ String proxyCredentials = ApplicationConfig [ ClientConstants . PROXY_CREDENTIALS_PROPERTY ] ? . Value ;
126+ ConfigureProxyParameters ( proxyAddress , proxyCredentials ) ;
127+ }
85128
129+ /// <summary>
130+ /// Configure app proxy parameters
131+ /// </summary>
132+ private void ConfigureProxyParameters ( String proxyAddress , String proxyCredentials )
133+ {
86134 if ( ! String . IsNullOrEmpty ( proxyAddress ) )
87135 {
88136 Logger . Debug ( "Configuring proxy host for client: {} auth: {}" , proxyAddress , proxyCredentials ) ;
89137 char [ ] delimiterChars = { ':' } ;
90138 String [ ] parsedAddress = proxyAddress . Split ( delimiterChars ) ;
91139 String [ ] parsedCredentials = ( proxyCredentials == null ? "" : proxyCredentials ) . Split ( delimiterChars ) ;
92140 int portValue = parsedAddress . Length > 1 ? ClientUtils . StrToIntDef ( parsedAddress [ 1 ] , ClientConstants . DEFAULT_PROXY_PORT ) : ClientConstants . DEFAULT_PROXY_PORT ;
93- WebProxy proxy = new WebProxy ( parsedAddress [ 0 ] , portValue ) ;
94-
141+
95142 if ( ! String . IsNullOrEmpty ( proxyCredentials ) )
96143 {
97144 if ( parsedCredentials . Length > 1 )
98145 {
99- proxy . Credentials = new NetworkCredential ( parsedCredentials [ 0 ] , parsedCredentials [ 1 ] ) ;
146+ _ClientConfig . ProxyAddress = parsedAddress [ 0 ] ;
147+ _ClientConfig . ProxyPort = portValue ;
148+ _ClientConfig . ProxyLogin = parsedCredentials [ 0 ] ;
149+ _ClientConfig . ProxyPassword = parsedCredentials [ 1 ] ;
100150 }
101151 else
102152 {
103153 Logger . Debug ( "Proxy credentials have wrong format, must be username:password" ) ;
104154 }
105155 }
106- RestClient . Proxy = proxy ;
107156 }
108-
109- Filters = new SortedSet < RequestFilter > ( ) ;
110157 }
111158
112159 /// <summary>
113- /// Loads client's app settings config section
160+ /// Set up client's app proxy
114161 /// </summary>
115- public static KeyValueConfigurationCollection LoadAppSettings ( )
162+ private void SetUpRestClientProxy ( )
116163 {
117- var path = typeof ( RestApiClient ) . Assembly . Location ;
118- var config = ConfigurationManager . OpenExeConfiguration ( path ) ;
119- var appSettings = ( AppSettingsSection ) config . GetSection ( "appSettings" ) ;
120- var basePath = appSettings . Settings [ ClientConstants . CONFIG_API_BASE_PATH ] ;
121- if ( basePath == null || string . IsNullOrWhiteSpace ( basePath . Value ) )
164+ if ( ! String . IsNullOrEmpty ( _ClientConfig . ProxyAddress ) )
165+ {
166+ WebProxy proxy = new WebProxy ( _ClientConfig . ProxyAddress , _ClientConfig . ProxyPort ) ;
167+ proxy . Credentials = new NetworkCredential ( _ClientConfig . ProxyLogin , _ClientConfig . ProxyPassword ) ;
168+ RestClient . Proxy = proxy ;
169+ }
170+ else
122171 {
123- throw new CallfireClientException ( "Cannot read " + ClientConstants . CONFIG_API_BASE_PATH +
124- " property from configuration file at: " + path + ".config" ) ;
172+ Logger . Debug ( "Proxy wasn't configured, please check input parameters" ) ;
125173 }
126- return appSettings . Settings ;
127174 }
128175
129176 /// <summary>
@@ -266,7 +313,7 @@ public Stream GetFileData(string path, IEnumerable<KeyValuePair<string, object>>
266313 var queryParams = ClientUtils . BuildQueryParams ( "name" , fileName ) ;
267314 var restRequest = CreateRestRequest ( path , Method . POST , queryParams ) ;
268315 restRequest . AddHeader ( "Content-Type" , "multipart/form-data" ) ;
269- restRequest . AddFileBytes ( "file" , File . ReadAllBytes ( filePath ) , Path . GetFileName ( filePath ) , contentType != null ? contentType : DEFAULT_FILE_CONTENT_TYPE ) ;
316+ restRequest . AddFileBytes ( "file" , File . ReadAllBytes ( filePath ) , Path . GetFileName ( filePath ) , contentType != null ? contentType : ClientConstants . DEFAULT_FILE_CONTENT_TYPE ) ;
270317 restRequest . AddParameter ( "name" , fileName ) ;
271318 return DoRequest < T > ( restRequest ) ;
272319 }
@@ -294,7 +341,7 @@ public Stream GetFileData(string path, IEnumerable<KeyValuePair<string, object>>
294341
295342 var restRequest = CreateRestRequest ( path , Method . POST , queryParams ) ;
296343 restRequest . AddHeader ( "Content-Type" , "multipart/form-data" ) ;
297- restRequest . AddFileBytes ( "file" , File . ReadAllBytes ( filePath ) , Path . GetFileName ( filePath ) , DEFAULT_FILE_CONTENT_TYPE ) ;
344+ restRequest . AddFileBytes ( "file" , File . ReadAllBytes ( filePath ) , Path . GetFileName ( filePath ) , ClientConstants . DEFAULT_FILE_CONTENT_TYPE ) ;
298345 restRequest . AddParameter ( "name" , fileName ) ;
299346 return DoRequest < T > ( restRequest ) ;
300347 }
0 commit comments