Ejemplo n.º 1
0
        string MapLinkForRequest(NewLink link)
        {
            var builder = new StringBuilder();

            builder
            .Append("{")
            .Append("\"path\" : \"" + link.Path + "\",")
            .Append("\"type\" : \"" + MapLinkType(link.Type) + "\",")
            .Append("\"accessibility\" : \"" + MapAccessibilityType(link.Accessibility) + "\"");

            if (link.SendEmail.HasValue)
            {
                builder.AppendFormat(@", ""send_email"" : ""{0}""", link.SendEmail.Value ? "true" : "false");
            }

            if (link.Recipients.Count > 0)
            {
                builder.Append(", \"recipients\": [");
                builder.Append(string.Join(", ", link.Recipients.Select(r => "\"" + r + "\"")));
                builder.Append("]");
            }

            if (!string.IsNullOrWhiteSpace(link.Message))
            {
                builder.Append(", \"message\": \"" + link.Message + "\"");
            }

            if (link.CopyMe.HasValue)
            {
                builder.AppendFormat(@", ""copy_me"": ""{0}""", link.CopyMe.Value ? "true" : "false");
            }

            if (link.Notify.HasValue)
            {
                builder.AppendFormat(@", ""notify"": ""{0}""", link.Notify.Value ? "true" : "false");
            }

            if (link.LinkToCurrent.HasValue)
            {
                builder.AppendFormat(@", ""link_to_current"": ""{0}""", link.LinkToCurrent.Value ? "true" : "false");
            }

            if (link.ExpiryDate.HasValue)
            {
                builder.AppendFormat(@", ""expiry_date"": ""{0}""", link.ExpiryDate.Value.ToString("yyyy-MM-dd"));
            }

            if (link.ExpiryClicks.HasValue)
            {
                builder.Append(", \"expiry_clicks\": \"" + link.ExpiryClicks.Value + "\"");
            }

            builder.Append("}");

            return(builder.ToString());
        }
Ejemplo n.º 2
0
        void ThrowExceptionsIfNewLinkIsInvalid(NewLink link)
        {
            if (link == null)
            {
                throw new ArgumentNullException(nameof(link));
            }

            if (string.IsNullOrWhiteSpace(link.Path))
            {
                throw new ArgumentNullException(nameof(link.Path));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates link
        /// </summary>
        /// <param name="link">Parameters</param>
        /// <returns>Created link details</returns>
        public async Task <CreatedLink> CreateLink(NewLink link)
        {
            ThrowExceptionsIfNewLinkIsInvalid(link);

            if (!link.Path.StartsWith("/", StringComparison.Ordinal))
            {
                link.Path = "/" + link.Path;
            }

            var uriBuilder  = BuildUri(LinkMethod);
            var httpRequest = new HttpRequestMessage(HttpMethod.Post, uriBuilder.Uri)
            {
                Content = new StringContent(MapLinkForRequest(link), Encoding.UTF8, "application/json")
            };

            var serviceHandler = new ServiceHandler <CreatedLinkResponse>(httpClient);
            var response       = await serviceHandler.SendRequestAsync(httpRequest).ConfigureAwait(false);

            return(LinksHelper.MapFlatCreatedLinkToCreatedLink(response.Data));
        }