I have a two sets of Urls one for PreProd and one for Prod. Each Url has several API nodes. Instead of hard coding these API nodes, I maintain them in an enum
Something like this:
//Prod
private enum Prod
{
precheckorder,
submitresubmit,
creditInquiry,
createupdateorder,
confirmorder,
getorderstatus,
cancelorder,
}
/// <summary>
/// Gets the relative URL.
/// </summary>
/// <param name="u">The u.</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
private static string GetRelativeUrl(Prod u)
{
switch (u)
{
case Prod.precheckorder:
return "https://contesa.tex.com/api/precheckorder";
case Prod.submitresubmit:
return "https://contesa.tex.com/api/submitresubmit";
case Prod.creditInquiry:
return "https://contesa.tex.com/api/creditinquiry";
case Prod.createupdateorder:
return "https://contesa.tex.com/api/createupdateorder";
case Prod.confirmorder:
return "https://contesa.tex.com/api/confirmorder";
case Prod.getorderstatus:
return "https://contesa.tex.com/api/getorderstatus";
case Prod.cancelorder:
return "https://contesa.tex.com/api/cancelorder";
default:
// Handle bad URL, possibly throw
throw new Exception();
}
}
We use environment variables to store the Environment name and thats what dictates which API set to use.
Ideally, I would like to have a single method, I pass in my environment and api name and it will return the API Url.
Something like
GettexApiUrlBasedOnEnvironment("Dev", "precheckorder");
and response will be
"https://contoso.tex.com/api/precheckorder"
Any ideas/suggestions will be much appreciated. TIA
Just store your urls in one dictionary, like this:
public enum ApiType
{
precheckorder,
submitresubmit,
creditInquiry,
createupdateorder,
confirmorder,
getorderstatus,
cancelorder,
}
public enum EnvironmentType {
Dev,
Prod
}
public static string GettexApiUrl(ApiType apiType) {
var envRaw = Environment.GetEnvironmentVariable("YourVariable");
EnvironmentType env;
if (!Enum.TryParse(envRaw, out env))
throw new Exception("Invalid environment provided in environment variable YourVariable: " + envRaw);
return GettexApiUrlBasedOnEnvironment(env, apiType);
}
public static string GettexApiUrlBasedOnEnvironment(EnvironmentType env, ApiType apiType) {
if (!_urls.ContainsKey(env))
throw new Exception("Invalid environment " + env);
var url = _urls[env];
if (!url.ContainsKey(apiType))
throw new Exception("Invalid api type " + apiType);
return url[apiType];
}
private static readonly Dictionary<EnvironmentType, Dictionary<ApiType, string>> _urls = new Dictionary<EnvironmentType, Dictionary<ApiType, string>>(
) {
{EnvironmentType.Dev, new Dictionary<ApiType, string>() {
{ApiType.precheckorder, "https://contoso.tex.com/api/precheckorder"},
// etc
} }, {
EnvironmentType.Prod, new Dictionary<ApiType, string>() {
{ApiType.precheckorder, "https://contesa.tex.com/api/precheckorder"},
}},
};
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments