/// <inheritdoc /> public async Task <T> Invoke <T>(string serviceName, string serviceMethod, params INextApiArgument[] arguments) { var command = NextApiClientUtils.PrepareCommand(serviceName, serviceMethod, arguments); if (TransportType == NextApiTransport.Http || arguments.Any(a => a is NextApiFileArgument) || typeof(T) == typeof(NextApiFileResponse)) { return(await InvokeHttp <T>(command)); } return(await InvokeSignalR <T>(command)); }
private async Task <T> InvokeHttp <T>(NextApiCommand command) { var form = NextApiClientUtils.PrepareRequestForm(command); HttpResponseMessage response; try { using var client = await GetHttpClient(); response = await client.PostAsync($"{Url}/http", form); response.EnsureSuccessStatusCode(); } catch (Exception ex) { throw new NextApiException(NextApiErrorCode.HttpError, ex.Message); } // check that response can processed as json if (!response.Content.Headers.ContentType.MediaType.Contains("application/json")) { if (typeof(T) != typeof(NextApiFileResponse)) { throw new NextApiException( NextApiErrorCode.IncorrectRequest, "Please specify correct return type for this request. Use NextApiFileResponse." ); } try { return(await NextApiClientUtils.ProcessNextApiFileResponse(response) as dynamic); } catch (Exception ex) { throw new NextApiException(NextApiErrorCode.HttpError, ex.Message); } } // process as normal nextapi response var data = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject <NextApiResponseJsonWrapper <T> >(data, SerializationUtils.GetJsonConfig()); if (!result.Success) { throw NextApiClientUtils.NextApiException(result.Error); } return(result.Data); }
/// <inheritdoc /> public async Task Invoke(string serviceName, string serviceMethod, params INextApiArgument[] arguments) { var command = NextApiClientUtils.PrepareCommand(serviceName, serviceMethod, arguments); if (TransportType == NextApiTransport.Http || arguments.Any(a => a is NextApiFileArgument)) { await InvokeHttp <object>(command); return; } await InvokeSignalR <object>(command); }
private async Task <T> InvokeSignalR <T>(NextApiCommand command) { var connection = await GetConnection(); command.Args = command.Args.Where(arg => arg is NextApiArgument).ToArray(); NextApiResponse response; try { response = await connection.InvokeAsync <NextApiResponse>("ExecuteCommand", command); } catch (Exception e) { throw new NextApiException(NextApiErrorCode.SignalRError, e.Message); } if (response.Error != null) { throw NextApiClientUtils.NextApiException(response.Error); } return((T)response.Data); }
private async Task <T> InvokeHttp <T>(NextApiCommand command) { var httpSerializationType = HttpSerializationType; var form = NextApiClientUtils.PrepareRequestForm(command, httpSerializationType); HttpResponseMessage response; try { using var client = await GetHttpClient(); response = await client.PostAsync($"{Url}/http", form); response.EnsureSuccessStatusCode(); } catch (Exception ex) { throw new NextApiException(NextApiErrorCode.HttpError, ex.Message); } // check that response can processed as json var mediaType = response.Content.Headers.ContentType.MediaType; if (!mediaType.Contains("application/json") && typeof(T) == typeof(NextApiFileResponse)) { try { return(await NextApiClientUtils.ProcessNextApiFileResponse(response) as dynamic); } catch (Exception ex) { throw new NextApiException(NextApiErrorCode.HttpError, ex.Message); } } switch (httpSerializationType) { case SerializationType.Json: { // process as normal nextapi response var data = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject <NextApiResponseJsonWrapper <T> >(data, SerializationUtils.GetJsonConfig()); if (!result.Success) { throw NextApiClientUtils.NextApiException(result.Error); } return(result.Data); } case SerializationType.MessagePack: { var resultByteArray = await response.Content.ReadAsByteArrayAsync(); var result = (NextApiResponse)MessagePackSerializer.Typeless.Deserialize(resultByteArray); if (!result.Success) { throw NextApiClientUtils.NextApiException(result.Error); } return((T)result.Data); } default: throw new Exception($"Unsupported serialization type {HttpSerializationType}"); } }