UnoSD/SparkSharp

Example Fails on JSON Deserialization

GitHubWhileAtWork opened this issue · 0 comments

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