public async Task<EntityVersion<WebResourceName, string>> TryUpdateEntity (WebResourceName url, string etag, string contents)
{
s_logger.DebugFormat ("Updating entity '{0}'", url);
var absoluteContactUrl = new Uri (_serverUrl, url.OriginalAbsolutePath);
s_logger.DebugFormat ("Absolute entity location: '{0}'", absoluteContactUrl);
IHttpHeaders responseHeaders;
try
{
responseHeaders = await _webDavClient.ExecuteWebDavRequestAndReturnResponseHeaders (
absoluteContactUrl,
"PUT",
null,
etag,
null,
"text/vcard",
contents);
}
catch (WebDavClientException x) when (x.StatusCode == HttpStatusCode.NotFound || x.StatusCode == HttpStatusCode.PreconditionFailed)
{
return null;
}
if (s_logger.IsDebugEnabled)
s_logger.DebugFormat ("Updated entity. Server response header: '{0}'", responseHeaders.ToString().Replace ("\r\n", " <CR> "));
Uri effectiveContactUrl;
if (responseHeaders.Location != null)
{
s_logger.DebugFormat ("Server sent new location: '{0}'", responseHeaders.Location);
effectiveContactUrl = responseHeaders.Location.IsAbsoluteUri ? responseHeaders.Location : new Uri (_serverUrl, responseHeaders.Location);
s_logger.DebugFormat ("New entity location: '{0}'", effectiveContactUrl);
}
else
{
effectiveContactUrl = absoluteContactUrl;
}
var newEtag = responseHeaders.ETag;
string version;
if (newEtag != null)
{
version = newEtag;
}
else
{
version = await GetEtag (effectiveContactUrl);
}
return new EntityVersion<WebResourceName, string> (new WebResourceName(effectiveContactUrl), version);
}