コード例 #1
0
        /// <summary>
        /// Execute (POST).
        /// </summary>
        /// <typeparam name="TReturn">The Returns object type.</typeparam>
        /// <typeparam name="TReturn">The Out object type.</typeparam>
        /// <param name="apiUrl">The action api url.</param>
        /// <param name="pObj">The parameter.</param>
        /// <param name="timeout">The timeout in ms.</param>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <returns>
        /// Returns instance of NRestResult.
        /// </returns>
        public NRestResult <TReturn, TOut> Execute <TReturn, TOut>(string apiUrl,
                                                                   object pObj     = null,
                                                                   int timeout     = 1000,
                                                                   string username = "", string password = "")
            where TReturn : new()
            where TOut : new()
        {
            NRestResult <TReturn, TOut> ret = new NRestResult <TReturn, TOut>();

            string     actionUrl = (!apiUrl.StartsWith("/")) ? @"/" + apiUrl : apiUrl;
            MethodBase med       = MethodBase.GetCurrentMethod();

            try
            {
                med.Info("api: {0}", BaseUrl + actionUrl);
                if (null != pObj)
                {
                    med.Info("body: {0}", pObj.ToJson(true));
                }

                var client = new RestClient(BaseUrl);
                //client.ReadWriteTimeout = timeout;
                client.Timeout = timeout;
                if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password))
                {
                    client.Authenticator = new HttpBasicAuthenticator(username, password);
                }
                //client.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.BypassCache);
                client.UseNewtonsoftJson(NJson.DefaultSettings);
                var request = new RestRequest(actionUrl, Method.POST);
                request.RequestFormat = DataFormat.Json;
                if (null != pObj)
                {
                    request.AddJsonBody(pObj);
                }

                var response = client.Execute(request);
                if (null != response)
                {
                    med.Info("result: {0}", response.Content);

                    if (response.IsSuccessful() && null != response.Content)
                    {
                        var obj = response.Content.FromJson <NDbResult <TReturn, TOut> >();
                        if (null != obj && obj.GetType() == typeof(NDbResult <TReturn, TOut>))
                        {
                            var dbRet = obj;
                            ret = dbRet.ToRest();
                        }
                        else
                        {
                            ret.data = (null != obj) ? obj.data : NDbResult <TReturn, TOut> .DefaultData();

                            ret.output = (null != obj) ? obj.output : NDbResult <TReturn, TOut> .DefaultOutput();
                        }
                    }
                    else
                    {
                        ret.RestResponseError();
                        string msg = string.Format(
                            "Rest Client Content Error - Code: {0}, Content: {1}",
                            (int)response.StatusCode, response.Content);
                        Console.WriteLine(msg);
                        med.Err(msg);
                    }
                }
                else
                {
                    ret.RestConenctFailed();
                }
            }
            catch (Exception ex)
            {
                med.Err(ex);
                ret.Error(ex);
            }

            return(ret);
        }