using System.Threading.Tasks;
using log4net;
+
namespace Pithos.Network
{
using System;
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)
public string DownloadStringWithRetry(string address,int retries=0)
{
+
if (address == null)
throw new ArgumentNullException("address");
TraceStart("GET",actualAddress);
var actualRetries = (retries == 0) ? Retries : retries;
-
var task = Retry(() =>
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)
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