Invoking a method from a string method name

w2olves

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

Evk

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Python

Invoking QML method from Python

From Dev

Invoking a method from parent fragment

From Dev

Invoking A Method From Another File C#

From Dev

Nameko - invoking RPC method from another service

From Java

Invoking Java main method with parameters from Eclipse

From Dev

Invoking a Runtime method from within Codename One

From Dev

Invoking a method from a RecyclerView obtained using reflection

From Dev

Invoking operator[] method from within object

From Dev

Invoking a method from null pointer in the lambda

From Dev

Invoking generic method from Main using reflection

From Dev

Invoking main() method from parent class

From Dev

Invoking a toString method from another class

From Dev

Mockito - NullpointerException when invoking method from Service

From Dev

Resolve method name from string defined in object

From Dev

Pass method to function using method name from string variable

From

Calling a Method From a String With the Method's Name in Ruby

From Dev

Invoke method in new thread (method name from string)?

From Java

Java invoking command line arguments from the main method to a separate method

From Dev

Invoking Future method in a loop

From Java

Invoking abstract method of enum

From Dev

Invoking private method issue

From Dev

Use of _ when invoking a method

From Java

Invoking a method of an anonymous class

From Dev

Invoking a Perl method

From Java

Invoking a Java Method in JSP

From Dev

Invoking method issue in Java

From Dev

Cost of invoking a method on Android

From Dev

SignalR not invoking method on server

From Dev

Invoking an anonymous method