Beispiel #1
0
        public HttpRequestBuilder UseQuery(string query)
        {
            ArgumentCheck.ThrowIfNull(query, nameof(query));

            _requestUriBuilder.Query = query;
            return(this);
        }
Beispiel #2
0
        public HttpRequestBuilder UseRequestUri(Uri requestUri)
        {
            ArgumentCheck.ThrowIfNull(requestUri, nameof(requestUri));

            _requestUriBuilder = new UriBuilder(requestUri);
            return(this);
        }
Beispiel #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ZammadAccount"/> class.
        /// </summary>
        /// <param name="endpoint">The endpoint of the Zammad instance being used.</param>
        /// <param name="authentication">Specifies the authentication method who is used for authentication.</param>
        /// <param name="user">The user who is used for authentication.</param>
        /// <param name="password">The password who is used for authentication.</param>
        /// <param name="token">The token who is used for authentication.</param>
        public ZammadAccount(Uri endpoint, ZammadAuthentication authentication, string user, string password, string token)
        {
            ArgumentCheck.ThrowIfNull(endpoint, nameof(endpoint));

            switch (authentication)
            {
            case ZammadAuthentication.Basic:
            {
                ArgumentCheck.ThrowIfNullOrEmpty(user, nameof(user));
                ArgumentCheck.ThrowIfNullOrEmpty(password, nameof(password));
                break;
            }

            case ZammadAuthentication.Token:
            {
                ArgumentCheck.ThrowIfNullOrEmpty(token, nameof(token));
                break;
            }

            default:
            {
                throw new NotSupportedException($"Authentication \"{authentication}\" is not supported.");
            }
            }

            Endpoint       = endpoint;
            Authentication = authentication;
            Token          = token;
            User           = user;
            Password       = password;
        }
Beispiel #4
0
        public static void CopyProperties <T>(T from, T to)
        {
            ArgumentCheck.ThrowIfNull(from, nameof(from));
            ArgumentCheck.ThrowIfNull(to, nameof(to));

            var fromType = from.GetType();
            var toType   = to.GetType();

            if (toType.IsSubclassOf(fromType) == false)
            {
                throw new ArgumentException($"The type of the argument \"{nameof(to)}\" must be derived from the type of the argument \"{nameof(from)}\".");
            }

            foreach (var property in fromType.GetProperties())
            {
                if (property.CanRead)
                {
                    var value = property.GetValue(from);
                    if (property.CanWrite)
                    {
                        property.SetValue(to, value);
                    }
                }
            }
        }
Beispiel #5
0
        public HttpRequestBuilder AddQuery(string key, string value)
        {
            ArgumentCheck.ThrowIfNullOrEmpty(key, nameof(key));
            ArgumentCheck.ThrowIfNull(value, nameof(value));

            var queryBuilder = new StringBuilder(_requestUriBuilder.Query);

            if (queryBuilder.Length == 0)
            {
                queryBuilder.Append('?');
            }
            else
            {
                queryBuilder.Append('&');
            }
            queryBuilder.AppendFormat("{0}={1}", key, value);
            _requestUriBuilder.Query = queryBuilder.ToString();
            return(this);
        }
Beispiel #6
0
        public HttpRequestBuilder UseJsonContent(object json)
        {
            ArgumentCheck.ThrowIfNull(json, nameof(json));

            var jsonBuilder = new StringBuilder();

            using (var stringWriter = new StringWriter(jsonBuilder))
                using (var jsonWriter = new JsonTextWriter(stringWriter))
                {
                    var serializer = new JsonSerializer
                    {
                        NullValueHandling = NullValueHandling.Ignore
                    };

                    serializer.Serialize(jsonWriter, json);
                }
            _content = new StringContent(jsonBuilder.ToString());
            _content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            return(this);
        }
 public HttpResponseParser UseHttpResponse(HttpResponseMessage httpResponse)
 {
     ArgumentCheck.ThrowIfNull(httpResponse, nameof(httpResponse));
     _httpResponse = httpResponse;
     return(this);
 }