Exemple #1
0
        /// <exception cref="BotRequestException">When non-Ok response returned from server.</exception>
        public async Task <T> MakeRequestAsync <T>(RequestBase <T> request)
        {
            using (var client = new HttpClient(MakeHttpMessageHandler()))
            {
                client.BaseAddress = baseAddress;
                using (var httpMessage = new HttpRequestMessage(HttpMethod.Get, request.MethodName))
                {
                    var postContent = request.CreateHttpContent();
                    if (postContent != null)
                    {
                        httpMessage.Method  = HttpMethod.Post;
                        httpMessage.Content = postContent;
                    }

                    using (var response = await client.SendAsync(httpMessage).ConfigureAwait(false))
                    {
                        if ((int)response.StatusCode >= 500)
                        {
                            // Let's throw exception. It's server fault
                            response.EnsureSuccessStatusCode();
                        }

                        var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                        var result = DeserializeMessage <BotResponse <T> >(responseText);
                        if (!result.Ok || !response.IsSuccessStatusCode)
                        {
                            var exceptionMessage = $"Request failed (status code {(int)response.StatusCode}): {result.Description}";
                            throw new BotRequestException(exceptionMessage)
                                  {
                                      StatusCode   = response.StatusCode,
                                      ResponseBody = responseText,
                                      Description  = result.Description,
                                      ErrorCode    = result.ErrorCode,
                                      Parameters   = result.Parameters,
                                  };
                        }

                        var retVal            = result.Result;
                        var forPostProcessing = retVal as IPostProcessingRequired;
                        if (forPostProcessing != null)
                        {
                            forPostProcessing.PostProcess(accessToken);
                        }

                        return(retVal);
                    }
                }
            }
        }
        /// <exception cref="BotRequestException">When non-Ok response returned from server.</exception>
        public async Task <T> MakeRequestAsync <T>(RequestBase <T> request)
        {
            var uri = new Uri("https://api.telegram.org/bot" + accessToken + "/" + request.MethodName);

            using var httpMessage = new HttpRequestMessage(HttpMethod.Get, uri);
            using var postContent = request.CreateHttpContent();
            if (postContent != null)
            {
                httpMessage.Method  = HttpMethod.Post;
                httpMessage.Content = postContent;
            }

            using var response = await httpClient.SendAsync(httpMessage).ConfigureAwait(false);

            if ((int)response.StatusCode >= 500)
            {
                // Let's throw exception. It's server fault
                response.EnsureSuccessStatusCode();
            }

            var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            var result = DeserializeMessage <BotResponse <T> >(responseText);

            if (!result.Ok || !response.IsSuccessStatusCode)
            {
                var exceptionMessage = $"Request failed (status code {(int)response.StatusCode}): {result.Description}";
                throw new BotRequestException(exceptionMessage)
                      {
                          StatusCode   = response.StatusCode,
                          ResponseBody = responseText,
                          Description  = result.Description,
                          ErrorCode    = result.ErrorCode,
                          Parameters   = result.Parameters,
                      };
            }

            var retVal = result.Result;

            if (retVal is IPostProcessingRequired forPostProcessing)
            {
                forPostProcessing.PostProcess(accessToken);
            }

            return(retVal);
        }
Exemple #3
0
        public async Task <T> MakeRequestAsync <T>(RequestBase <T> request)
        {
            using (var client = new HttpClient(MakeHttpMessageHandler()))
            {
                client.BaseAddress = baseAddress;
                using (var httpMessage = new HttpRequestMessage(HttpMethod.Get, request.MethodName))
                {
                    var postContent = request.CreateHttpContent();
                    if (postContent != null)
                    {
                        httpMessage.Method  = HttpMethod.Post;
                        httpMessage.Content = postContent;
                    }

                    using (var response = await client.SendAsync(httpMessage).ConfigureAwait(false))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                            var result = DeserializeMessage <BotResponse <T> >(responseText);
                            if (result.Ok)
                            {
                                var retVal            = result.Result;
                                var forPostProcessing = retVal as IPostProcessingRequired;
                                if (forPostProcessing != null)
                                {
                                    forPostProcessing.PostProcess(accessToken);
                                }
                                return(retVal);
                            }
                        }
                        return(default(T));
                    }
                }
            }
        }