Example #1
0
        public static HttpMethod ToHttpMethod(this HttpMethodKind httpMethodKind)
        {
            switch (httpMethodKind)
            {
            case HttpMethodKind.Get:
                return(HttpMethod.Get);

            case HttpMethodKind.Post:
                return(HttpMethod.Post);

            case HttpMethodKind.Put:
                return(HttpMethod.Put);

            case HttpMethodKind.Delete:
                return(HttpMethod.Delete);

            case HttpMethodKind.Patch:
                return(new HttpMethod("PATCH"));

            case HttpMethodKind.Head:
                return(new HttpMethod("HEAD"));

            case HttpMethodKind.Options:
                return(new HttpMethod("OPTIONS"));

            default:
                return(HttpMethod.Get);
            }
        }
Example #2
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));
        }
Example #3
0
        async Task <Value> Execute(HttpMethodKind action, string path, Expr data = null, IReadOnlyDictionary <string, string> query = null, TimeSpan?queryTimeout = null)
        {
            var dataString   = data == null ?  null : JsonConvert.SerializeObject(data, Formatting.None);
            var responseHttp = await clientIO.DoRequest(action, path, dataString, query, queryTimeout).ConfigureAwait(false);

            RaiseForStatusCode(responseHttp);

            var responseContent = FromJson(responseHttp.ResponseContent);

            return(responseContent["resource"]);
        }
        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 #5
0
 public RequestResult(
     HttpMethodKind method,
     string path,
     IReadOnlyDictionary <string, string> query,
     string requestContent,
     string responseContent,
     int statusCode,
     IReadOnlyDictionary <string, IEnumerable <string> > responseHeaders,
     DateTime startTime,
     DateTime endTime)
 {
     Method          = method;
     Path            = path;
     Query           = query;
     RequestContent  = requestContent;
     ResponseContent = responseContent;
     StatusCode      = statusCode;
     ResponseHeaders = responseHeaders;
     StartTime       = startTime;
     EndTime         = endTime;
 }
        /// <summary>
        /// All-caps name of the method.
        /// </summary>
        public static string Name(this HttpMethodKind method)
        {
            switch (method)
            {
            case HttpMethodKind.Get:
                return("GET");

            case HttpMethodKind.Post:
                return("POST");

            case HttpMethodKind.Put:
                return("PUT");

            case HttpMethodKind.Patch:
                return("PATCH");

            case HttpMethodKind.Delete:
                return("DELETE");
            }

            throw new ArgumentException($"Bad value: {method}");
        }
Example #7
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));
        }
Example #8
0
 public Task <RequestResult> DoRequest(HttpMethodKind method, string path, string data, IReadOnlyDictionary <string, string> query = null, TimeSpan?queryTimeout = null) =>
 DoRequestAsync(method, path, data, query, queryTimeout);
Example #9
0
 public Task <RequestResult> DoRequest(HttpMethodKind method, string path, string data, IReadOnlyDictionary <string, string> query = null) =>
 Task.FromResult(resp);
 public ConsulPathAttribute(string path, HttpMethodKind httpMethodKind = HttpMethodKind.Get)
 {
     Path       = path;
     HttpMethod = httpMethodKind.ToHttpMethod();
 }
 public HttpFullUrlAttribute(string url, HttpMethodKind httpMethodKind = HttpMethodKind.Get) : base(url, httpMethodKind)
 {
 }
Example #12
0
        public HttpUrlAttribute(string url, HttpMethodKind httpMethodKind = HttpMethodKind.Get)
        {
            Url = url;

            HttpMethod = httpMethodKind.ToHttpMethod();
        }