Added splash gradient
[pithos-ms-client] / trunk / Pithos.Network / RestClient.cs
index a76722d..293518a 100644 (file)
@@ -13,6 +13,7 @@ using System.Runtime.Serialization;
 using System.Threading.Tasks;
 using log4net;
 
+
 namespace Pithos.Network
 {
     using System;
@@ -108,7 +109,7 @@ namespace Pithos.Network
 
         protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
         {
-            return ProcessResponse(()=>base.GetWebResponse(request, result));
+            return ProcessResponse(()=>base.GetWebResponse(request, result)); 
         }
 
         protected override WebResponse GetWebResponse(WebRequest request)
@@ -177,6 +178,7 @@ namespace Pithos.Network
 
         public string DownloadStringWithRetry(string address,int retries=0)
         {
+            
             if (address == null)
                 throw new ArgumentNullException("address");
 
@@ -185,7 +187,6 @@ namespace Pithos.Network
             TraceStart("GET",actualAddress);            
             
             var actualRetries = (retries == 0) ? Retries : retries;
-            
 
             
             var task = Retry(() =>
@@ -219,17 +220,27 @@ namespace Pithos.Network
             RetryWithoutContent(address, retries, "DELETE");
         }
 
-        public string GetHeaderValue(string headerName)
+        public string GetHeaderValue(string headerName,bool optional=false)
         {
             if (this.ResponseHeaders==null)
                 throw new InvalidOperationException("ResponseHeaders are null");
             Contract.EndContractBlock();
 
             var values=this.ResponseHeaders.GetValues(headerName);
-            if (values == null)
-                throw new WebException(String.Format("The {0}  header is missing", headerName));
-            else
+            if (values != null)
                 return values[0];
+
+            if (optional)            
+                return null;            
+            //A required header was not found
+            throw new WebException(String.Format("The {0}  header is missing", headerName));
+        }
+
+        public void SetNonEmptyHeaderValue(string headerName, string value)
+        {
+            if (String.IsNullOrWhiteSpace(value))
+                return;
+            Headers.Add(headerName,value);
         }
 
         private void RetryWithoutContent(string address, int retries, string method)
@@ -439,6 +450,21 @@ namespace Pithos.Network
             var builder = new UriBuilder(String.Join("/", BaseAddress, container, objectName));
             return builder;
         }
+
+        public Dictionary<string, string> GetMeta(string metaPrefix)
+        {
+            if (String.IsNullOrWhiteSpace(metaPrefix))
+                throw new ArgumentNullException("metaPrefix");
+            Contract.EndContractBlock();
+
+            var keys = ResponseHeaders.AllKeys.AsQueryable();
+            var dict = (from key in keys
+                        where key.StartsWith(metaPrefix)
+                        let name = key.Substring(metaPrefix.Length)
+                        select new { Name = name, Value = ResponseHeaders[key] })
+                        .ToDictionary(t => t.Name, t => t.Value);
+            return dict;
+        }
     }
 
     public class RetryException:Exception