in RESTProxy/Models/ProxyManager.cs [277:365]
private static bool TryGetEndpoint(
string tenantId,
string tenantName,
EndpointType endpointType,
out Endpoint endpoint,
out HttpResponseMessage errorResponse)
{
endpoint = null;
errorResponse = null;
string errorMessage = string.Empty;
TenantEndpointCollection tenantEndpointCollection;
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 tenantEndpointCollection))
{
endpoint = tenantEndpointCollection.GetNextEndpoint(endpointType);
return true;
}
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 tenantEndpointCollection))
{
endpoint = tenantEndpointCollection.GetNextEndpoint(endpointType);
return true;
}
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 tenantEndpointCollection))
{
endpoint = tenantEndpointCollection.GetNextEndpoint(endpointType);
return true;
}
else
{
errorMessage = string.Format(
"This Proxy is not configured to handle requests for the requested Tenant [{0}].",
tenantName);
return false;
}
}
}
catch (KeyNotFoundException ex)
{
errorMessage = ex.Message;
return false;
}
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)
};
}
}
}