/// <summary> /// This creates an HttpClientHandler, normally one should use CreateWebRequestHandler /// </summary> /// <param name="suppliedHttpSettings">IHttpSettings instance or null if the global settings need to be used</param> /// <returns>HttpMessageHandler (HttpClientHandler)</returns> public static HttpMessageHandler CreateHttpClientHandler(IHttpSettings suppliedHttpSettings = null) { var httpSettings = suppliedHttpSettings ?? HttpSettings.GlobalHttpSettings; var httpClientHandler = new HttpClientHandler(); SetDefaults(httpClientHandler, httpSettings); return httpClientHandler; }
/// <summary> /// Create the SabNzbClient object, here the HttpClient is configured /// </summary> /// <param name="baseUri">Base URL, e.g. https://yoursabnzbserver</param> /// <param name="apiKey">API token</param> /// <param name="httpSettings">IHttpSettings or null for default</param> public SabNzbClient(Uri baseUri, string apiKey, IHttpSettings httpSettings = null) { if (baseUri == null) { throw new ArgumentNullException(nameof(baseUri)); } _apiKey = apiKey ?? throw new ArgumentNullException(nameof(apiKey)); SabNzbApiUri = baseUri.AppendSegments("api").ExtendQuery(new Dictionary <string, string> { { "output", "json" }, { "apikey", apiKey } }); _behaviour = new HttpBehaviour { HttpSettings = httpSettings ?? HttpExtensionsGlobals.HttpSettings, JsonSerializer = new SimpleJsonSerializer(), OnHttpRequestMessageCreated = httpMessage => { if (!string.IsNullOrEmpty(_user) && _password != null) { httpMessage?.SetBasicAuthorization(_user, _password); } return(httpMessage); } }; }
/// <summary> /// Create the JenkinsApi object, here the HttpClient is configured /// </summary> /// <param name="baseUri">Base URL, e.g. https://yourjenkinsserver</param> /// <param name="httpSettings">IHttpSettings or null for default</param> private JenkinsClient(Uri baseUri, IHttpSettings httpSettings = null) { JenkinsBaseUri = baseUri ?? throw new ArgumentNullException(nameof(baseUri)); Settings = httpSettings ?? HttpExtensionsGlobals.HttpSettings.ShallowClone(); Settings.PreAuthenticate = true; Behaviour = new HttpBehaviour { HttpSettings = Settings, JsonSerializer = new SimpleJsonSerializer(), OnHttpRequestMessageCreated = httpMessage => { if (_crumbIssuer != null) { httpMessage?.Headers.TryAddWithoutValidation(_crumbIssuer.CrumbRequestField, _crumbIssuer.Crumb); } if (!string.IsNullOrEmpty(_user) && _apiToken != null) { httpMessage?.SetBasicAuthorization(_user, _apiToken); } return(httpMessage); } }; }
/// <summary> /// Create the NicePerformClient object, here the HttpClient is configured /// </summary> /// <param name="nicePerformUri">Base URL, e.g. https://yourniceperformserver</param> /// <param name="httpSettings">IHttpSettings or null for default</param> private NicePerformClient(Uri nicePerformUri, IHttpSettings httpSettings = null) { NicePerformUri = nicePerformUri ?? throw new ArgumentNullException(nameof(nicePerformUri)); ConfluenceApiUri = nicePerformUri.AppendSegments("rest", "api"); Behaviour = ConfigureBehaviour(new HttpBehaviour(), httpSettings); }
/// <summary> /// Helper method to configure the IChangeableHttpBehaviour /// </summary> /// <param name="behaviour">IChangeableHttpBehaviour</param> /// <param name="httpSettings">IHttpSettings</param> /// <returns>the behaviour, but configured as IHttpBehaviour </returns> public IHttpBehaviour ConfigureBehaviour(IChangeableHttpBehaviour behaviour, IHttpSettings httpSettings = null) { behaviour.HttpSettings = httpSettings ?? HttpExtensionsGlobals.HttpSettings.ShallowClone(); #if NET461 // Disable caching, if no HTTP settings were provided. if (httpSettings == null) { behaviour.HttpSettings.RequestCacheLevel = RequestCacheLevel.NoCacheNoStore; } #endif // Using our own Json Serializer, implemented with Json.NET behaviour.JsonSerializer = new JsonNetJsonSerializer(); behaviour.OnHttpRequestMessageCreated = httpMessage => { httpMessage?.Headers.TryAddWithoutValidation("X-Atlassian-Token", "nocheck"); if (!string.IsNullOrEmpty(_user) && _password != null) { httpMessage?.SetBasicAuthorization(_user, _password); } return(httpMessage); }; return(behaviour); }
/// <summary> /// Create the JiraApi, using OAuth 1 for the communication, here the HttpClient is configured /// </summary> /// <param name="baseUri">Base URL, e.g. https://yourjiraserver</param> /// <param name="jiraOAuthSettings">JiraOAuthSettings</param> /// <param name="httpSettings">IHttpSettings or null for default</param> public JiraApi(Uri baseUri, JiraOAuthSettings jiraOAuthSettings, IHttpSettings httpSettings = null) : this(baseUri) { var jiraOAuthUri = JiraBaseUri.AppendSegments("plugins", "servlet", "oauth"); var oAuthSettings = new OAuth1Settings { TokenUrl = jiraOAuthUri.AppendSegments("request-token"), TokenMethod = HttpMethod.Post, AccessTokenUrl = jiraOAuthUri.AppendSegments("access-token"), AccessTokenMethod = HttpMethod.Post, CheckVerifier = false, SignatureType = OAuth1SignatureTypes.RsaSha1, Token = jiraOAuthSettings.Token, ClientId = jiraOAuthSettings.ConsumerKey, CloudServiceName = jiraOAuthSettings.CloudServiceName, RsaSha1Provider = jiraOAuthSettings.RsaSha1Provider, AuthorizeMode = jiraOAuthSettings.AuthorizeMode, AuthorizationUri = jiraOAuthUri.AppendSegments("authorize") .ExtendQuery(new Dictionary <string, string> { { OAuth1Parameters.Token.EnumValueOf(), "{RequestToken}" }, { OAuth1Parameters.Callback.EnumValueOf(), "{RedirectUrl}" } }) }; // Configure the OAuth1Settings _behaviour = ConfigureBehaviour(OAuth1HttpBehaviourFactory.Create(oAuthSettings), httpSettings); }
/// <summary> /// Create the ConfluenceApi object, here the HttpClient is configured /// </summary> /// <param name="confluenceUri">Base URL, e.g. https://yourConfluenceserver</param> /// <param name="httpSettings">IHttpSettings or null for default</param> private ConfluenceClient(Uri confluenceUri, IHttpSettings httpSettings = null) { ConfluenceUri = confluenceUri ?? throw new ArgumentNullException(nameof(confluenceUri)); ConfluenceApiUri = confluenceUri.AppendSegments("rest", "api"); Behaviour = ConfigureBehaviour(new HttpBehaviour(), httpSettings); }
/// <summary> /// Create the JiraApi object, here the HttpClient is configured /// </summary> /// <param name="baseUri">Base URL, e.g. https://yourjiraserver</param> /// <param name="httpSettings">IHttpSettings or null for default</param> private JiraClient(Uri baseUri, IHttpSettings httpSettings = null) { Behaviour = ConfigureBehaviour(new HttpBehaviour(), httpSettings); JiraBaseUri = baseUri; JiraRestUri = baseUri.AppendSegments("rest", "api", "2"); JiraAuthUri = baseUri.AppendSegments("rest", "auth", "1"); JiraAgileRestUri = baseUri.AppendSegments("rest", "agile", "1.0"); JiraGreenhopperRestUri = baseUri.AppendSegments("rest", "greenhopper", "1.0"); }
/// <summary> /// Create a IWebProxy Object which can be used to access the Internet /// This method will create a proxy according to the properties in the Settings class /// </summary> /// <param name="supliedHttpSettings">IHttpSettings instance or null if the global settings need to be used</param> /// <returns>IWebProxy filled with all the proxy details or null if none is set/wanted</returns> public static IWebProxy CreateProxy(IHttpSettings supliedHttpSettings = null) { var httpSettings = supliedHttpSettings ?? HttpSettings.GlobalHttpSettings; // This is already checked in the HttpClientFactory, but should be checked if this call is used elsewhere. if (!httpSettings.UseProxy) { return null; } IWebProxy proxyToUse; if (httpSettings.UseDefaultProxy) { proxyToUse = WebRequest.GetSystemWebProxy(); } else { if (httpSettings.ProxyBypassList != null) { proxyToUse = new WebProxy(httpSettings.ProxyUri, httpSettings.ProxyBypassOnLocal, httpSettings.ProxyBypassList); } else { proxyToUse = new WebProxy(httpSettings.ProxyUri, httpSettings.ProxyBypassOnLocal); } } if (httpSettings.UseDefaultCredentialsForProy) { if (proxyToUse is WebProxy) { // Read note here: https://msdn.microsoft.com/en-us/library/system.net.webproxy.credentials.aspx var webProxy = proxyToUse as WebProxy; webProxy.UseDefaultCredentials = true; } else { proxyToUse.Credentials = CredentialCache.DefaultCredentials; } } else { if (proxyToUse is WebProxy) { // Read note here: https://msdn.microsoft.com/en-us/library/system.net.webproxy.credentials.aspx var webProxy = proxyToUse as WebProxy; webProxy.UseDefaultCredentials = false; webProxy.Credentials = httpSettings.ProxyCredentials; } else { proxyToUse.Credentials = httpSettings.ProxyCredentials; } } return proxyToUse; }
/// <summary> /// Create the ConfluenceApi object, here the HttpClient is configured /// </summary> /// <param name="bitbucketUri">Base URL, e.g. https://yourConfluenceserver</param> /// <param name="httpSettings">IHttpSettings or null for default</param> private BitbucketClient(Uri bitbucketUri, IHttpSettings httpSettings = null) { if (bitbucketUri == null) { throw new ArgumentNullException(nameof(bitbucketUri)); } BitbucketUri = bitbucketUri; BitbucketApiUri = bitbucketUri.AppendSegments("rest", "api", "1.0"); Behaviour = ConfigureBehaviour(new HttpBehaviour(), httpSettings); }
/// <summary> /// Apply settings on the WebRequestHandler, this also calls the SetDefaults for the underlying HttpClientHandler /// </summary> /// <param name="webRequestHandler"></param> /// <param name="suppliedHttpSettings">IHttpSettings instance or null if the global settings need to be used</param> public static void SetDefaults(WebRequestHandler webRequestHandler, IHttpSettings suppliedHttpSettings = null) { var httpSettings = suppliedHttpSettings ?? HttpSettings.GlobalHttpSettings; SetDefaults(webRequestHandler as HttpClientHandler, httpSettings); webRequestHandler.AllowPipelining = httpSettings.AllowPipelining; webRequestHandler.ReadWriteTimeout = httpSettings.ReadWriteTimeout; webRequestHandler.AuthenticationLevel = httpSettings.AuthenticationLevel; webRequestHandler.ContinueTimeout = httpSettings.ContinueTimeout; webRequestHandler.ImpersonationLevel = httpSettings.ImpersonationLevel; webRequestHandler.MaxResponseHeadersLength = httpSettings.MaxResponseHeadersLength; }
/// <summary> /// Retrieve only the content headers, by using the HTTP HEAD method /// </summary> /// <param name="uri">Uri to get HEAD for</param> /// <param name="throwErrorOnNonSuccess">true to throw an exception when an error is returned, else the headers are returned</param> /// <param name="token">CancellationToken</param> /// <param name="httpSettings">IHttpSettings instance or null if the global settings need to be used</param> /// <returns>HttpContentHeaders</returns> public static async Task<HttpContentHeaders> HeadAsync(this Uri uri, bool throwErrorOnNonSuccess = true, CancellationToken token = default(CancellationToken), IHttpSettings httpSettings = null) { if (uri == null) { throw new ArgumentNullException(nameof(uri)); } using (var client = HttpClientFactory.CreateHttpClient(httpSettings, uri)) using (var request = new HttpRequestMessage(HttpMethod.Head, uri)) { var responseMessage = await client.SendAsync(request, token).ConfigureAwait(false); await responseMessage.HandleErrorAsync(throwErrorOnNonSuccess, token); return responseMessage.Content.Headers; } }
/// <summary> /// Create a HttpClient with default, in the HttpClientExtensions configured, settings /// </summary> /// <param name="suppliedHttpSettings">IHttpSettings instance or null if the global settings need to be used</param> /// <param name="uriForConfiguration">If a Uri is supplied, this is used to configure the HttpClient. Currently the Uri.UserInfo is used to set the basic authorization.</param> /// <returns>HttpClient</returns> public static HttpClient CreateHttpClient(IHttpSettings suppliedHttpSettings = null, Uri uriForConfiguration = null) { var httpSettings = suppliedHttpSettings ?? HttpSettings.GlobalHttpSettings; var client = new HttpClient(HttpMessageHandlerFactory.CreateWebRequestHandler(httpSettings)) { Timeout = httpSettings.RequestTimeout, MaxResponseContentBufferSize = httpSettings.MaxResponseContentBufferSize }; if (!string.IsNullOrEmpty(httpSettings.DefaultUserAgent)) { client.DefaultRequestHeaders.UserAgent.TryParseAdd(httpSettings.DefaultUserAgent); } client.SetBasicAuthorization(uriForConfiguration); return client; }
/// <summary> /// Apply settings on the HttpClientHandler /// </summary> /// <param name="httpClientHandler"></param> /// <param name="suppliedHttpSettings">IHttpSettings instance or null if the global settings need to be used</param> public static void SetDefaults(HttpClientHandler httpClientHandler, IHttpSettings suppliedHttpSettings = null) { var httpSettings = suppliedHttpSettings ?? HttpSettings.GlobalHttpSettings; httpClientHandler.AllowAutoRedirect = httpSettings.AllowAutoRedirect; httpClientHandler.AutomaticDecompression = httpSettings.DefaultDecompressionMethods; httpClientHandler.CookieContainer = httpSettings.UseCookies ? new CookieContainer() : null; httpClientHandler.Credentials = httpSettings.UseDefaultCredentials ? CredentialCache.DefaultCredentials : httpSettings.Credentials; httpClientHandler.MaxAutomaticRedirections = httpSettings.MaxAutomaticRedirections; httpClientHandler.MaxRequestContentBufferSize = httpSettings.MaxRequestContentBufferSize; httpClientHandler.UseCookies = httpSettings.UseCookies; httpClientHandler.UseDefaultCredentials = httpSettings.UseDefaultCredentials; httpClientHandler.Proxy = httpSettings.UseProxy ? ProxyFactory.CreateProxy(httpSettings) : null; httpClientHandler.UseProxy = httpSettings.UseProxy; httpClientHandler.PreAuthenticate = httpSettings.PreAuthenticate; }
/// <summary> /// Get LastModified for a URI /// </summary> /// <param name="uri">Uri</param> /// <param name="throwErrorOnNonSuccess">true to throw an exception when an error is returned, else DateTimeOffset.MinValue is returned</param> /// <param name="token">CancellationToken</param> /// <param name="httpSettings">IHttpSettings instance or null if the global settings need to be used</param> /// <returns>DateTime</returns> public static async Task<DateTimeOffset> LastModifiedAsync(this Uri uri, bool throwErrorOnNonSuccess = true, CancellationToken token = default(CancellationToken), IHttpSettings httpSettings = null) { if (uri == null) { throw new ArgumentNullException(nameof(uri)); } try { var headers = await uri.HeadAsync(throwErrorOnNonSuccess, token, httpSettings).ConfigureAwait(false); if (headers.LastModified.HasValue) { return headers.LastModified.Value; } } catch { // Ignore } // Pretend it is old return DateTimeOffset.MinValue; }
/// <summary> /// Create the ConfluenceApi object, here the HttpClient is configured /// </summary> /// <param name="baseUri">Base URL, e.g. https://yourConfluenceserver</param> /// <param name="httpSettings">IHttpSettings or null for default</param> public ConfluenceApi(Uri baseUri, IHttpSettings httpSettings = null) { if (baseUri == null) { throw new ArgumentNullException(nameof(baseUri)); } ConfluenceBaseUri = baseUri.AppendSegments("rest", "api"); _behaviour = new HttpBehaviour { HttpSettings = httpSettings, OnHttpRequestMessageCreated = httpMessage => { httpMessage?.Headers.TryAddWithoutValidation("X-Atlassian-Token", "no-check"); if (!string.IsNullOrEmpty(_user) && _password != null) { httpMessage?.SetBasicAuthorization(_user, _password); } return(httpMessage); } }; }
/// <summary> /// Create the ServiceNowApi object, here the HttpClient is configured /// </summary> /// <param name="baseUri">Base URL, e.g. https://servicenow</param> /// <param name="httpSettings">IHttpSettings or null for default</param> public ServiceNowApi(Uri baseUri, IHttpSettings httpSettings = null) { if (baseUri == null) { throw new ArgumentNullException(nameof(baseUri)); } BaseUri = baseUri.AppendSegments("api", "now"); _behaviour = new HttpBehaviour { HttpSettings = httpSettings ?? HttpExtensionsGlobals.HttpSettings, OnHttpRequestMessageCreated = httpMessage => { if (!string.IsNullOrEmpty(_user) && _password != null) { httpMessage?.SetBasicAuthorization(_user, _password); } if (!string.IsNullOrEmpty(_userToken)) { httpMessage?.AddRequestHeader("X-UserToken", _userToken); } return(httpMessage); } }; }
/// <summary> /// Factory method to create a BitbucketClient /// </summary> /// <param name="bitbucketUri">Uri to your bitbucket server</param> /// <param name="httpSettings">IHttpSettings used if you need specific settings</param> /// <returns>IBitbucketClient</returns> public static IBitbucketClient Create(Uri bitbucketUri, IHttpSettings httpSettings = null) { return(new BitbucketClient(bitbucketUri, httpSettings)); }
/// <summary> /// Factory method to create a NicePerformClient /// </summary> /// <param name="nicePerformUri">Uri to your Nice Perform server</param> /// <param name="httpSettings">IHttpSettings used if you need specific settings</param> /// <returns>IConfluenceClient</returns> public static INicePerformClient Create(Uri nicePerformUri, IHttpSettings httpSettings = null) { return(new NicePerformClient(nicePerformUri, httpSettings)); }
/// <summary> /// Factory method to create the jira client /// </summary> public static IJiraClient Create(Uri baseUri, IHttpSettings httpSettings = null) => new JiraClient(baseUri, httpSettings);
/// <summary> /// Download a uri response as string /// </summary> /// <param name="uri">An Uri to specify the download location</param> /// <param name="throwErrorOnNonSuccess">true to throw an exception when an error occurse, else null is returned</param> /// <param name="token">CancellationToken</param> /// <param name="httpSettings">IHttpSettings instance or null if the global settings need to be used</param> /// <returns>string with the content</returns> public static async Task<string> GetAsStringAsync(this Uri uri, bool throwErrorOnNonSuccess = true, CancellationToken token = default(CancellationToken), IHttpSettings httpSettings = null) { if (uri == null) { throw new ArgumentNullException(nameof(uri)); } using (var client = HttpClientFactory.CreateHttpClient(httpSettings, uri)) using (var response = await client.GetAsync(uri, HttpCompletionOption.ResponseContentRead, token).ConfigureAwait(false)) { return await response.GetAsStringAsync(throwErrorOnNonSuccess, token).ConfigureAwait(false); } }
/// <summary> /// Factory method to create a ConfluenceClient /// </summary> /// <param name="confluenceUri">Uri to your confluence server</param> /// <param name="httpSettings">IHttpSettings used if you need specific settings</param> /// <returns>IConfluenceClient</returns> public static IConfluenceClient Create(Uri confluenceUri, IHttpSettings httpSettings = null) { return(new ConfluenceClient(confluenceUri, httpSettings)); }
/// <summary>Factory method to create the jira client</summary> //public static IWorklogService Create(Uri baseUri, IHttpSettings httpSettings = null) //{ // return (IWorklogService)new WorklogService(baseUri, httpSettings); //} /// <summary> /// Helper method to configure the IChangeableHttpBehaviour /// </summary> /// <param name="behaviour">IChangeableHttpBehaviour</param> /// <param name="httpSettings">IHttpSettings</param> /// <returns>the behaviour, but configured as IHttpBehaviour </returns> private IHttpBehaviour ConfigureBehaviour(IChangeableHttpBehaviour behaviour, IHttpSettings httpSettings = null) { behaviour.HttpSettings = httpSettings ?? HttpExtensionsGlobals.HttpSettings; behaviour.JsonSerializer = (IJsonSerializer) new JsonNetJsonSerializer(); behaviour.OnHttpRequestMessageCreated = (Func <HttpRequestMessage, HttpRequestMessage>)(httpMessage => { if (httpMessage != null) { httpMessage.Headers.TryAddWithoutValidation("X-Atlassian-Token", "nocheck"); } if (!string.IsNullOrEmpty(this._user) && this._password != null && httpMessage != null) { httpMessage.SetBasicAuthorization(this._user, this._password); } return(httpMessage); }); return((IHttpBehaviour)behaviour); }
/// <summary> /// Create the JiraApi object, here the HttpClient is configured /// </summary> /// <param name="baseUri">Base URL, e.g. https://yourjiraserver</param> /// <param name="httpSettings">IHttpSettings or null for default</param> public WorklogService(Uri baseUri, IHttpSettings httpSettings = null) : this(baseUri) { this.Behaviour = this.ConfigureBehaviour((IChangeableHttpBehaviour) new HttpBehaviour(), httpSettings); }
/// <summary> /// Factory method to create the jira client /// </summary> public static IJenkinsClient Create(Uri baseUri, IHttpSettings httpSettings = null) { return(new JenkinsClient(baseUri, httpSettings)); }
private ConfluenceOAuthClient(Uri baseUri, ConfluenceOAuthSettings confluenceOAuthSettings, IHttpSettings httpSettings = null) : base(baseUri, httpSettings) { var confluenceOAuthUri = ConfluenceUri.AppendSegments("plugins", "servlet", "oauth"); var oAuthSettings = new OAuth1Settings { TokenUrl = confluenceOAuthUri.AppendSegments("request-token"), TokenMethod = HttpMethod.Post, AccessTokenUrl = confluenceOAuthUri.AppendSegments("access-token"), AccessTokenMethod = HttpMethod.Post, CheckVerifier = false, SignatureType = OAuth1SignatureTypes.RsaSha1, // According to <a href="https://community.atlassian.com/t5/Questions/Confluence-Oauth-Authentication/qaq-p/331326#M51385">here</a> // the OAuth arguments need to be passed in the query SignatureTransport = OAuth1SignatureTransports.QueryParameters, Token = confluenceOAuthSettings.Token, ClientId = confluenceOAuthSettings.ConsumerKey, CloudServiceName = confluenceOAuthSettings.CloudServiceName, RsaSha1Provider = confluenceOAuthSettings.RsaSha1Provider, AuthorizeMode = confluenceOAuthSettings.AuthorizeMode, AuthorizationUri = confluenceOAuthUri.AppendSegments("authorize") .ExtendQuery(new Dictionary <string, string> { { OAuth1Parameters.Token.EnumValueOf(), "{RequestToken}" }, { OAuth1Parameters.Callback.EnumValueOf(), "{RedirectUrl}" } }) }; // Configure the OAuth1Settings Behaviour = ConfigureBehaviour(OAuth1HttpBehaviourFactory.Create(oAuthSettings), httpSettings); }
/// <summary> /// Get the content as a MemoryStream /// </summary> /// <param name="uri">Uri</param> /// <param name="throwErrorOnNonSuccess">true to throw an exception when an error occurse, else null is returned</param> /// <param name="token">CancellationToken</param> /// <param name="httpSettings">IHttpSettings instance or null if the global settings need to be used</param> /// <returns>MemoryStream</returns> public static async Task<MemoryStream> GetAsMemoryStreamAsync(this Uri uri, bool throwErrorOnNonSuccess = true, CancellationToken token = default(CancellationToken), IHttpSettings httpSettings = null) { if (uri == null) { throw new ArgumentNullException(nameof(uri)); } using (var client = HttpClientFactory.CreateHttpClient(httpSettings, uri)) { return await client.GetAsMemoryStreamAsync(uri, throwErrorOnNonSuccess, token); } }
/// <summary> /// Helper method to configure the IChangeableHttpBehaviour /// </summary> /// <param name="behaviour">IChangeableHttpBehaviour</param> /// <param name="httpSettings">IHttpSettings</param> /// <returns>the behaviour, but configured as IHttpBehaviour </returns> private IHttpBehaviour ConfigureBehaviour(IChangeableHttpBehaviour behaviour, IHttpSettings httpSettings = null) { #if NET45 || NET46 // Add SvgBitmapHttpContentConverter if it was not yet added if (HttpExtensionsGlobals.HttpContentConverters.All(x => x.GetType() != typeof(SvgBitmapHttpContentConverter))) { HttpExtensionsGlobals.HttpContentConverters.Add(SvgBitmapHttpContentConverter.Instance.Value); } #endif behaviour.HttpSettings = httpSettings ?? HttpExtensionsGlobals.HttpSettings.ShallowClone(); #if NET45 || NET46 // Disable caching, if no HTTP settings were provided. if (httpSettings == null) { behaviour.HttpSettings.RequestCacheLevel = RequestCacheLevel.NoCacheNoStore; } #endif // Using our own Json Serializer, implemented with Json.NET behaviour.JsonSerializer = new JsonNetJsonSerializer(); behaviour.OnHttpRequestMessageCreated = httpMessage => { httpMessage?.Headers.TryAddWithoutValidation("X-Atlassian-Token", "nocheck"); if (!string.IsNullOrEmpty(_user) && _password != null) { httpMessage?.SetBasicAuthorization(_user, _password); } return(httpMessage); }; return(behaviour); }
/// <summary> /// Simple extension to post Form-URLEncoded Content /// </summary> /// <param name="uri">Uri to post to</param> /// <param name="formContent">Dictionary with the values</param> /// <param name="token">Cancellationtoken</param> /// <param name="httpSettings">IHttpSettings instance or null if the global settings need to be used</param> /// <returns>HttpResponseMessage</returns> public static async Task<HttpResponseMessage> PostFormUrlEncodedAsync(this Uri uri, IDictionary<string, string> formContent, CancellationToken token = default(CancellationToken), IHttpSettings httpSettings = null) { if (uri == null) { throw new ArgumentNullException(nameof(uri)); } if (formContent == null) { throw new ArgumentNullException(nameof(formContent)); } using (var content = new FormUrlEncodedContent(formContent)) using (var client = HttpClientFactory.CreateHttpClient(httpSettings, uri)) { return await client.PostAsync(uri, content, token); } }
/// <summary> /// Helper method to configure the IChangeableHttpBehaviour /// </summary> /// <param name="behaviour">IChangeableHttpBehaviour</param> /// <param name="httpSettings">IHttpSettings</param> /// <returns>the behaviour, but configured as IHttpBehaviour </returns> protected IHttpBehaviour ConfigureBehaviour(IChangeableHttpBehaviour behaviour, IHttpSettings httpSettings = null) { behaviour.HttpSettings = httpSettings ?? HttpExtensionsGlobals.HttpSettings; #if NET471 || NET461 // Disable caching, if no HTTP settings were provided. // This is needed as was detected here: https://github.com/dapplo/Dapplo.Confluence/issues/11 if (httpSettings == null) { behaviour.HttpSettings.RequestCacheLevel = RequestCacheLevel.NoCacheNoStore; } #endif // Using our own Json Serializer, implemented with JsonNetJsonSerializer behaviour.JsonSerializer = CreateJsonNetJsonSerializer(); behaviour.OnHttpRequestMessageCreated = httpMessage => { httpMessage?.Headers.TryAddWithoutValidation("X-Atlassian-Token", "no-check"); if (!string.IsNullOrEmpty(_user) && _password != null) { httpMessage?.SetBasicAuthorization(_user, _password); } return(httpMessage); }; return(behaviour); }
/// <summary> /// Helper method to configure the IChangeableHttpBehaviour /// </summary> /// <param name="behaviour">IChangeableHttpBehaviour</param> /// <param name="httpSettings">IHttpSettings</param> /// <returns>the behaviour, but configured as IHttpBehaviour </returns> private IHttpBehaviour ConfigureBehaviour(IChangeableHttpBehaviour behaviour, IHttpSettings httpSettings = null) { #if NET45 || NET46 if (HttpExtensionsGlobals.HttpContentConverters.All(x => x.GetType() != typeof(SvgBitmapHttpContentConverter))) { HttpExtensionsGlobals.HttpContentConverters.Add(SvgBitmapHttpContentConverter.Instance.Value); } #endif behaviour.HttpSettings = httpSettings ?? HttpExtensionsGlobals.HttpSettings; behaviour.OnHttpRequestMessageCreated = httpMessage => { httpMessage?.Headers.TryAddWithoutValidation("X-Atlassian-Token", "nocheck"); if (!string.IsNullOrEmpty(_user) && _password != null) { httpMessage?.SetBasicAuthorization(_user, _password); } return(httpMessage); }; return(behaviour); }
/// <summary> /// Create the JiraApi object, here the HttpClient is configured /// </summary> /// <param name="baseUri">Base URL, e.g. https://yourjiraserver</param> /// <param name="httpSettings">IHttpSettings or null for default</param> private JiraClient(Uri baseUri, IHttpSettings httpSettings = null) : this(baseUri) { Behaviour = ConfigureBehaviour(new HttpBehaviour(), httpSettings); }
/// <summary> /// Download a uri response as string /// </summary> /// <param name="uri">An Uri to specify the download location</param> /// <param name="token">CancellationToken</param> /// <param name="httpSettings">IHttpSettings instance or null if the global settings need to be used</param> /// <returns>HttpResponseMessage</returns> public static async Task<HttpResponseMessage> GetAsync(this Uri uri, CancellationToken token = default(CancellationToken), IHttpSettings httpSettings = null) { if (uri == null) { throw new ArgumentNullException(nameof(uri)); } using (var client = HttpClientFactory.CreateHttpClient(httpSettings, uri)) { return await client.GetAsync(uri, token).ConfigureAwait(false); } }
/// <summary> /// Create the JiraApi object, here the HttpClient is configured /// </summary> /// <param name="baseUri">Base URL, e.g. https://yourjiraserver</param> /// <param name="httpSettings">IHttpSettings or null for default</param> public JiraApi(Uri baseUri, IHttpSettings httpSettings = null) : this(baseUri) { _behaviour = ConfigureBehaviour(new HttpBehaviour(), httpSettings); }
/// <summary> /// This creates an advanced HttpMessageHandler, used in desktop applications /// Should be preferred /// </summary> /// <param name="suppliedHttpSettings">IHttpSettings instance or null if the global settings need to be used</param> /// <returns>HttpMessageHandler (WebRequestHandler)</returns> public static HttpMessageHandler CreateWebRequestHandler(IHttpSettings suppliedHttpSettings = null) { var httpSettings = suppliedHttpSettings ?? HttpSettings.GlobalHttpSettings; var webRequestHandler = new WebRequestHandler(); SetDefaults(webRequestHandler, httpSettings); return webRequestHandler; }
/// <summary> /// Create the IConfluenceClient, using OAuth 1 for the communication, here the HttpClient is configured /// </summary> /// <param name="baseUri">Base URL, e.g. https://yourconfluenceserver</param> /// <param name="confluenceOAuthSettings">ConfluenceOAuthSettings</param> /// <param name="httpSettings">IHttpSettings or null for default</param> public static IConfluenceClient Create(Uri baseUri, ConfluenceOAuthSettings confluenceOAuthSettings, IHttpSettings httpSettings = null) { return(new ConfluenceOAuthClient(baseUri, confluenceOAuthSettings, httpSettings)); }
/// <summary> /// Helper method to configure the IChangeableHttpBehaviour /// </summary> /// <param name="behaviour">IChangeableHttpBehaviour</param> /// <param name="httpSettings">IHttpSettings</param> /// <returns>the behaviour, but configured as IHttpBehaviour </returns> private IHttpBehaviour ConfigureBehaviour(IChangeableHttpBehaviour behaviour, IHttpSettings httpSettings = null) { behaviour.HttpSettings = httpSettings ?? HttpExtensionsGlobals.HttpSettings; behaviour.OnHttpRequestMessageCreated = httpMessage => { httpMessage?.Headers.TryAddWithoutValidation("X-Atlassian-Token", "no-check"); if (!string.IsNullOrEmpty(_user) && _password != null) { httpMessage?.SetBasicAuthorization(_user, _password); } return(httpMessage); }; return(behaviour); }