public void ToString_UseDifferentProductInfos_AllSerializedCorrectly() { ProductInfoHeaderValue productInfo = new ProductInfoHeaderValue("product", "1.0"); Assert.Equal("product/1.0", productInfo.ToString()); productInfo = new ProductInfoHeaderValue("(comment)"); Assert.Equal("(comment)", productInfo.ToString()); }
public SyncClient( string auth, int timeout, ProductInfoHeaderValue userAgent ) { _auth = auth; _timeout = timeout; _userAgent = userAgent.ToString(); }
public void ToString_Aggregate_AllSerializedCorrectly() { HttpRequestMessage request = new HttpRequestMessage(); string input = string.Empty; ProductInfoHeaderValue productInfo = new ProductInfoHeaderValue("product", "1.0"); Assert.Equal("product/1.0", productInfo.ToString()); input += productInfo.ToString(); request.Headers.UserAgent.Add(productInfo); productInfo = new ProductInfoHeaderValue("(comment)"); Assert.Equal("(comment)", productInfo.ToString()); input += " " + productInfo.ToString(); // Space delineated request.Headers.UserAgent.Add(productInfo); Assert.Equal(input, request.Headers.UserAgent.ToString()); }
/// <summary> /// Creates a new System.Net.Http.HttpClient instance configured with the Graph.Community middleware plus the handlers provided. /// </summary> /// <param name="options">The <see cref="CommunityGraphClientOptions"/> to use.</param> /// <param name="handlers">An ordered list of System.Net.Http.DelegatingHandler instances to be invoked</param> /// <param name="version">The graph version to use.</param> /// <param name="nationalCloud">The national cloud endpoint to use.</param> /// <param name="proxy">The proxy to be used with created client.</param> /// <param name="finalHandler">The last HttpMessageHandler to HTTP calls.</param> /// <returns>A GraphServiceClient instance with the configured handlers.</returns> public static GraphServiceClient Create(CommunityGraphClientOptions options, IList <DelegatingHandler> handlers, string version = "v1.0", string nationalCloud = "Global", IWebProxy proxy = null, HttpMessageHandler finalHandler = null) { if (options == null) { throw new ArgumentNullException("options"); } ProductInfoHeaderValue defaultUserAgent = defaultDecoration.ToUserAgent(); ProductInfoHeaderValue specifiedUserAgent = default; if (!options.UserAgentInfo.IsEmpty()) { specifiedUserAgent = options.UserAgentInfo.ToUserAgent(); } else { // if we got a user agent string, validate it if (!string.IsNullOrEmpty(options.UserAgent)) { if (!ProductInfoHeaderValue.TryParse(options.UserAgent, out specifiedUserAgent)) { throw new ArgumentOutOfRangeException("CommunityGraphClientOptions.UserAgent", "Cannot parse UserAgent string"); } } } handlers.Insert(0, new SharePointServiceHandler()); var httpClient = GraphClientFactory.Create(handlers, version, nationalCloud, proxy, finalHandler); if (specifiedUserAgent != null) { httpClient.DefaultRequestHeaders.UserAgent.Add(specifiedUserAgent); } // if the provided string does not have the SharePoint throttling decoration, add the library user agent to the default. // https://docs.microsoft.com/en-us/sharepoint/dev/general-development/how-to-avoid-getting-throttled-or-blocked-in-sharepoint-online#how-to-decorate-your-http-traffic-to-avoid-throttling if (!specifiedUserAgent.ToString().Contains("ISV")) { httpClient.DefaultRequestHeaders.UserAgent.Add(defaultUserAgent); } httpClient.DefaultRequestHeaders.Add(CommunityGraphConstants.Library.VersionHeaderName, CommunityGraphConstants.Library.VersionHeaderValue); var graphServiceClient = new GraphServiceClient(httpClient); return(graphServiceClient); }
internal SenseRealtimeClient(int monitorId, string token) { _handlers["realtime_update"] = HandleRealtimeUpdate; _wssUrl = $"wss://clientrt.sense.com/monitors/{monitorId}/realtimefeed?access_token={token}"; _receiverCts = new CancellationTokenSource(); _wsClient = new ClientWebSocket(); _receiveBufferPool = ArrayPool <byte> .Create(65536, 20); var version = GetType().Assembly.GetName().Version.ToString(); var uaHeader = new ProductInfoHeaderValue(new ProductHeaderValue(HttpUtility.UrlEncode("https://github.com/cisien/SenseClient"), version)); _wsClient.Options.SetRequestHeader("User-Agent", uaHeader.ToString()); }
private WebClient GetWebClient() { var client = new WebClient(); var assembly = GetType().Assembly; var product = assembly.GetName().Name; var version = assembly .GetCustomAttributes <AssemblyInformationalVersionAttribute>() .FirstOrDefault()?.InformationalVersion ?? "0.0.0"; var productInfoHeader = new ProductInfoHeaderValue(product, version); client.Headers.Add("user-agent", productInfoHeader.ToString()); return(client); }
/// <summary> /// Matcher to check if a <see cref="ReportedErrorEvent"/> matches one /// generated by <see cref="CreateComplexContext"/> /// </summary> private ReportedErrorEvent IsComplexContext() { bool isWindows = Environment.OSVersion.ToString().Contains("Windows"); return(Match.Create <ReportedErrorEvent>(e => e.Message.Contains(SimpleException.Message) && e.Context.HttpRequest.Method.Equals(DeleteMethod.ToString()) && e.Context.HttpRequest.Url.Equals(GoogleUri.ToString()) && e.Context.HttpRequest.UserAgent.Equals(UserAgentValue.ToString()) && e.Context.HttpRequest.ResponseStatusCode == (int)ConflictStatusCode && (!isWindows || e.Context.ReportLocation.LineNumber > 0) && (!isWindows || !string.IsNullOrEmpty(e.Context.ReportLocation.FilePath)) && e.Context.ReportLocation.FunctionName.Equals("CreateComplexContext") && e.ServiceContext.Service.Equals(ServiceName) && e.ServiceContext.Version.Equals(Version) )); }