Пример #1
0
        /// <summary>
        /// Send the given request to the given seafile server
        /// </summary>
        /// <param name="serverUri">The server address</param>
        /// <param name="request">The request to send</param>
        /// <param name="timeout">User-defined request timeout (if non null)</param>
        /// <returns>The response</returns>
        /// <exception cref="SeafException"></exception>
        public async Task <T> SendRequestAsync <T>(Uri serverUri, SeafRequest <T> request, TimeSpan?timeout)
        {
            if (client == null)
            {
                throw new InvalidOperationException("This SeafHttpConnection has already been closed.");
            }

            HttpRequestMessage requestMessage = CreateHttpRequestMessage(serverUri, request);

            HttpResponseMessage response;

            // when no custom timeout has been passed use the client's default timeout
            using (CancellationTokenSource cTokenSource = new CancellationTokenSource(timeout ?? client.Timeout))
            {
                response = await client.SendAsync(requestMessage, cTokenSource.Token);
            }

            if (request.WasSuccessful(response))
            {
                return(await request.ParseResponseAsync(response));
            }
            else
            {
                throw GetSeafException(request, response);
            }
        }
Пример #2
0
        /// <summary>
        ///     Return an HttpRequestMessage which represents the given seafile request
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="serverUri"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public HttpRequestMessage CreateHttpRequestMessage <T>(Uri serverUri, SeafRequest <T> request)
        {
            var targetUri = new Uri(serverUri, request.CommandUri);

            switch (request.HttpAccessMethod)
            {
            case HttpAccessMethod.Custom:
                return(request.GetCustomizedRequest(serverUri));

            default:
                return(HttpUtils.CreateRequest(GetHttpMethod(request.HttpAccessMethod), targetUri, request.GetAdditionalHeaders(), request.GetBodyParameters()));
            }
        }
Пример #3
0
        public async Task <T> SendRequestAsync <T>(Uri serverUri, SeafRequest <T> request)
        {
            HttpRequestMessage requestMessage = CreateHttpRequestMessage(serverUri, request);

            HttpResponseMessage response;
            HttpClientHandler   handler = new HttpClientHandler();

            handler.AllowAutoRedirect = false;
            using (HttpClient client = new HttpClient(handler))
                response = await client.SendAsync(requestMessage);

            if (request.WasSuccessful(response))
            {
                return(await request.ParseResponseAsync(response));
            }
            else
            {
                throw new SeafException(request.GetSeafError(response));
            }
        }
Пример #4
0
        /// <summary>
        /// Return an HttpRequestMessage which represents the given seafile request
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="serverUri"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public HttpRequestMessage CreateHttpRequestMessage <T>(Uri serverUri, SeafRequest <T> request)
        {
            Uri targetUri;

            if (string.IsNullOrEmpty(serverUri.LocalPath))
            {
                targetUri = new Uri(serverUri, request.CommandUri);
            }
            else
            {
                string localpath = serverUri.LocalPath;
                if (!localpath[localpath.Length - 1].Equals('/'))
                {
                    localpath += '/';
                }

                targetUri = new Uri(serverUri, localpath + request.CommandUri);
            }

            switch (request.HttpAccessMethod)
            {
            case HttpAccessMethod.Get:
                return(HttpUtils.CreateSimpleRequest(HttpMethod.Get, targetUri, request.GetAdditionalHeaders()));

            case HttpAccessMethod.Post:
                return(HttpUtils.CreatePostRequest(targetUri, request.GetAdditionalHeaders(), request.GetPostParameters()));

            case HttpAccessMethod.Put:
                return(HttpUtils.CreatePutRequest(targetUri, request.GetAdditionalHeaders(), request.GetPostParameters()));

            case HttpAccessMethod.Delete:
                return(HttpUtils.CreateSimpleRequest(HttpMethod.Delete, targetUri, request.GetAdditionalHeaders()));

            case HttpAccessMethod.Custom:
                return(request.GetCustomizedRequest(serverUri));

            default:
                throw new ArgumentException("Unsupported method: " + request.HttpAccessMethod.ToString());
            }
        }
Пример #5
0
        SeafException GetSeafException <T>(SeafRequest <T> request, HttpResponseMessage response)
        {
            if (response.StatusCode == (System.Net.HttpStatusCode) 429) // TooManyRequests
            {
                IEnumerable <string> values;
                if (response.Headers.TryGetValues("X-Throttle-Wait-Seconds", out values))
                {
                    int seconds;
                    if (int.TryParse(values.First(), out seconds))
                    {
                        return(new SeafTooManyRequestsException(TimeSpan.FromSeconds(seconds)));
                    }
                }

                // could not read the wait header
                return(new SeafTooManyRequestsException(TimeSpan.FromSeconds(30)));
            }
            else
            {
                return(new SeafException(request.GetSeafError(response)));
            }
        }
Пример #6
0
 /// <summary>
 /// Send the given request to the given seafile server
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="serverUri">The server address</param>
 /// <param name="request">The request to send</param>
 /// <exception cref="SeafException"></exception>
 /// <returns>The response</returns>
 public async Task <T> SendRequestAsync <T>(Uri serverUri, SeafRequest <T> request)
 {
     return(await SendRequestAsync(serverUri, request, null));
 }