Ejemplo n.º 1
0
        async Task SendAsync(
            IXipRequestMessage message,
            Action <object> responseHandler,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (responseHandler == null)
            {
                throw new ArgumentNullException(nameof(responseHandler));
            }

            var response = await httpClient.SendAsync(
                new HttpRequestMessage (HttpMethod.Post, "/api/v1") {
                Content = new XipRequestMessageHttpContent(message)
            },
                HttpCompletionOption.ResponseHeadersRead,
                cancellationToken);

            if (!response.IsSuccessStatusCode)
            {
                var bodyContent = await response.Content.ReadAsStringAsync();

                Log.Error(TAG, $"Server returned error, body content is: {bodyContent}.");
                response.EnsureSuccessStatusCode();
            }

            using (var contentStream = new HttpResponseStream(await response.Content.ReadAsStreamAsync())) {
                var responseSerializer = new XipSerializer(contentStream, InteractiveSerializerSettings.SharedInstance);

                while (!cancellationToken.IsCancellationRequested)
                {
                    try {
                        var responseMessage = responseSerializer.Deserialize();
                        if (responseMessage is XipEndOfMessagesMessage)
                        {
                            break;
                        }
                        responseHandler(responseMessage);
                    } catch (EndOfStreamException) {
                        break;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        async Task <TResult> SendAsync <TResult> (
            IXipRequestMessage message,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            TResult         result      = default(TResult);
            XipErrorMessage errorResult = null;

            await SendAsync(message, response => {
                errorResult = response as XipErrorMessage;
                if (errorResult == null)
                {
                    result = (TResult)response;
                }
            }, cancellationToken);

            if (errorResult != null)
            {
                Log.Error(TAG, $"SendAsync ({message.GetType ()}) -> " +
                          $"{typeof (TResult)}: {errorResult}");
                errorResult.Throw();
            }

            return(result);
        }
Ejemplo n.º 3
0
            public XipRequestMessageHttpContent(IXipRequestMessage message)
            {
                this.message = message;

                EnsureHeaders();
            }