public async Task Sync()
        {
            ClientRequest req = this.m_pendingRequest;

            if (req == null)
            {
                return;
            }

            // If there are no actions to dispatch, short-circuit without sending an empty request to the server
            if (!req.HasActions)
            {
                return;
            }

            this.m_pendingRequest = null;
            RequestMessageBody msgBody      = req.BuildRequestMessageBody();
            ClientRequestFlags requestFlags = req.Flags;

            RequestExecutorRequestMessage requestExecutorRequestMessage = new RequestExecutorRequestMessage();

            requestExecutorRequestMessage.Url  = Utility.CombineUrl(this.m_url, Constants.ProcessQuery);
            requestExecutorRequestMessage.Body = Utility.ToJsonString(msgBody);
            foreach (var pair in this.RequestHeaders)
            {
                requestExecutorRequestMessage.Headers[pair.Key] = pair.Value;
            }

            req.InvalidatePendingInvalidObjectPaths();

            RequestExecutorResponseMessage response = await this.m_requestExecutor.Execute(requestExecutorRequestMessage);

            JToken json = Utility.ToJsonObject(response.Body);

            this.m_processingResult = true;
            try
            {
                req.ProcessResponse(json);
            }
            finally
            {
                this.m_processingResult = false;
            }
        }
Example #2
0
        public async Task <RequestExecutorResponseMessage> Execute(RequestExecutorRequestMessage request)
        {
            HttpClient         client      = new HttpClient();
            HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post, request.Url);

            foreach (var pair in request.Headers)
            {
                httpRequest.Headers.Add(pair.Key, pair.Value);
            }
            string body = string.Empty;

            if (request.Body != null)
            {
                body = request.Body;
            }

            string contentType = request.ContentType;

            if (string.IsNullOrEmpty(contentType))
            {
                contentType = Constants.ContentTypeApplicationJson;
            }

            httpRequest.Content = new StringContent(body, Encoding.UTF8, contentType);
            HttpResponseMessage httpResponse = await client.SendAsync(httpRequest);

            RequestExecutorResponseMessage ret = new RequestExecutorResponseMessage();

            ret.StatusCode = httpResponse.StatusCode;
            foreach (var pair in httpResponse.Headers)
            {
                ret.Headers[pair.Key] = string.Join(",", pair.Value.ToArray());
            }

            ret.Body = await httpResponse.Content.ReadAsStringAsync();

            return(ret);
        }