private static void VerifyParameters(HttpSettings settings, string name, string value)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentNullException(nameof(value));
            }
        }
        /// <summary>
        /// Adds a "Basic" Authorization Header to the request.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="userName">The userName used to authorize to the resource.</param>
        /// <param name="password">The credentials containing the authentication information of the user agent for</param>
        /// <returns>The same <see cref="HttpSettings"/> instance so that multiple calls can be chained.</returns>
        public static HttpSettings UseBasicAuthorization(this HttpSettings settings, string userName, string password)
        {
            if (string.IsNullOrWhiteSpace(userName))
            {
                throw new ArgumentNullException(nameof(userName));
            }

            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentNullException(nameof(password));
            }

            var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(userName + ":" + password));

            settings.SetAuthorization("Basic", credentials);
            return(settings);
        }
Beispiel #3
0
        /// <summary>
        /// Sets the request body from an object. Serialized as JSON
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="data">The object to set as the request body. It will be serialized to JSON.</param>
        /// <param name="indentOutput">Option to indent the output of the format</param>
        /// <remarks>
        /// This uses the JavascriptSerializer
        /// </remarks>
        /// <returns>The same <see cref="HttpSettings"/> instance so that multiple calls can be chained.</returns>
        public static HttpSettings SetJsonRequestBody <T>(this HttpSettings settings, T data, bool indentOutput = true)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var requestBody = JsonEncoder.SerializeObject(data);

            settings.RequestBody = Encoding.UTF8.GetBytes(requestBody);
            settings.SetContentType("application/json");
            return(settings);
        }
Beispiel #4
0
        /// <summary>
        /// Sets the request body as form url encoded.
        /// Only valid for Http Methods that allow a request body.
        /// Any existing content in the RequestBody is overriden.
        /// Accepts multiple parameters with the same key.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="data">Enumerable of <see cref="KeyValuePair{TKey,TValue}"/> of data to url encode and set to the body.</param>
        /// <returns>The same <see cref="HttpSettings"/> instance so that multiple calls can be chained.</returns>
        public static HttpSettings SetFormUrlEncodedRequestBody(this HttpSettings settings,
                                                                IEnumerable <KeyValuePair <string, string> > data)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            settings.RequestBody = new FormUrlEncodedContent(data).ReadAsByteArrayAsync().Result;
            settings.SetContentType("application/x-www-form-urlencoded");

            return(settings);
        }
        public static void HttpDelete(this ICakeContext context, string address, HttpSettings settings)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (string.IsNullOrWhiteSpace(address))
            {
                throw new ArgumentNullException(nameof(address));
            }

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            var client   = GetHttpClient(context, settings);
            var response = client.DeleteAsync(address).Result;
        }
Beispiel #6
0
        /// <summary>
        /// Appends the cookie to the settings cookie collection
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="name">name of the cookie</param>
        /// <param name="value">value to cookie</param>
        /// <returns>The same <see cref="HttpSettings"/> instance so that multiple calls can be chained.</returns>
        public static HttpSettings AppendCookie(this HttpSettings settings, string name, string value)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentNullException(nameof(value));
            }

            settings.Cookies[name] = value;

            return(settings);
        }
Beispiel #7
0
        /// <summary>
        /// Sets the request body from an object. Serialized as JSON
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="data">The object to set as the request body. It will be serialized to JSON.</param>
        /// <param name="indentOutput">Option to indent the output of the format</param>
        /// <remarks>
        /// This uses the JavascriptSerializer
        /// </remarks>
        /// <returns>The same <see cref="HttpSettings"/> instance so that multiple calls can be chained.</returns>
        public static HttpSettings SetJsonRequestBody <T>(this HttpSettings settings, T data, bool indentOutput = true)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var requestBody = new JavaScriptSerializer().Serialize(data);

            if (indentOutput)
            {
                requestBody = FormatJsonOutput(requestBody);
            }

            settings.RequestBody = Encoding.UTF8.GetBytes(requestBody);
            settings.SetContentType("application/json");
            return(settings);
        }
Beispiel #8
0
        public static byte[] HttpPutAsByteArray(this ICakeContext context, string address, HttpSettings settings)
        {
            VerifyParameters(context, address, settings);

            var client   = GetHttpClient(context, settings);
            var response = client.PutAsync(address, new ByteArrayContent(settings.RequestBody)).Result;

            var result = response.Content.ReadAsByteArrayAsync().Result;

            return(result);
        }
Beispiel #9
0
 /// <summary>
 /// Appends a Cache-Control header with no-store
 /// </summary>
 /// <param name="settings">The settings.</param>
 /// <returns>The same <see cref="HttpSettings"/> instance so that multiple calls can be chained.</returns>
 public static HttpSettings SetNoCache(this HttpSettings settings)
 {
     return(settings.AppendHeader("Cache-Control", "no-store"));
 }
