3
using System.Collections.Specialized ;
9
/// Configuration settings for the Flickr.Net API Library.
12
/// <p>First, register the configuration section in the configSections section:</p>
13
/// <p><code><configSections>
14
/// <section name="flickrNet" type="FlickrNet.FlickrConfigurationManager,FlickrNet"/>
15
/// </configSections></code>
18
/// Next, include the following config section:
22
/// apiKey="1234567890abc" // optional
23
/// secret="2134123" // optional
24
/// token="234234" // optional
25
/// cacheSize="1234" // optional, in bytes (defaults to 50 * 1024 * 1024 = 50MB)
26
/// cacheTimeout="[d.]HH:mm:ss" // optional, defaults to 1 day (1.00:00:00) - day component is optional
27
/// // e.g. 10 minutes = 00:10:00 or 1 hour = 01:00:00 or 2 days, 12 hours = 2.12:00:00
29
/// <proxy // proxy element is optional, but if included the attributes below are mandatory as mentioned
30
/// ipaddress="127.0.0.1" // mandatory
31
/// port="8000" // mandatory
32
/// username="username" // optional, but must have password if included
33
/// password="password" // optional, see username
34
/// domain="domain" // optional, used for Microsoft authenticated proxy servers
36
/// </flickrNet>
39
internal class FlickrConfigurationSettings
41
#region Private Variables
42
private string _apiKey;
43
private string _apiSecret;
44
private string _apiToken;
45
private int _cacheSize;
46
private TimeSpan _cacheTimeout = TimeSpan.MinValue;
47
private string _proxyAddress;
48
private int _proxyPort;
49
private bool _proxyDefined;
50
private string _proxyUsername;
51
private string _proxyPassword;
52
private string _proxyDomain;
53
private string _cacheLocation;
54
private bool _cacheDisabled;
55
private SupportedService _service;
58
#region FlickrConfigurationSettings Constructor
60
/// Loads FlickrConfigurationSettings with the settings in the config file.
62
/// <param name="configNode">XmlNode containing the configuration settings.</param>
63
public FlickrConfigurationSettings(XmlNode configNode)
65
if( configNode == null ) throw new ArgumentNullException("configNode");
67
foreach(XmlAttribute attribute in configNode.Attributes)
69
switch(attribute.Name)
72
_apiKey = attribute.Value;
75
_apiSecret = attribute.Value;
78
_apiToken = attribute.Value;
83
_cacheDisabled = bool.Parse(attribute.Value);
86
catch(FormatException ex)
88
throw new System.Configuration.ConfigurationException("cacheDisbled should be \"true\" or \"false\"", ex, configNode);
93
_cacheSize = int.Parse(attribute.Value);
96
catch(FormatException ex)
98
throw new System.Configuration.ConfigurationException("cacheSize should be integer value", ex, configNode);
103
_cacheTimeout = TimeSpan.Parse(attribute.Value);
106
catch(FormatException ex)
108
throw new System.Configuration.ConfigurationException("cacheTimeout should be TimeSpan value ([d:]HH:mm:ss)", ex, configNode);
110
case "cacheLocation":
111
_cacheLocation = attribute.Value;
117
_service = (SupportedService)Enum.Parse(typeof(SupportedService), attribute.Value, true);
120
catch(ArgumentException ex)
122
throw new System.Configuration.ConfigurationException("service must be one of the supported services (See SupportedServices enum)", ex, configNode);
126
throw new System.Configuration.ConfigurationException(String.Format("Unknown attribute '{0}' in flickrNet node", attribute.Name), configNode);
130
foreach(XmlNode node in configNode.ChildNodes)
135
ProcessProxyNode(node, configNode);
138
throw new System.Configuration.ConfigurationException(String.Format("Unknown node '{0}' in flickrNet node", node.Name), configNode);
144
#region ProcessProxyNode - Constructor Helper Method
145
private void ProcessProxyNode(XmlNode proxy, XmlNode configNode)
147
if( proxy.ChildNodes.Count > 0 )
148
throw new System.Configuration.ConfigurationException("proxy element does not support child elements");
150
_proxyDefined = true;
151
foreach(XmlAttribute attribute in proxy.Attributes)
154
switch(attribute.Name)
157
_proxyAddress = attribute.Value;
162
_proxyPort = int.Parse(attribute.Value);
164
catch(FormatException ex)
166
throw new System.Configuration.ConfigurationException("proxy port should be integer value", ex, configNode);
170
_proxyUsername = attribute.Value;
173
_proxyPassword = attribute.Value;
176
_proxyDomain = attribute.Value;
179
throw new System.Configuration.ConfigurationException(String.Format("Unknown attribute '{0}' in flickrNet/proxy node", attribute.Name), configNode);
183
if( _proxyAddress == null )
184
throw new System.Configuration.ConfigurationException("proxy ipaddress is mandatory if you specify the proxy element");
185
if( _proxyPort == 0 )
186
throw new System.Configuration.ConfigurationException("proxy port is mandatory if you specify the proxy element");
187
if( _proxyUsername != null && _proxyPassword == null )
188
throw new System.Configuration.ConfigurationException("proxy password must be specified if proxy username is specified");
189
if( _proxyUsername == null && _proxyPassword != null )
190
throw new System.Configuration.ConfigurationException("proxy username must be specified if proxy password is specified");
191
if( _proxyDomain != null && _proxyUsername == null )
192
throw new System.Configuration.ConfigurationException("proxy username/password must be specified if proxy domain is specified");
196
#region Public Properties
198
/// API key. Null if not present. Optional.
202
get { return _apiKey; }
206
/// Shared Secret. Null if not present. Optional.
208
public string SharedSecret
210
get { return _apiSecret; }
214
/// API token. Null if not present. Optional.
216
public string ApiToken
218
get { return _apiToken; }
222
/// Cache size in bytes. 0 if not present. Optional.
224
public bool CacheDisabled
226
get { return _cacheDisabled; }
230
/// Cache size in bytes. 0 if not present. Optional.
234
get { return _cacheSize; }
238
/// Cache timeout. Equals TimeSpan.MinValue is not present. Optional.
240
public TimeSpan CacheTimeout
242
get { return _cacheTimeout; }
245
public string CacheLocation
247
get { return _cacheLocation; }
250
public SupportedService Service
252
get { return _service; }
256
/// If the proxy is defined in the configuration section.
258
public bool IsProxyDefined
260
get { return _proxyDefined; }
264
/// If <see cref="IsProxyDefined"/> is true then this is mandatory.
266
public string ProxyIPAddress
268
get { return _proxyAddress; }
272
/// If <see cref="IsProxyDefined"/> is true then this is mandatory.
276
get { return _proxyPort; }
280
/// The username for the proxy. Optional.
282
public string ProxyUsername
284
get { return _proxyUsername; }
288
/// The password for the proxy. Optional.
290
public string ProxyPassword
292
get { return _proxyPassword; }
296
/// The domain for the proxy. Optional.
298
public string ProxyDomain
300
get { return _proxyDomain; }
b'\\ No newline at end of file'