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

Example Fails on JSON Deserialization #1

Open
GitHubWhileAtWork opened this issue Apr 3, 2020 · 0 comments
Open

Example Fails on JSON Deserialization #1

GitHubWhileAtWork opened this issue Apr 3, 2020 · 0 comments

Comments

@GitHubWhileAtWork
Copy link

GitHubWhileAtWork commented Apr 3, 2020

The below example successfully opens a session, submits comamnds and receives results. Unfortunately, the result is then combed through in the async Task ExecuteStatementAsync(string code, bool silently) of LivySession.cs, resulting in a plain text string being passed to a JSON deserialize command. On this final line of the method, it throws a helpful exception, which is then passed on down the line, culminating in the unhelpful exception:

"Unexpected character encountered while parsing value: r. Path '', line 1, position 1."

It's not obvious what to return from the async Task ExecuteStatementAsync method.

Example Code to which I refer:

using (var client = new LivyClient("http://url-to-livy", "username", "password"))
using (var session = await client.CreateSessionAsync(SimpleExampleSessionConfiguration.GetConfiguration()))
{
    var sum = await session.ExecuteStatementAsync<int>("val res = 1 + 1\nprintln(res)");
    
    // Prints 2
    Console.WriteLine(sum);
}

Where exception occurs (return statement):

        async Task<T> ExecuteStatementAsync<T>(string code, bool silently)
        {
            if (!silently)
                Log("Waiting for session to be ready...");

            await WaitForSessionAsync().ConfigureAwait(false);

            if (!silently)
                Log("Session ready");

            if (!silently)
                Log("Running code...");

            var response = await _client.PostAsync($"{_sessionPath}/statements", new { code })
                                        .ConfigureAwait(false);

            if (!response.IsSuccessStatusCode)
                Log(await response.Content.ReadAsStringAsync().ConfigureAwait(false));

            response.EnsureSuccessStatusCode();

            var resultPollingRelativePath = response.Headers.Location.AsRelativePath();

            if (!silently)
                Log("Waiting for results to be ready...");

            // TODO: This is not a session state, maybe a statement state?
            var result = await WaitForStatesAsync(resultPollingRelativePath, SessionState.Available).ConfigureAwait(false);
            var output = result["output"];

            ThrowIfError(output);

            var data = output["data"]["text/plain"].ToString();

            if (!silently)
                Log("Results ready");

            return JsonConvert.DeserializeObject<T>(data);
        }

Thanks
Andrew

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

1 participant