Beispiel #10
0
 public static string HttpGet(this ICakeContext context, string address, HttpSettings settings)
 {
     return(Encoding.UTF8.GetString(HttpGetAsByteArray(context, address, settings)));
 }
Beispiel #11
0
 /// <summary>
 /// Gets an <see cref="HttpClient"/> pre-populated with the correct default/
 /// The returned client should be disposed of by the caller.
 /// </summary>
 /// <param name="context">The current Cake context.</param>
 /// <param name="settings">HttpSettings to apply to the HttpClient.</param>
 /// <returns>A <see cref="HttpClient"/> instance.</returns>
 private static HttpClient GetHttpClient(ICakeContext context, HttpSettings settings)
 {
     return(new HttpClient(new CakeHttpClientHandler(context, settings)));
 }
Beispiel #12
0
        public static byte[] HttpSendAsByteArray(this ICakeContext context, string address, string httpMethod, HttpSettings settings)
        {
            VerifyParameters(context, address, settings);

            if (string.IsNullOrWhiteSpace(httpMethod))
            {
                throw new ArgumentNullException(nameof(httpMethod));
            }

            var client = GetHttpClient(context, settings);

            var request = new HttpRequestMessage
            {
                Method     = new HttpMethod(httpMethod),
                RequestUri = new Uri(address),
                Content    = (settings.RequestBody != null && settings.RequestBody.Length > 0) ?  new ByteArrayContent(settings.RequestBody) : null
            };

            var response = client.SendAsync(request).Result;

            var result = response.Content.ReadAsByteArrayAsync().Result;

            return(result);
        }
Beispiel #13
0
        public static byte[] HttpPatchAsByteArray(this ICakeContext context, string address, HttpSettings settings)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (string.IsNullOrWhiteSpace(address))
            {
                throw new ArgumentNullException(nameof(address));
            }

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            return(HttpSendAsByteArray(context, address, "PATCH", settings));
        }
        public static byte[] HttpPutAsByteArray(this ICakeContext context, string address, HttpSettings settings)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (string.IsNullOrWhiteSpace(address))
            {
                throw new ArgumentNullException(nameof(address));
            }

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            var client   = GetHttpClient(context, settings);
            var response = client.PutAsync(address, new ByteArrayContent(settings.RequestBody)).Result;

            var result = response.Content.ReadAsByteArrayAsync().Result;

            return(result);
        }
Beispiel #15
0
 /// <summary>
 /// Sets the request body as form url encoded.
 /// Only valid for Http Methods that allow a request body.
 /// Any existing content in the RequestBody is overriden.
 /// </summary>
 /// <param name="settings">The settings.</param>
 /// <param name="data">Dictionary of data to url encode and set to the body.</param>
 /// <returns>The same <see cref="HttpSettings"/> instance so that multiple calls can be chained.</returns>
 public static HttpSettings SetFormUrlEncodedRequestBody(this HttpSettings settings,
                                                         IDictionary <string, string> data)
 => SetFormUrlEncodedRequestBody(settings, data?.AsEnumerable());
        public static byte[] HttpPatchAsByteArray(this ICakeContext context, string address, HttpSettings settings)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (string.IsNullOrWhiteSpace(address))
            {
                throw new ArgumentNullException(nameof(address));
            }

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            var request = new HttpRequestMessage(new HttpMethod("PATCH"), address)
            {
                Content = new ByteArrayContent(settings.RequestBody)
            };

            var client = GetHttpClient(context, settings);

            var response = client.SendAsync(request).Result;

            var result = response.Content.ReadAsByteArrayAsync().Result;

            return(result);
        }
        public static byte[] HttpGetAsByteArray(this ICakeContext context, string address, HttpSettings settings)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (string.IsNullOrWhiteSpace(address))
            {
                throw new ArgumentNullException(nameof(address));
            }

            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            var client = GetHttpClient(context, settings);
            var result = client.GetByteArrayAsync(address).Result;

            return(result);
        }
Beispiel #18
0
        public static byte[] HttpGetAsByteArray(this ICakeContext context, string address, HttpSettings settings)
        {
            VerifyParameters(context, address, settings);

            var client = GetHttpClient(context, settings);
            var result = client.GetByteArrayAsync(address).Result;

            return(result);
        }
 /// <summary>
 ///  Adds client certificate(s) to the http handler.
 /// </summary>
 /// <param name="settings">The settings.</param>
 /// <param name="clientCertificates">Client certificates to include in requests.</param>
 /// <returns></returns>
 public static HttpSettings UseClientCertificates(this HttpSettings settings, params X509Certificate2[] clientCertificates)
 {
     return(settings.UseClientCertificates((IEnumerable <X509Certificate2>)clientCertificates));
 }