vassilych / cscs

CSCS: Customized Scripting in C#
MIT License
169 stars 49 forks source link

Call EvaluateAsync #17

Open bakman23 opened 4 years ago

bakman23 commented 4 years ago

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 commented 4 years ago

Hey, sure. If you provide me with more details around it and what you are trying to achieve, I can try to help you... Thanks, Vassili

On Wed, Mar 4, 2020 at 4:33 PM bakman23 notifications@github.com wrote:

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 :)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/vassilych/cscs/issues/17?email_source=notifications&email_token=AE74VFEWMKOEG7MVEU72S4TRFZYDJA5CNFSM4LBK4Y72YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4ISNRCNA, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE74VFFJO4VKS63YF252CHLRFZYDJANCNFSM4LBK4Y7Q .

bakman23 commented 4 years ago

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 commented 4 years ago

Can you send the whole class, including its name and how it’s registered with the parser (if registered)?

On Thu, 5 Mar 2020 at 10:19, bakman23 notifications@github.com wrote:

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 ?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/vassilych/cscs/issues/17?email_source=notifications&email_token=AE74VFDLOJXBUMQSUIIROQ3RF5VD3A5CNFSM4LBK4Y72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEN4MOOQ#issuecomment-595117882, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE74VFGLMCH7VRP6KOS4OKLRF5VD3ANCNFSM4LBK4Y7Q .

bakman23 commented 4 years ago

` 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 commented 4 years ago

Basically you don't need to override anything. First you register your class with the parser as follows:

ParserFunction.RegisterFunction("ApiGet", new ApiGetFonction());

You already do this. So now every time the parser parses ApiGet(); the code in EvaluateAsync() will be triggered.

So to change the functionality you just change the contents of EvaluateAsync() method. Let me know if I understand your question right.

Thanks, Vassili

On Thu, Mar 5, 2020 at 11:07 AM bakman23 notifications@github.com wrote:

` class Api {

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

}

class ApiGetFonction : ParserFunction { 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 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();

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/vassilych/cscs/issues/17?email_source=notifications&email_token=AE74VFFW6IZBWNCDABQ4XSDRF52VFA5CNFSM4LBK4Y72YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEN4SIEQ#issuecomment-595141650, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE74VFB5JQWEMCUUUQYLYPLRF52VFANCNFSM4LBK4Y7Q .