/// <summary>
        /// 把http请求转换到搜索请求
        /// </summary>
        /// <param name="request">http请求</param>
        /// <param name="defaultPageSize">默认的每页数量,默认是50</param>
        /// <returns></returns>
        public static StaticTableSearchRequest FromHttpRequest(
            IHttpRequest request, int?defaultPageSize = null)
        {
            var searchRequest = new StaticTableSearchRequest();
            var pageNo        = request.Get <string>(UrlPagination.UrlParam, null);

            searchRequest.PageNo = ((pageNo == UrlPagination.LastPageAlias) ?
                                    UrlPagination.LastPageNo : pageNo.ConvertOrDefault <int>(1));
            searchRequest.PageSize = request.Get(PageSizeKey, defaultPageSize ?? 50);
            searchRequest.Keyword  = request.Get <string>(KeywordKey);
            foreach (var pair in request.GetAll())
            {
                searchRequest.Conditions[pair.First] = pair.Second[0];
            }
            return(searchRequest);
        }
        public async Task TestGetRequestNotFound()
        {
            SystemNetHttpServiceModule module = new SystemNetHttpServiceModule();

            IHttpRequest request = module.NewRequest();

            IHttpResponse response = await request.Get("http://does.not.exist.com").Execute();

            Assert.IsFalse(response.Successful);
            Assert.NotNull(response.Error);
        }
Example #3
0
 /// <summary>
 /// Get all parameters into a given type
 /// </summary>
 /// <typeparam name="T">The type contains parameters</typeparam>
 /// <param name="request">Http request</param>
 /// <returns></returns>
 public static T GetAllAs <T>(this IHttpRequest request)
 {
     if (request.ContentType?.StartsWith("application/json") ?? false)
     {
         // Deserialize with json
         // It should only read once
         var json = (string)request.HttpContext.Items.GetOrCreate(
             "__json_body", () => new StreamReader(request.Body).ReadToEnd());
         return(JsonConvert.DeserializeObject <T>(json));
     }
     else if (typeof(T) == typeof(IDictionary <string, object>) ||
              typeof(T) == typeof(Dictionary <string, object>))
     {
         // Return all parameters
         return((T)(object)request.GetAllDictionary().ToDictionary(
                    p => p.Key, p => (object)p.Value.FirstOrDefault()));
     }
     else if (typeof(T) == typeof(IDictionary <string, string>) ||
              typeof(T) == typeof(Dictionary <string, string>))
     {
         // Return all parameters
         return((T)(object)request.GetAllDictionary().ToDictionary(
                    p => p.Key, p => (string)p.Value.FirstOrDefault()));
     }
     else
     {
         // Get each property by it's name
         var value = (T)Activator.CreateInstance(typeof(T));
         foreach (var property in typeof(T).FastGetProperties())
         {
             if (!property.CanRead || !property.CanWrite)
             {
                 continue;                         // Property is read or write only
             }
             object propertyValue;
             if (property.PropertyType == typeof(IHttpPostedFile))
             {
                 propertyValue = request.GetPostedFile(property.Name);
             }
             else
             {
                 propertyValue = request.Get <string>(property.Name)
                                 .ConvertOrDefault(property.PropertyType, null);
             }
             if (propertyValue != null)
             {
                 property.FastSetValue(value, propertyValue);
             }
         }
         return(value);
     }
 }
        public async Task TestGetRequestSuccessfulNetwork()
        {
            SystemNetHttpServiceModule module = new SystemNetHttpServiceModule();

            IHttpRequest request = module.NewRequest();

            IHttpResponse response = await request.Get("http://www.mocky.io/v2/5a5f74172e00006e260a8476").Execute();

            Assert.NotNull(response);
            Assert.Null(response.Error);
            Assert.IsTrue(response.Successful);
            Assert.AreEqual("{\n" + " \"story\": {\n"
                            + "     \"title\": \"Test Title\"\n" + " }    \n" + "}", response.Body);
        }
        public async Task TestGetRequestSuccessfulLocal()
        {
            SystemNetHttpServiceModule module = new SystemNetHttpServiceModule();

            IHttpRequest request = module.NewRequest();

            IHttpResponse response = await request.Get(localPath(GET_TEST_PATH)).Execute();

            Assert.NotNull(response);
            Assert.Null(response.Error);
            Assert.IsTrue(response.Successful);
            Assert.AreEqual(200, response.StatusCode);
            Assert.AreEqual(GET_TEST_BODY, response.Body);
            var jsonObject = JsonObject.Parse(response.Body);

            Assert.AreEqual(HELLO_WORLD, (string)jsonObject["text"]);
        }
Example #6
0
        /// <summary>
        /// Get all parameters into a given type<br/>
        /// 获取Http请求中的所有参数, 以指定类型返回<br/>
        /// </summary>
        /// <typeparam name="T">The type contains parameters</typeparam>
        /// <param name="request">Http request</param>
        /// <returns></returns>
        /// <example>
        /// <code language="cs">
        /// var request = HttpManager.CurrentContext.Request;
        /// var result = request.GetAllAs&lt;TestData&gt;();
        /// </code>
        /// </example>
        public static T GetAllAs <T>(this IHttpRequest request)
        {
            var jsonBody = request.GetJsonBody();

            if (!string.IsNullOrEmpty(jsonBody))
            {
                // Deserialize with json
                return(JsonConvert.DeserializeObject <T>(jsonBody));
            }
            else if (typeof(T) == typeof(IDictionary <string, object>) ||
                     typeof(T) == typeof(Dictionary <string, object>))
            {
                // Return all parameters
                return((T)(object)request.GetAllDictionary().ToDictionary(
                           p => p.Key, p => (object)p.Value.FirstOrDefault()));
            }
            else if (typeof(T) == typeof(IDictionary <string, string>) ||
                     typeof(T) == typeof(Dictionary <string, string>))
            {
                // Return all parameters
                return((T)(object)request.GetAllDictionary().ToDictionary(
                           p => p.Key, p => (string)p.Value.FirstOrDefault()));
            }
            else
            {
                // Get each property by it's name
                var value = (T)Activator.CreateInstance(typeof(T));
                foreach (var property in typeof(T).FastGetProperties())
                {
                    if (!property.CanRead || !property.CanWrite)
                    {
                        continue;                         // Property is read or write only
                    }
                    var propertyValue = request.Get <object>(property.Name)
                                        .ConvertOrDefault(property.PropertyType, null);
                    if (propertyValue != null)
                    {
                        property.FastSetValue(value, propertyValue);
                    }
                }
                return(value);
            }
        }
Example #7
0
        //public HttpContent Delete<Parameter>(Uri _url, Parameter parameter)
        //{
        //    return _http.Delete(_url, CreateBody.Body(parameter));
        //}

        #endregion

        #region Get
        public HttpContent Get(KeyValuePair <string, string> action)
        {
            return(_http.Get(BuildUri.Build(action)));
        }
Example #8
0
 public HttpContent Get <Parameter>(IHttpRequest header, KeyValuePair <string, string> action, Parameter parameter)
 {
     return(header.Get(BuildUri.Build(action, parameter)));
 }
Example #9
0
 public HttpContent Get(IHttpRequest header, KeyValuePair <string, string> action)
 {
     return(header.Get(BuildUri.Build(action)));
 }
 public async Task <string> GetForcast()
 {
     return(await _httpRequest.Get($"http://localhost:5100/Forecast"));
 }