Exemplo n.º 1
0
        //static public Task<T> ExecuteAsync<T>(this RestClient client, DynamicRestRequest<T> request)
        //    //where T : class
        //{
        //    return client.ExecuteAsync<T>(request);
        //}
        static async public Task <T> ExecuteAsync <T>(this RestClient client, DynamicRestRequest <T> request)
        //where T : class
        {
            var response = await client.ExecuteAsync((RestRequest)request);

            return(ToEntity <T>(request, response));
        }
Exemplo n.º 2
0
        static public T BuildImage <T>(this RestClient client, DynamicRestRequest <T> request, string filePath)
            where T : class
        {
            Task <T> task = client.BuildImageAsync <T>(request, filePath);

            task.Wait(client.Timeout);
            return(task.Result);
        }
Exemplo n.º 3
0
        static public void DownloadFile(this RestClient client, DynamicRestRequest <byte[]> request, string filePath)
        {// ContentType=="application/octet-stream"
            var buffer = client.Execute(request);

            using (FileStream outFileStream = new FileStream(filePath, FileMode.Create))
            {
                outFileStream.Write(buffer, 0, buffer.Length);
                outFileStream.Close();
            }
        }
Exemplo n.º 4
0
        //static public T Execute<T>(this RestClient client, DynamicRestRequest<T> request)
        //    //where T : class
        //{
        //    return client.Execute<T>((DynamicRestRequest)request);
        //}
        static public T Execute <T>(this RestClient client, DynamicRestRequest <T> request)
        //where T : class
        {
            var response = client.Execute(request.Node);

            return(ToEntity <T>(request, response));

            /* 某些情況只支援Single Thread不能用非同步去模擬同步,否則會卡死
             * var rltTask = client.ExecuteAsync<T>(request);
             * rltTask.Wait(client.Timeout);
             * return rltTask.Result;
             */
        }
Exemplo n.º 5
0
        static public T ToEntity <T>(DynamicRestRequest <T> request, IRestResponse response)
        {
            Exception err;

            if (!request.IgnoreException && TryCheckException(response, out err))
            {
                throw err;
            }
            if (request.ContentConvertMethod != null)
            {
                return(request.ContentConvertMethod(response));
            }
            return(response.Json2Entity <T>(request.ContentProcessMethod));
        }
Exemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        static async public Task <T> BuildImageAsync <T>(this RestClient client, DynamicRestRequest <T> request, string filePath)
        //where T : class
        {
            using (HttpClient httpClient = new HttpClient()
            {
                BaseAddress = client.BaseUrl
            })
            {
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/tar"));
                HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, BuildUrlQueryString(request.Node));
                req.Content = new StreamContent(new FileStream(filePath, FileMode.Open, FileAccess.Read));
                var responseTask = await httpClient.SendAsync(req);

                var rltMsgTask = await responseTask.Content.ReadAsStringAsync();

                return(JsonConvert.DeserializeObject <T>(request.ContentProcessMethod(rltMsgTask)));
            }
        }
Exemplo n.º 7
0
        /// <param name="defKey">法一:定義RC.resx檔,TakeRequest("key")取得url定義,例如 Util\Support.Open\RC.resx 或 Sgx\Sgx.Data.Mgr\RC.resx</param>
        /// <param name="defKey">法二:傳url path ,例如 TakeRequest("/knowledgebases/18155/generateAnswer")</param>
        public static DynamicRestRequest <T> TakeRequest <T>(string defKey, string id = null, string act = null)
        //where T : class
        {
            DynamicRestRequest request = TakeRequest(defKey, id, act);
            var it = request.Clone <T>();

            if (typeof(string).IsAssignableFrom(typeof(T)))
            {
                it.ContentConvertMethod = rp => (T)(dynamic)rp.Content;
            }
            else if (typeof(byte[]).IsAssignableFrom(typeof(T)))
            {
                it.ContentConvertMethod = rp => (T)(dynamic)rp.RawBytes;
            }
            else if (typeof(bool).Equals(typeof(T)))
            {
                it.ContentConvertMethod = rp => (T)(dynamic)(string.IsNullOrEmpty(rp.Content) ? false : rp.Content.Contains("OK"));
            }
            return(it);
        }
Exemplo n.º 8
0
        public static DynamicRestRequest TakeRequest(string defKey, string id = null, string act = null)
        {
            ApiDef apidef;

            if (defKey.Contains("/"))
            {
                apidef = new ApiDef()
                {
                    Path = defKey
                };
            }
            else if (!ApiSetting.Instance.TryGet(defKey, out apidef))
            {
                throw new NotSupportedException("ListContainers");
            }
            dynamic request = new DynamicRestRequest(apidef.Path, apidef.Method);

            //request.Node.RequestFormat = DataFormat.Json;
            //request.JsonBody = new JObject();
            request.UrlSegment.id     = id;
            request.UrlSegment.action = act;
            return(request);
        }
Exemplo n.º 9
0
 static public IRestResponse Execute(this RestClient client, DynamicRestRequest request)
 {
     return(client.Execute((RestRequest)request));
 }
Exemplo n.º 10
0
 static public Task <IRestResponse> ExecuteAsync(this RestClient client, DynamicRestRequest request)
 {
     return(client.ExecuteTaskAsync((RestRequest)request));
     //使用DoExecuteAsync同ExecuteTaskAsync,只是為練功用
     //return client.DoExecuteAsync<IRestResponse>((RestRequest)request,rp=>rp);
 }