Exemple #1
0
        /// <summary>
        /// Makes a Rest request
        /// </summary>
        /// <returns></returns>
        public T MakeReqeust <T>(RestOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            if (string.IsNullOrWhiteSpace(options.Url))
            {
                throw new ArgumentNullOrEmptyException("options.Url");
            }
            T result = default(T);
            MediaTypeWithQualityHeaderValue meadiTypeHeader = RestHelper.ConvertMediaTypeToHeaderValue(MediaType.ApplicationJson);

            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(options.Url);
                client.DefaultRequestHeaders.Accept.Add(meadiTypeHeader);
                HttpResponseMessage response = null;
                switch (options.Method)
                {
                case RestMethod.Get:
                    response = client.GetAsync(options.Url).Result;
                    break;

                case RestMethod.Post:
                case RestMethod.Delete:
                case RestMethod.Head:
                case RestMethod.Options:
                case RestMethod.Put:
                case RestMethod.Trace:
                // For future usage.
                default:
                    break;
                }

                if (response.IsSuccessStatusCode)
                {
                    result = response.Content.ReadAsAsync <T>().Result;
                }
            }
            return(result);
        }
Exemple #2
0
 /// <summary>
 /// Initialize new instance of RestClient with RestOptions
 /// </summary>
 public RestClient(RestOptions arguments)
 {
     Arguments = arguments;
 }