コード例 #1
0
        public virtual ApiResult <T> Get <T>(string url, bool retry = true, bool withAuthorize = true)
        {
            url = ConstructAPIURL(url);

            ApiResult <T> result = new ApiResult <T>();

            try
            {
                using (JiraWebClient wc = new JiraWebClient(AuthToken))
                {
                    string jsonResponse = wc.DownloadString(new Uri(url));

                    if (!string.IsNullOrEmpty(jsonResponse))
                    {
                        if (typeof(T) == typeof(byte[]))
                        {
                            result.Result = (T)Convert.ChangeType(Convert.FromBase64String(jsonResponse), typeof(T));
                        }
                        else
                        {
                            result.Result = JsonConvert.DeserializeObject <T>(jsonResponse, jsonSettings);
                        }
                    }
                }
            }
            catch (WebException e)
            {
                if (!retry)
                {
                    Console.Error.WriteLine(string.Format("API: Exception GET {0} - Exception: {1}", url, e));
                    result.Exception = e;
                }
                else
                {
                    Console.Error.WriteLine(string.Format("API: Exception GET {0}. Trying again... - Exception: {1}", url, e));

                    withAuthorize = ValidateResponse(e);

                    if (withAuthorize)
                    {
                        JiraAuthorize();
                    }

                    return(Get <T>(url, false));
                }
            }

            return(result);
        }
コード例 #2
0
        public virtual ApiResult <T> Delete <T>(string url, object data = null, bool retry = true, bool withAuthorize = true)
        {
            url = ConstructAPIURL(url);

            ApiResult <T> result = new ApiResult <T>();

            try
            {
                using (JiraWebClient wc = new JiraWebClient(AuthToken))
                {
                    string jsonResponse = wc.UploadString(new Uri(url), "DELETE", JsonConvert.SerializeObject(data, jsonSettings));

                    if (!string.IsNullOrEmpty(jsonResponse))
                    {
                        result.Result = JsonConvert.DeserializeObject <T>(jsonResponse, jsonSettings);
                    }
                }
            }
            catch (WebException e)
            {
                if (!retry)
                {
                    Console.Error.WriteLine(string.Format("API: Exception DELETE {0} - Exception: {1}", url, e));
                    result.Exception = e;
                }
                else
                {
                    Console.Error.WriteLine(string.Format("API: Exception DELETE {0}. Trying again... - Exception: {1}", url, e));

                    withAuthorize = ValidateResponse(e);

                    if (withAuthorize)
                    {
                        JiraAuthorize();
                    }

                    return(Delete <T>(url, data, false));
                }
            }

            return(result);
        }