public void GetResultsTNullUrlTest()
 {
     var cache = new Cache.HttpCache.CacheHelpers();
     var webClientHelper = new WebClientHelpers(cache);
     var results = webClientHelper.GetResults<JObject>(null);
     Assert.IsNull(results);
 }
 public void GetResultsBadUrlJsonTest()
 {
     var cache = new Cache.HttpCache.CacheHelpers();
     var webClientHelper = new WebClientHelpers(cache);
     var results = webClientHelper.GetResults<JObject>("http://localhost/badurl");
     const string badUrlResults = "{\r\n  \"Error\": \"Error retrieving url:http://localhost/badurl - The remote server returned an error: (404) Not Found.\"\r\n}";
     Assert.AreEqual(results.ToString(), badUrlResults);
 }
 public void GetResultsBadUrlTest()
 {
     var cache = new Cache.HttpCache.CacheHelpers();
     var webClientHelper = new WebClientHelpers(cache);
     var results = webClientHelper.GetResults("http://localhost/badurl");
     const string badUrlResults = "Error retrieving url:http://localhost/badurl - The remote server returned an error: (404) Not Found.";
     Assert.AreEqual(results, badUrlResults);
 }
 public void GetResultsStringTest()
 {
     var cache = new Cache.HttpCache.CacheHelpers();
     var webClientHelper = new WebClientHelpers(cache);
     var results = webClientHelper.GetResults(JsonTestUrl);
     Assert.IsNotNull(results);
     Assert.IsTrue(results.Contains("client_ip"));
 }
 public void GetResultsJsonTest()
 {
     var cache = new Cache.HttpCache.CacheHelpers();
     var webClientHelper = new WebClientHelpers(cache);
     var results = webClientHelper.GetResults<JObject>(JsonTestUrl);
     
     Assert.IsNotNull(results);
     Assert.IsNotNull(results["client_ip"]);
 }
 public void GetResultsJsonWithHeadersTest()
 {
     var headers = new NameValueCollection {{"test", "test"}};
     var cache = new Cache.HttpCache.CacheHelpers();
     var webClientHelper = new WebClientHelpers(cache);
     var results = webClientHelper.GetResults<JObject>(JsonTestUrl, headers);
     
     Assert.IsNotNull(results);
     Assert.IsNotNull(results["client_ip"]);
 }
Beispiel #7
0
        /// <summary>
        /// Processes the json.
        /// </summary>
        /// <param name="method">HTTP method, GET, POST or PUT</param>
        /// <param name="endpointUrl">Endpoint URL</param>
        /// <param name="data">Json formatted string</param>
        /// <returns></returns>
        internal async Task <string> ProcessJson(string method, string endpointUrl, [Optional] string data)
        {
            if (WriteDebug)
            {
                Debug.WriteLine($"[RadarrSharp] [ProcessJson] [DEBUG] '{method}': Endpoint URL: '{endpointUrl}', data: '{data}'");
            }

            string response = null;

            using (_webClient = new WebClient
            {
                Headers = WebClientHelpers.GetWebHeaderCollection(ApiKey),
                Proxy = null,
                Encoding = Encoding.UTF8
            })
            {
                try
                {
                    if (method == "POST" || method == "PUT")
                    {
                        response = await _webClient.UploadStringTaskAsync($"{ApiUrl}{endpointUrl}", method, data);
                    }

                    if (method == "GET")
                    {
                        response = await _webClient.DownloadStringTaskAsync($"{ApiUrl}{endpointUrl}");
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"[RadarrSharp] [ProcessJson] [ERROR] '{method}': Endpoint URL: '{endpointUrl}', data: '{data}', {ex}");
                }
                finally
                {
                    if (WriteDebug)
                    {
                        Debug.WriteLine($"[RadarrSharp] [ProcessJson] [DEBUG] Response: {response}");
                        var webClientHeaders = _webClient.ResponseHeaders;
                        if (webClientHeaders != null)
                        {
                            for (int i = 0; i < webClientHeaders.Count; i++)
                            {
                                Debug.WriteLine($"[RadarrSharp] [ProcessJson] [DEBUG] Response header: {webClientHeaders.GetKey(i)}={webClientHeaders.Get(i)}");
                            }
                        }
                    }
                }
            }

            return(response);
        }
Beispiel #8
0
        /// <summary>
        /// Gets the POST/PUT response as a json formatted string
        /// </summary>
        /// <param name="endpointUrl">Endpoint URL</param>
        /// <param name="data">Json formatted string</param>
        /// <param name="method">HTTP method, POST/PUT</param>
        /// <returns>string</returns>
        internal async Task <string> PostJson(string endpointUrl, string data, string method)
        {
            if (WriteDebug)
            {
                Debug.WriteLine($"[SonarrSharp] [DEBUG] [SonarrClient.PostJson] {method}: Endpoint URL: '{endpointUrl}', data: '{data}'");
            }

            var response = string.Empty;

            using (_webClient = new WebClient {
                Headers = WebClientHelpers.GetWebHeaderCollection(ApiKey), Proxy = null
            })
            {
                try
                {
                    response = await _webClient.UploadStringTaskAsync($"{ApiUrl}{endpointUrl}", method, data);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"[SonarrSharp] [ERROR] [SonarrClient.PostJson] {method}: Endpoint URL: '{endpointUrl}', data: '{data}', {ex}");
                }
                finally
                {
                    if (WriteDebug)
                    {
                        Debug.WriteLine($"[SonarrSharp] [DEBUG] [SonarrClient.PostJson] {method}: Endpoint URL: '{endpointUrl}', data: '{data}', response: {response}");
                        var webClientHeaders = _webClient.ResponseHeaders;
                        if (webClientHeaders != null)
                        {
                            for (int i = 0; i < webClientHeaders.Count; i++)
                            {
                                Debug.WriteLine($"[SonarrSharp] [DEBUG] [SonarrClient.GetJson] Response header: {webClientHeaders.GetKey(i)}={webClientHeaders.Get(i)}");
                            }
                        }
                    }
                }
            }

            if (!string.IsNullOrEmpty(response)) // Convert response to UTF8
            {
                response = Encoding.UTF8.GetString(Encoding.Default.GetBytes(response));
            }

            return(response);
        }
Beispiel #9
0
        /// <summary>
        /// Gets the log file.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <returns></returns>
        public async Task <string> GetLogFile(string filename)
        {
            string logFile = null;

            using (var wc = new WebClient {
                Headers = WebClientHelpers.GetWebHeaderCollection(_sonarrClient.ApiKey), Proxy = null
            })
            {
                try
                {
                    logFile = await wc.DownloadStringTaskAsync(_sonarrClient.ApiUrl + $"/log/file/filename={filename}");
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"[SonarrSharp] [ERROR] [Log.GetLogFile] Filename: '{filename}', {ex}");
                }
            }

            return(logFile);
        }