示例#1
0
        private Task <Stream> GetStreamAsync(string restServicePath, JIRAApiType apiType, CancellationToken cancellationToken)
        {
            var path = GetApiPath(apiType) + restServicePath;

            cancellationToken.ThrowIfCancellationRequested();

            return(_httpClient.GetAsync(path, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
                   .ContinueWith(
                       task => GetStreamFromHttpResponseAsync(task, path, apiType, cancellationToken),
                       cancellationToken,
                       TaskContinuationOptions.AttachedToParent,
                       TaskScheduler.Current)
                   .Unwrap());
        }
示例#2
0
        private Uri GetApiPath(JIRAApiType apiType)
        {
            switch (apiType)
            {
            case JIRAApiType.Repository:
                return(new Uri("/rest/dev-status/1.0/", UriKind.Relative));

            case JIRAApiType.Session:
                return(new Uri("/rest/auth/1/", UriKind.Relative));

            case JIRAApiType.Standard:
            default:
                return(new Uri("/rest/api/2/", UriKind.Relative));
            }
        }
示例#3
0
        private Task <Stream> PostStreamAsync(string restServicePath, JIRAApiType apiType, HttpContent content, CancellationToken cancellationToken)
        {
            string path = restServicePath;

            if (!restServicePath.StartsWith(GetApiPath(apiType).ToString()))
            {
                path = GetApiPath(apiType) + restServicePath;
            }
            cancellationToken.ThrowIfCancellationRequested();

            return(_httpClient.PostAsync(path, content, cancellationToken)
                   .ContinueWith(
                       task => GetStreamFromHttpResponseAsync(task, path, apiType, cancellationToken),
                       cancellationToken,
                       TaskContinuationOptions.AttachedToParent,
                       TaskScheduler.Current)
                   .Unwrap());
        }
示例#4
0
        private Task <JObject> GetJsonResponseAsync(string actionPath, JIRAApiType apiType, CancellationToken cancellationToken)
        {
            var getStreamTask = GetStreamAsync(actionPath, apiType, cancellationToken);

            return(getStreamTask.ContinueWith(
                       task =>
            {
                using (var responseStream = task.Result)
                {
                    var jsonStreamReader = new StreamReader(responseStream);
                    var jsonTextReader = (TextReader)jsonStreamReader;
                    var jDoc = new JsonTextReader(jsonTextReader);

                    var jObj = JObject.Load(jDoc);

                    return jObj;
                }
            },
                       cancellationToken,
                       TaskContinuationOptions.AttachedToParent,
                       TaskScheduler.Current));
        }
示例#5
0
        private Task <Stream> GetStreamFromHttpResponseAsync(Task <HttpResponseMessage> task, string restServicePath, JIRAApiType apiType, CancellationToken cancellationToken)
        {
#if !__MonoCS__
            bool retry        = task.IsCanceled && !cancellationToken.IsCancellationRequested;
            bool unauthorized = task.Status == TaskStatus.RanToCompletion &&
                                (task.Result.StatusCode == HttpStatusCode.Unauthorized || task.Result.StatusCode == HttpStatusCode.BadRequest);

            if (!retry)
            {
                if (task.Result.IsSuccessStatusCode)
                {
                    var httpContent = task.Result.Content;

                    if (httpContent.Headers.ContentType.MediaType == "text/html")
                    {
                        // JIRA responds with an HTML login page when guest access is denied.
                        unauthorized = true;
                    }
                    else
                    {
                        return(httpContent.ReadAsStreamAsync());
                    }
                }
            }

            if (retry)
            {
                return(GetStreamAsync(restServicePath, apiType, cancellationToken));
            }

            if (unauthorized)
            {
                var issueTrackerCredentials = _issueTrackerWatcher.GetIssueTrackerCredentials(this, true);

                if (issueTrackerCredentials != null)
                {
                    UpdateHttpClientOptions(issueTrackerCredentials, cancellationToken);
                    return(GetStreamAsync(restServicePath, apiType, cancellationToken));
                }

                throw new OperationCanceledException(task.Result.ReasonPhrase);
            }

            throw new HttpRequestException(task.Result.ReasonPhrase);
#else
            return(null);
#endif
        }