/// <summary> /// Wrapper used for the purposes of logging user requests to Application Insights /// in case we need to review user actions at a later time. /// </summary> /// <param name="userName">The user that requested the action</param> /// <param name="pathAndQuery"> /// The UriAbsolutePath and Query properties. Simply, this is the entire Uri except for /// the protocol and domain information. /// </param> /// <param name="method">The <see cref="HttpMethod"/> of the REST API.</param> /// <param name="tenantId">The TenantId of the account that this request was for.</param> /// <param name="tenantName">The friendly name of <paramref name="tenantId"/>.</param> /// <param name="endpointType">The type of endpoint that should be used for the request.</param> /// <param name="statusCode">The <see cref="HttpStatusCode"/> for the result of the request.</param> /// <param name="correlationId">The ID given to the request by the API to enable post-mortem analysis.</param> /// <param name="duration">The total number of seconds that the request took to complete.</param> private static void LogTelemetryEvent( string userName, string pathAndQuery, HttpMethod method, string tenantId, string tenantName, EndpointType endpointType, HttpStatusCode statusCode, string correlationId, double duration) { ProxyManager.telemetryClient.Context.Session.Id = System.Guid.NewGuid().ToString(); ProxyManager.telemetryClient.Context.Component.Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); Dictionary <string, string> properties = new Dictionary <string, string>(); properties.Add("UserName", userName); properties.Add("PathAndQuery", pathAndQuery); properties.Add("Method", method.ToString()); properties.Add("StatusCode", statusCode.ToString()); properties.Add("CorrelationId", correlationId); properties.Add("EndpointType", endpointType.ToString()); properties.Add("TenantId", tenantId); properties.Add("TenantFriendlyName", tenantName); Dictionary <string, double> metrics = new Dictionary <string, double>(); metrics.Add("Duration", duration); ProxyManager.telemetryClient.TrackEvent("ProxyRequest", properties, metrics); }
public IEnumerable<Endpoint> GetMetadataByType(EndpointType endpointType) { using (var client = new HttpClient()) { var url = string.Concat(BaseUrl, "metadata/bytype/", endpointType.ToString()); var responseMessage = client.GetAsync(url).Result; var body = responseMessage.Content.ReadAsStringAsync().Result; return JsonConvert.DeserializeObject<List<Endpoint>>(body); } }
public static string GetContentType(EndpointType endpointType) { switch (endpointType) { case EndpointType.Soap11: case EndpointType.Soap12: case EndpointType.Xml: return Xml; case EndpointType.Json: return Json; case EndpointType.Jsv: return JsvText; case EndpointType.ProtoBuf: return ProtoBuf; default: throw new NotSupportedException(endpointType.ToString()); } }
public static string GetContentType(EndpointType endpointType) { switch (endpointType) { case EndpointType.Soap11: case EndpointType.Soap12: case EndpointType.Xml: return(Xml); case EndpointType.Json: return(Json); case EndpointType.Jsv: return(JsvText); case EndpointType.ProtoBuf: return(ProtoBuf); default: throw new NotSupportedException(endpointType.ToString()); } }
public List<Network> Last15Results(EndpointType type) { IEnumerable<Network> networks = (from net in tableNetwork where net.EndpointType.Equals(type.ToString()) orderby net.Id descending select net).Take(16); List<Network> lists = new List<Network>(networks); if (lists.Count != 0) { lists.RemoveAt(0); } foreach (Network net in lists) { net.PingStringToArrays(); net.GeneratingPingTcpStats(); net.GeneratingPingUdpStats(); } return lists; }
public List <Network> Last15Results(EndpointType type) { IEnumerable <Network> networks = (from net in tableNetwork where net.EndpointType.Equals(type.ToString()) orderby net.Id descending select net).Take(16); List <Network> lists = new List <Network>(networks); if (lists.Count != 0) { lists.RemoveAt(0); } foreach (Network net in lists) { net.PingStringToArrays(); net.GeneratingPingTcpStats(); net.GeneratingPingUdpStats(); } return(lists); }
/// <summary> /// Rotates through all the <see cref="Endpoint"/>s in this collection, round-robin style, /// and retrieves the next available one for the specified <paramref name="endpointType"/>. /// </summary> /// <param name="endpointType">The type of endpoint that should be used for the request.</param> /// <returns> /// The next <see cref="Endpoint"/> in the collection that matches the specified type. /// </returns> /// <exception cref="KeyNotFoundException"> /// No <see cref="Endpoint"/> is defined for the specified <paramref name="endpointType"/>. /// </exception> public Endpoint GetNextEndpoint(EndpointType endpointType) { try { this.semaphore.Wait(); List <Endpoint> endpoints; if (this.EndpointsByType.TryGetValue(endpointType, out endpoints)) { int nextIndex = this.NextEndpointIndex[endpointType]; Endpoint endpoint = endpoints[nextIndex]; // Careful not to do a post-increment here, as the increment would happen _after_ the assignment. // A pre-increment would work, but I'd argue that expliclty adding 1 here is more clear. this.NextEndpointIndex[endpointType] = nextIndex + 1; if (this.NextEndpointIndex[endpointType] >= endpoints.Count) { this.NextEndpointIndex[endpointType] = 0; } return(endpoint); } else { throw new KeyNotFoundException(string.Format( "This Proxy is not configured to handle requests for Tenant [{0} ({1})] with the endpoint type of [{2}].", this.TenantId, this.TenantFriendlyName, endpointType.ToString())); } } finally { this.semaphore.Release(); } }
public static string GetContentFormat(EndpointType endpointType) { return(endpointType.ToString().ToLower()); }
/// <summary> /// Tries to get the <see cref="Endpoint"/> that should be used to execute the request /// with the specified properties. /// </summary> /// <param name="tenantId"> /// [Optional] The tenantId that should be used for the request if the proxy supports /// multiple tenants. Mutually exclusive with <paramref name="tenantName"/> since /// <paramref name="tenantName"/> is a "friendly" version of this value. If neither this /// nor <paramref name="tenantName"/> are provided, the default TenantId will be used for /// this request. /// </param> /// <param name="tenantName"> /// [Optional] The friendly name of the <paramref name="tenantId"/> that should be used for /// the request if the proxy supports multiple tenants. Mutually exclusive with /// <paramref name="tenantId"/> since this is a friendly name version of that value. /// If neither this nor <paramref name="tenantId"/> are provided, the default TenantId will /// be used for this request. /// </param> /// <param name="endpointType">The type of endpoint that should be used for the request.</param> /// <param name="endpoint"> /// The resolved <see cref="Endpoint"/> that should be used for this request. /// </param> /// <param name="errorResponse"> /// This contains the <see cref="HttpResponseMessage"/> that should be returned to the user /// with the appropriate explanation if we are unable to determine a valid /// <see cref="Endpoint"/> to execute this request on. /// </param> /// <returns>true if a valid <see cref="Endpoint"/> was found; false otherwise.</returns> /// <remarks> /// We are intentionally trying to encapsulate the exception handling within here, hence the /// "Try" naming scheme that returns a boolean with <paramref name="endpoint"/> and /// <paramref name="errorResponse"/> as out parameters. /// <para /> /// When both tenantId and tenantName are specified, it will be considered a failure case. /// </remarks> private static bool TryGetEndpoint( string tenantId, string tenantName, EndpointType endpointType, out Endpoint endpoint, out HttpResponseMessage errorResponse) { endpoint = null; errorResponse = null; string errorMessage = string.Empty; Dictionary <EndpointType, Endpoint> endpointByType; try { if (string.IsNullOrWhiteSpace(tenantId) && string.IsNullOrWhiteSpace(tenantName)) { if (string.IsNullOrWhiteSpace(ProxyManager.defaultTenantId)) { errorMessage = "No TenantId was specified with this request, and this Proxy is not configured with a default TenantId."; return(false); } else { if (ProxyManager.endpointByTenantId.TryGetValue(ProxyManager.defaultTenantId, out endpointByType)) { if (!endpointByType.TryGetValue(endpointType, out endpoint)) { errorMessage = string.Format( "No TenantId was specified with this request, and the default TenantId for this Proxy is not configured to handle requests for the specified endpoint type [{0}].", endpointType.ToString()); return(false); } } else { errorMessage = "No TenantId was specified with this request, and the default TenantId for this Proxy is misconfigured."; return(false); } } } else if (!string.IsNullOrWhiteSpace(tenantId) && !string.IsNullOrWhiteSpace(tenantName)) { errorMessage = "Do not specify BOTH TenantId and TenantName. Only specify one of those values to avoid ambiguity."; return(false); } else if (!string.IsNullOrWhiteSpace(tenantId)) { if (ProxyManager.endpointByTenantId.TryGetValue(tenantId.ToLowerInvariant(), out endpointByType)) { if (!endpointByType.TryGetValue(endpointType, out endpoint)) { errorMessage = string.Format( "This Proxy is not configured to handle requests for TenantId [{0}] with the endpoint type of [{1}].", tenantId, endpointType.ToString()); return(false); } } else { errorMessage = string.Format( "This Proxy is not configured to handle requests for the requested TenantId [{0}].", tenantId); return(false); } } else { if (ProxyManager.endpointByTenantName.TryGetValue(tenantName.ToLowerInvariant(), out endpointByType)) { if (!endpointByType.TryGetValue(endpointType, out endpoint)) { errorMessage = string.Format( "This Proxy is not configured to handle requests for Tenant [{0}] with the endpoint type of [{1}].", tenantName, endpointType.ToString()); return(false); } } else { errorMessage = string.Format( "This Proxy is not configured to handle requests for the requested Tenant [{0}].", tenantName); return(false); } } return(true); } finally { if (endpoint == null) { const string ErrorMessageFormat = "{{\"code\":\"BadRequest\", \"message\":{0}}}"; string formattedError = string.Format(ErrorMessageFormat, JsonConvert.ToString(errorMessage)); errorResponse = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(formattedError, Encoding.UTF8, ProxyManager.JsonMediaType) }; } } }
// return the path + endpoint public string GetEndpoint() { char [] delimiterChars = { '_', ' ', ',', '.', ':', '\t' }; string [] endpointArr = selectedEndpoint.ToString().Split(delimiterChars); return(hosts [(int)selectedHost] + "" + endpointArr [1] + "/" + endpointArr [0]); }
public static string GetContentFormat(EndpointType endpointType) { return endpointType.ToString().ToLower(); }
/// <summary> /// Creates a new instance of the <see cref="MissingEndpointException"/> /// </summary> /// <param name="endpoint">The endpoint</param> public MissingEndpointException(EndpointType endpoint) : base(endpoint.ToString()) { }