Example #1
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);
        }
Example #2
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);
        }
Example #3
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);
        }