示例#1
0
        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);
        }
示例#2
0
        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}");
            }
        }