Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call EvaluateAsync #17

Open
bakman23 opened this issue Mar 4, 2020 · 5 comments
Open

Call EvaluateAsync #17

bakman23 opened this issue Mar 4, 2020 · 5 comments

Comments

@bakman23
Copy link

bakman23 commented Mar 4, 2020

Hello,
is it possible to raise only this method when I call my custom function ?

` protected override async Task EvaluateAsync(ParsingScript script)
{
List args = script.GetFunctionArgs();
Utils.CheckArgs(args.Count, 1, m_name);

        var url = Utils.GetSafeString(args, 0);
        Variable results = await HttpGet(url);

        return results;
    }`

Thanks :)

@vassilych
Copy link
Owner

vassilych commented Mar 4, 2020 via email

@bakman23
Copy link
Author

bakman23 commented Mar 5, 2020

Thanks fr your response,
the case is simple I have a custom function and I need to call an async method in the method Evaluate.
I see it's possible to override EvaluateAsync, but I don't know how and when this method is called.

I find a solution,
Call directly EvaluateAsync from Evaluate method like that:

`
protected override Variable Evaluate(ParsingScript script)
{
return EvaluateAsync(script).Result;
}

    protected override async Task<Variable> EvaluateAsync(ParsingScript script)
    {
        List<Variable> args = script.GetFunctionArgs();
        Utils.CheckArgs(args.Count, 1, m_name);

        var url = Utils.GetSafeString(args, 0);
        Variable results = await HttpGetAsync(url);

        return results;
    }

`
Is it correct ?

@vassilych
Copy link
Owner

vassilych commented Mar 5, 2020 via email

@bakman23
Copy link
Author

bakman23 commented Mar 5, 2020

`
class Api {

    public static void Init()
    {
        ParserFunction.RegisterFunction("ApiGet", new ApiGetFonction());
    }

}

class ApiGetFonction : ParserFunction
{
    protected override async Task<Variable> EvaluateAsync(ParsingScript script)
    {
        List<Variable> args = script.GetFunctionArgs();
        Utils.CheckArgs(args.Count, 1, m_name);

        var url = Utils.GetSafeString(args, 0);
        Variable results = await HttpGetAsync(url);

        return results;
    }

    protected override Variable Evaluate(ParsingScript script)
    { 
        return EvaluateAsync(script).Result;
    }

    public async Task<Variable> HttpGetAsync(string url) {

        HttpClient client = new HttpClient();
        HttpResponseMessage response = await client.GetAsync(url);
        response.EnsureSuccessStatusCode();
        string responseBody = await response.Content.ReadAsStringAsync();


        Variable results = new Variable(Variable.VarType.STRING);

        results.AddVariable(new Variable(responseBody));

        return results;
    }


}

`

And the registration is in Interpreter.cs -> RegisterFunctions()

Api.Init();

@vassilych
Copy link
Owner

vassilych commented Mar 6, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants