private async static Task <HttpResponseMessage> RequestAsync(DavRequest davRequest)
        {
            try
            {
                using (var hc = new HttpClient())
                {
                    hc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(davRequest.Authorization)));
                    if (!String.IsNullOrEmpty(davRequest.Header))
                    {
                        hc.DefaultRequestHeaders.Add("X_REWRITER_URL", davRequest.Header);
                    }
                    var method  = new HttpMethod(davRequest.Method);
                    var request = new HttpRequestMessage(method, davRequest.Url);

                    if (davRequest.Data != null)
                    {
                        request.Content = new StringContent(davRequest.Data);
                    }
                    return(await hc.SendAsync(request).ConfigureAwait(false));
                }
            }
            catch (AggregateException ex)
            {
                throw new RadicaleException(ex.Message);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                throw new RadicaleException(ex.Message);
            }
        }
        public async static Task <DavResponse> UpdateAsync(DavRequest davRequest)
        {
            davRequest.Method = "PROPPATCH";
            var response = await RequestAsync(davRequest).ConfigureAwait(false);

            return(GetDavResponse(response));
        }
        public async Task <DavResponse> GetCollection(string url, string authorization)
        {
            var davRequest = new DavRequest()
            {
                Url           = url,
                Authorization = authorization
            };

            return(await RadicaleClient.GetAsync(davRequest).ConfigureAwait(false));
        }
        public async Task <DavResponse> UpdateItem(string url, string authorization, string data, string headerUrl = "")
        {
            var davRequest = new DavRequest()
            {
                Url           = url,
                Authorization = authorization,
                Header        = headerUrl,
                Data          = data
            };

            return(await RadicaleClient.UpdateItemAsync(davRequest).ConfigureAwait(false));
        }
        public async Task <DavResponse> GetCollection(string url, string authorization, string myUri)
        {
            var path           = (new Uri(url).AbsolutePath.StartsWith("/carddav")) ? (new Uri(url).AbsolutePath.Remove(0, 8)) : new Uri(url).AbsolutePath;
            var defaultUrlconn = defaultRadicaleUrl + path;
            var davRequest     = new DavRequest()
            {
                Url           = defaultUrlconn,
                Authorization = authorization,
                Header        = myUri
            };

            return(await RadicaleClient.GetAsync(davRequest).ConfigureAwait(false));
        }
        public async Task <DavResponse> Update(string name, string description, string backgroundColor, Uri uri, string userName, string authorization)
        {
            var requestUrl = GetRadicaleUrl(uri.ToString(), userName, IsShared, isRedirectUrl: true, entityId: Uid);

            var davRequest = new DavRequest()
            {
                Url           = requestUrl,
                Authorization = authorization,
                Data          = GetData(strUpdateTemplate, name, description, backgroundColor)
            };

            return(await RadicaleClient.UpdateAsync(davRequest).ConfigureAwait(false));
        }
        public async Task <DavResponse> UpdateItem(string url, string authorization, string data, string headerUrl = "")
        {
            var path       = (new Uri(url).AbsolutePath.StartsWith("/carddav")) ? (new Uri(url).AbsolutePath.Remove(0, 8)) : new Uri(url).AbsolutePath;
            var requrl     = defaultRadicaleUrl + path;
            var davRequest = new DavRequest()
            {
                Url           = requrl,
                Authorization = authorization,
                Header        = headerUrl,
                Data          = data
            };

            return(await RadicaleClient.UpdateItemAsync(davRequest).ConfigureAwait(false));
        }
        public async Task <DavResponse> Create(string name, string description, string backgroundColor, string uri, string authorization, bool isReadonly = true)
        {
            var rewriterUri = uri.StartsWith("http") ? uri : "";

            var davRequest = new DavRequest()
            {
                Url           = uri,
                Authorization = authorization,
                Header        = rewriterUri,
                Data          = GetData(strTemplate, name, description, backgroundColor)
            };

            return(await RadicaleClient.CreateAsync(davRequest).ConfigureAwait(false));
        }
        public async Task <DavResponse> Update(string name, string description, string backgroundColor, string uri, string userName, string authorization, bool isReadonly = true)
        {
            var addbookId = isReadonly ? readonlyAddBookName : defaultAddBookName;

            var header = uri.StartsWith("http") ? uri : "";

            var requestUrl = defaultRadicaleUrl + "/" + HttpUtility.UrlEncode(userName) + "/" + addbookId;

            var davRequest = new DavRequest()
            {
                Url           = requestUrl,
                Authorization = authorization,
                Data          = GetData(strTemplate, name, description, backgroundColor),
                Header        = header
            };

            return(await RadicaleClient.UpdateAsync(davRequest).ConfigureAwait(false));
        }
        public async Task <DavResponse> CreateAsync(string name, string description, string backgroundColor, Uri uri, string userName, string authorization)
        {
            var requestUrl = uri.Scheme + "://" + uri.Host + "/caldav/" + HttpUtility.UrlEncode(userName) + "/" + Uid + (IsShared ? "-readonly" : "");

            var davRequest = new DavRequest()
            {
                Url           = requestUrl,
                Authorization = authorization,
                Data          = GetData(strCreateTemplate, name, description, backgroundColor)
            };

            try
            {
                return(await RadicaleClient.CreateAsync(davRequest).ConfigureAwait(false));
            }
            catch (Exception ex)
            {
                throw new RadicaleException(ex.Message);
            }
        }
        public void Delete(string uri, Guid userID, string email, int tenantId = 0)
        {
            var authorization = GetSystemAuthorization();
            var deleteUrlBook = GetRadicaleUrl(uri, email.ToLower(), true, true);
            var davRequest    = new DavRequest()
            {
                Url           = deleteUrlBook,
                Authorization = authorization
            };

            try
            {
                RadicaleClient.RemoveAsync(davRequest).ConfigureAwait(false);
                var dbConn = new DbRadicale();
                dbConn.RemoveCardDavUser(tenantId, userID.ToString());
            }
            catch (Exception ex)
            {
                Logger.Error("ERROR: " + ex.Message);
            }
        }
        public void UpdateItemForAllAddBooks(List <string> emailList, string uri, CardDavItem user, int tenantId = 0, string changedEmail = null)
        {
            var authorization = GetSystemAuthorization();

            if (changedEmail != null)
            {
                var deleteUrlBook = GetRadicaleUrl(uri, changedEmail.ToLower(), true, true);
                var davRequest    = new DavRequest()
                {
                    Url           = deleteUrlBook,
                    Authorization = authorization
                };
                RadicaleClient.RemoveAsync(davRequest).ConfigureAwait(false);

                try
                {
                    var dbConn = new DbRadicale();
                    dbConn.RemoveCardDavUser(tenantId, user.ID.ToString());
                }
                catch (Exception ex)
                {
                    Logger.Error("ERROR: " + ex.Message);
                }
            }

            foreach (string email in emailList)
            {
                try
                {
                    var currentEmail = email.ToLower();
                    var userData     = GetUserSerialization(user);
                    var requestUrl   = GetRadicaleUrl(uri, currentEmail, true, true, itemID: user.ID.ToString());
                    UpdateItem(requestUrl, authorization, userData, uri).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    Logger.Error("ERROR: " + ex.Message);
                }
            }
        }
        public async static Task <DavResponse> GetAsync(DavRequest davRequest)
        {
            davRequest.Method = "GET";
            var response = await RequestAsync(davRequest).ConfigureAwait(false);

            var davResponse = new DavResponse()
            {
                StatusCode = (int)response.StatusCode
            };

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                davResponse.Completed = true;
                davResponse.Data      = await response.Content.ReadAsStringAsync();
            }
            else
            {
                davResponse.Completed = false;
                davResponse.Error     = response.ReasonPhrase;
            }

            return(davResponse);
        }
 public async static Task RemoveAsync(DavRequest davRequest)
 {
     davRequest.Method = "DELETE";
     await RequestAsync(davRequest).ConfigureAwait(false);
 }