예제 #1
0
 public static T Send <T>(IApiRequest request)
 {
     using (var client = new GZipWebClient())
     {
         client.BaseAddress = RootUrl;
         var url          = request.GetUrl();
         var jsonResponse = client.DownloadString(url);
         var dto          = JsonConvert.DeserializeObject <T>(jsonResponse);
         return(dto);
     }
 }
        // These methods could be exported into a common API object and this API class inheriting it

        private async Task <T> SendAsync <T>(IApiRequest request) where T : class
        {
            var url = $"{Settings.Url}/{request.GetUrl()}?code={Settings.ApiKey}";

            var message = new HttpRequestMessage
            {
                Method     = request.GetMethod(),
                RequestUri = new Uri(url)
            };

            var response = await Client.Value.SendAsync(message);

            var responseContent = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                throw new ApplicationException($"{response.StatusCode} - {responseContent}");
            }

            return(responseContent.Deserialize <T>());
        }