Ejemplo n.º 1
0
        /// <summary>
        /// Retrieve the own workitems
        /// </summary>
        /// <returns>WorkItemList</returns>
        public async Task <WorkItemList> GetOwnWorkitems()
        {
            _tfsHttpBehaviour.MakeCurrent();
            Uri apiUri = _tfsConfiguration.TfsUri.AppendSegments("_apis").ExtendQuery("api-version", "3.0");
            var client = HttpClientFactory.Create(_tfsConfiguration.TfsUri).SetBasicAuthorization("", _tfsConfiguration.ApiKey);

            var workitemsQueryUri = apiUri.AppendSegments("wit", "wiql");

            var wiql = new JObject {
                { "query", "Select [System.Id] FROM WorkItems WHERE [System.AssignedTo] = @me" }
            };

            var queryResult = await client.PostAsync <HttpResponse <WorkItemQueryResult, string> >(workitemsQueryUri, wiql).ConfigureAwait(false);

            if (queryResult.HasError)
            {
                throw new Exception(queryResult.ErrorResponse);
            }

            var workItemsUri = apiUri.AppendSegments("wit", "workItems").ExtendQuery("ids", string.Join(",", queryResult.Response.Items.Select(item => item.Id)));
            var result       = await client.GetAsAsync <HttpResponse <WorkItemList, string> >(workItemsUri).ConfigureAwait(false);

            if (result.HasError)
            {
                throw new Exception(result.ErrorResponse);
            }

            return(result.Response);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Delete an imgur image, this is done by specifying the delete hash
        /// </summary>
        /// <param name="imgurImage"></param>
        /// <param name="token"></param>
        public static async Task <string> DeleteImgurImageAsync(ImgurImage imgurImage, CancellationToken token = default)
        {
            Log.Info().WriteLine("Deleting Imgur image for {0}", imgurImage.Data.Deletehash);
            var    deleteUri = new Uri(string.Format(_imgurConfiguration.ApiUrl + "/image/{0}", imgurImage.Data.Deletehash));
            string responseString;

            Behaviour.MakeCurrent();
            using (var client = HttpClientFactory.Create(deleteUri))
            {
                var response = await client.DeleteAsync(deleteUri, token).ConfigureAwait(false);

                if ((response.StatusCode != HttpStatusCode.NotFound) && (response.StatusCode != HttpStatusCode.BadRequest))
                {
                    await response.HandleErrorAsync().ConfigureAwait(false);
                }
                responseString = await response.GetAsAsync <string>(token).ConfigureAwait(false);

                Log.Info().WriteLine("Delete result: {0}", responseString);
            }
            // Make sure we remove it from the history, if no error occured
            _imgurConfiguration.RuntimeImgurHistory.Remove(imgurImage.Data.Id);
            _imgurConfiguration.ImgurUploadHistory.Remove(imgurImage.Data.Id);

            // dispose is called inside the imgurInfo object
            imgurImage.Image = null;
            return(responseString);
        }
        public async Task TestHttpExtensionsDefaultReadWrite()
        {
            var iniFileConfig = IniFileConfigBuilder.Create()
                                .WithApplicationName("Dapplo")
                                .WithFilename("dapplo.httpextensions")
                                .WithoutSaveOnExit()
                                .BuildApplicationConfig();

            // Important to disable the auto-save, otherwise we get test issues
            var iniConfig = new IniConfig(iniFileConfig);

            using (var testMemoryStream = new MemoryStream())
            {
                await IniConfig.Current.ReadFromStreamAsync(testMemoryStream).ConfigureAwait(false);
            }
            var httpConfiguration = await iniConfig.RegisterAndGetAsync <IHttpConfiguration>();

            Assert.NotNull(httpConfiguration);
            using (var writeStream = new MemoryStream())
            {
                await iniConfig.WriteToStreamAsync(writeStream).ConfigureAwait(false);

                writeStream.Seek(0, SeekOrigin.Begin);
                await iniConfig.ReadFromStreamAsync(writeStream).ConfigureAwait(false);

                var behaviour = new HttpBehaviour
                {
                    HttpSettings = httpConfiguration
                };
                behaviour.MakeCurrent();
                HttpClientFactory.Create();
            }
        }
Ejemplo n.º 4
0
        public async Task TestRedirectAndNotFollow()
        {
            var behavior = new HttpBehaviour
            {
                HttpSettings = new HttpSettings()
            };

            behavior.HttpSettings.AllowAutoRedirect = false;
            behavior.MakeCurrent();
            await Assert.ThrowsAsync <HttpRequestException>(async() => await new Uri("https://httpbin.org/redirect/2").GetAsAsync <string>());
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     Get details for an incident
        /// </summary>
        /// <param name="incidentId"></param>
        /// <param name="token"></param>
        /// <returns>dynamic</returns>
        public async Task <string> GetIncidentAsync(string incidentId, CancellationToken token = default)
        {
            // Only use if available
            _behaviour?.MakeCurrent();
            var incidentUri = BaseUri.AppendSegments("table", "incident").ExtendQuery("sysparm_limit", 1).ExtendQuery("sysparm_query", "number=" + incidentId);

            // Specify the fields to retrieve
            if (ServiceNowConfig.IncidentFields?.Length > 0)
            {
                incidentUri = incidentUri.ExtendQuery("sysparm_fields", string.Join(",", ServiceNowConfig.IncidentFields));
            }

            var response = await incidentUri.GetAsAsync <HttpResponse <string, Error> >(token);

            if (response.HasError)
            {
                throw new Exception(response.ErrorResponse.ErrorDetails.Message);
            }
            return(response.Response);
        }
Ejemplo n.º 6
0
        /// <summary>
        ///     Attach content to the specified issue
        ///     See: https://docs.atlassian.com/confluence/REST/latest/#d2e3035
        /// </summary>
        /// <param name="contentId">Id of the content to attach to</param>
        /// <param name="content">the content can be anything what Dapplo.HttpExtensions supports</param>
        /// <param name="cancellationToken">CancellationToken</param>
        /// <returns>Result with Attachment items</returns>
        public async Task <Result <Attachment> > AttachAsync(string contentId, object content, CancellationToken cancellationToken = default(CancellationToken))
        {
            _behaviour.MakeCurrent();
            var attachUri = ConfluenceBaseUri.AppendSegments("content", contentId, "child", "attachments");
            var response  = await attachUri.PostAsync <HttpResponse <Result <Attachment>, Error> >(content, cancellationToken).ConfigureAwait(false);

            if (response.HasError)
            {
                throw new Exception(response.ErrorResponse.Message);
            }
            return(response.Response);
        }
		public async Task TestRedirectAndNotFollow()
		{
			var behavior = new HttpBehaviour
			{
				HttpSettings = new HttpSettings()
			};
			behavior.HttpSettings.AllowAutoRedirect = false;
			behavior.MakeCurrent();
			await Assert.ThrowsAsync<HttpRequestException>(async () => await new Uri("https://httpbin.org/redirect/2").GetAsAsync<string>());
		}
Ejemplo n.º 8
0
        /// <summary>
        ///     Retrieve the version
        /// </summary>
        /// <param name="cancellationToken">CancellationToken</param>
        /// <returns>version as string</returns>
        public async Task <string> GetVersionAsync(CancellationToken cancellationToken = default)
        {
            var versionUri = SabNzbApiUri.ExtendQuery("mode", "version");

            _behaviour.MakeCurrent();
            var container = await versionUri.GetAsAsync <VersionContainer>(cancellationToken);

            return(container?.Version);
        }