async Task <RequestResult> DoRequestAsync(HttpMethodKind method, string path, string data, IReadOnlyDictionary <string, string> query = null)
        {
            var dataString  = data == null ?  null : new StringContent(data);
            var queryString = query == null ? null : QueryString(query);

            if (queryString != null)
            {
                path = $"{path}?{queryString}";
            }

            var startTime = DateTime.UtcNow;

            var message = new HttpRequestMessage(new HttpMethod(method.Name()), path);

            message.Content = dataString;
            message.Headers.Authorization = authHeader;

            var last = lastSeen.Txn;

            if (last.HasValue)
            {
                message.Headers.Add("X-Last-Seen-Txn", last.Value.ToString());
            }

            var httpResponse = await client.SendAsync(message).ConfigureAwait(false);

            string response;

            if (httpResponse.Content.Headers.ContentEncoding.Any(encoding => encoding == "gzip"))
            {
                response = await DecompressGZip(httpResponse.Content).ConfigureAwait(false);
            }
            else
            {
                response = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
            }

            var endTime = DateTime.UtcNow;

            if (httpResponse.Headers.Contains("X-Txn-Time"))
            {
                // there shouldn't ever be more than one...
                var time = httpResponse.Headers.GetValues("X-Txn-Time").First();

                lastSeen.SetTxn(Convert.ToInt64(time));
            }

            return(new RequestResult(method, path, query, data, response, (int)httpResponse.StatusCode, ToDictionary(httpResponse.Headers), startTime, endTime));
        }
Example #2
0
        async Task <RequestResult> DoRequestAsync(HttpMethodKind method, string path, string data, IReadOnlyDictionary <string, string> query = null, TimeSpan?queryTimeout = null)
        {
            var dataString  = data == null ?  null : new StringContent(data);
            var queryString = query == null ? null : QueryString(query);

            if (queryString != null)
            {
                path = $"{path}?{queryString}";
            }

            var startTime = DateTime.UtcNow;

            var message = new HttpRequestMessage(new HttpMethod(method.Name()), $"{endpoint}{path}");

            message.Content = dataString;
            message.Headers.Authorization = authHeader;
            message.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
            message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            message.Headers.Add("X-FaunaDB-API-Version", "3");
            message.Headers.Add("X-Fauna-Driver", "csharp");

            var last = lastSeen.Txn;

            if (last.HasValue)
            {
                message.Headers.Add("X-Last-Seen-Txn", last.Value.ToString());
            }

            TimeSpan?timeout = queryTimeout ?? clientTimeout ?? client.Timeout;

            if (timeout.HasValue)
            {
                message.SetTimeout(timeout);
                message.Headers.Add("X-Query-Timeout", timeout.Value.TotalMilliseconds.ToString());
            }

            var httpResponse = await client.SendAsync(message, CancellationToken.None).ConfigureAwait(false);

            string response;

            if (httpResponse.Content.Headers.ContentEncoding.Any(encoding => encoding == "gzip"))
            {
                response = await DecompressGZip(httpResponse.Content).ConfigureAwait(false);
            }
            else
            {
                response = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
            }

            var endTime = DateTime.UtcNow;

            if (httpResponse.Headers.Contains("X-Txn-Time"))
            {
                // there shouldn't ever be more than one...
                var time = httpResponse.Headers.GetValues("X-Txn-Time").First();

                lastSeen.SetTxn(Convert.ToInt64(time));
            }

            return(new RequestResult(method, path, query, data, response, (int)httpResponse.StatusCode, ToDictionary(httpResponse.Headers), startTime, endTime));
        }