Пример #1
0
        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 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;

            return(new RequestResult(method, path, query, data, response, (int)httpResponse.StatusCode, ToDictionary(httpResponse.Headers), startTime, endTime));
        }
Пример #2
0
        async Task <StreamingRequestResult> DoStreamingRequestAsync(string data, IReadOnlyDictionary <string, string> query = null)
        {
            var path        = StreamingPath;
            var dataString  = data == null ?  null : new StringContent(data, Encoding.UTF8, "application/json");
            var queryString = query == null ? null : QueryString(query);

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

            var startTime = DateTime.UtcNow;

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

            message.Content = dataString;
            message.Headers.Authorization = authHeader;
            message.Headers.Add("X-FaunaDB-API-Version", "4");
            message.Headers.Add("X-Fauna-Driver", "csharp");

            var last = lastSeen.Txn;

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

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

            Stream response = await httpResponse.Content.ReadAsStreamAsync();

            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));
            }

            var errorContent = String.Empty;

            if (!httpResponse.IsSuccessStatusCode)
            {
                StreamReader streamReader = new StreamReader(response);
                errorContent = await streamReader.ReadLineAsync();
            }

            return(new StreamingRequestResult(query, data, response, (int)httpResponse.StatusCode, errorContent, ToDictionary(httpResponse.Headers), startTime, endTime));
        }
Пример #3
0
        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));
        }
Пример #4
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));
        }