Esempio n. 1
0
        protected T Execute <T>(RestApiMethod method, string api_name, NameValueCollection data = null)
        {
            string response = "";

            if (data == null)
            {
                data = new NameValueCollection();
            }

            if (method == RestApiMethod.GET)
            {
                string uri = baseurl + api_name + "?" + ToQueryString(data);

                Logger.Debug($"GET {uri}");

                WebRequest request = WebRequest.Create(uri);
                request.Method = "GET";
                request.Headers.Add("Authorization", "Bot " + token);

                WebResponse  resp   = request.GetResponse();
                Stream       st     = resp.GetResponseStream();
                StreamReader reader = new StreamReader(st);
                response = reader.ReadToEnd();
            }
            else
            {
                string uri      = baseurl + api_name;
                string postData = ToJsonString(data);

                Logger.Debug($"POST {uri}");

                WebRequest request = WebRequest.Create(uri);
                request.Method = method.ToString();
                request.Headers.Add("Authorization", "Bot " + token);
                request.ContentType = "application/json";
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                request.ContentLength = byteArray.Length;
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Close();

                WebResponse  resp   = request.GetResponse();
                Stream       st     = resp.GetResponseStream();
                StreamReader reader = new StreamReader(st);
                response = reader.ReadToEnd();
            }

            return(JsonConvert.DeserializeObject <T>(response));
        }
Esempio n. 2
0
        /// <summary>
        /// Get async.
        /// </summary>
        /// <returns>The async.</returns>
        /// <param name="address">Address.</param>
        /// <param name="apiMethod">API method.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public async Task <TaskResult <T> > GetAsync <T>(string address, RestApiMethod apiMethod)
        {
            Guard.Argument(address, nameof(address)).NotNull().NotEmpty();

            JObject jObject = null;
            var     cts     = new CancellationTokenSource();

            try
            {
                var baseAddress = GetBaseAddress();
                var path        = string.Format(apiRestSection.GetSection(Constant.Routing).GetValue <string>(apiMethod.ToString()), address);

                cts.CancelAfter(60000);
                jObject = await GetAsync <T>(baseAddress, path, cts.Token);

                if (jObject == null)
                {
                    return(TaskResult <T> .CreateFailure(JObject.FromObject(new
                    {
                        success = false,
                        message = "Please check the logs for any details."
                    })));
                }
            }
            catch (OperationCanceledException ex)
            {
                logger.LogWarning(ex.Message);
            }
            catch (Exception ex)
            {
                logger.LogWarning(ex.Message);
                return(TaskResult <T> .CreateFailure(ex));
            }

            return(TaskResult <T> .CreateSuccess(jObject.ToObject <T>()));
        }
Esempio n. 3
0
        /// <summary>
        /// Get range async.
        /// </summary>
        /// <returns>The range async.</returns>
        /// <param name="address">Address.</param>
        /// <param name="skip">Skip.</param>
        /// <param name="take">Take.</param>
        /// <param name="apiMethod">API method.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public async Task <IEnumerable <T> > GetRangeAsync <T>(string address, int skip, int take, RestApiMethod apiMethod)
        {
            Guard.Argument(address, nameof(address)).NotNull().NotEmpty();

            IEnumerable <T> messages = null;;
            var             cts      = new CancellationTokenSource();

            try
            {
                var baseAddress = GetBaseAddress();
                var path        = string.Format(apiRestSection.GetSection(Constant.Routing).GetValue <string>(apiMethod.ToString()), address, skip, take);

                cts.CancelAfter(60000);

                var returnMessages = await GetRangeAsync(baseAddress, path, cts.Token);

                messages = returnMessages?.Select(m => m.ToObject <T>());
            }
            catch (OperationCanceledException ex)
            {
                logger.LogWarning(ex.Message);
            }

            return(Task.FromResult(messages).Result);
        }