コード例 #1
0
        public Uri BuildRequestUri(Methods method, Version methodApiVersion, SubsonicParameters parameters = null, bool checkForTokenUsability = true)
        {
            var uriBuilder = new UriBuilder(Url);

            var pathBuilder = new StringBuilder(uriBuilder.Path);

            pathBuilder.AppendFormat("/rest/{0}.view", method.GetXmlEnumAttribute());
            uriBuilder.Path = Regex.Replace(pathBuilder.ToString(), "/+", "/");

            var queryBuilder = new StringBuilder();

            queryBuilder.AppendFormat("v={0}&c={1}", methodApiVersion, ClientName);

            if (parameters != null && parameters.Parameters.Count > 0)
            {
                foreach (var parameter in parameters.Parameters)
                {
                    var key   = string.Empty;
                    var value = string.Empty;

                    if (parameter is DictionaryEntry entry)
                    {
                        key   = entry.Key.ToString();
                        value = entry.Value.ToString();
                    }
                    else if (parameter is KeyValuePair <string, string> kvpEntry)
                    {
                        key   = kvpEntry.Key;
                        value = kvpEntry.Value;
                    }

                    queryBuilder.AppendFormat("&{0}={1}", Uri.EscapeDataString(key), Uri.EscapeDataString(value));
                }
            }

            if (checkForTokenUsability && ShouldUseNewAuthentication())
            {
                var subsonicToken = SubsonicAuthentication.GetToken();
                queryBuilder.AppendFormat("&u={0}&t={1}&s={2}", UserName, subsonicToken.Token, subsonicToken.Salt);
            }

            uriBuilder.Query = queryBuilder.ToString();
            return(uriBuilder.Uri);
        }
コード例 #2
0
        public static SubsonicParameters Create(SubsonicParameterType type = SubsonicParameterType.Single)
        {
            var parameters = new SubsonicParameters {
                ParameterType = type
            };

            switch (type)
            {
            case SubsonicParameterType.List:
                parameters.Parameters = new List <KeyValuePair <string, string> >();
                break;

            case SubsonicParameterType.Single:
                parameters.Parameters = new Dictionary <string, string>();
                break;
            }

            return(parameters);
        }
コード例 #3
0
        public Uri BuildRequestUriUser(Methods method, Version methodApiVersion, SubsonicParameters parameters = null)
        {
            var uriBuilder = new UriBuilder(BuildRequestUri(method, methodApiVersion, parameters, false));

            var queryBuilder = new StringBuilder(uriBuilder.Query.TrimStart('?'));

            if (ShouldUseNewAuthentication())
            {
                var subsonicToken = SubsonicAuthentication.GetToken();
                queryBuilder.AppendFormat("&u={0}&t={1}&s={2}", UserName, subsonicToken.Token, subsonicToken.Salt);
            }
            else
            {
                var encodedPassword = $"enc:{Password.ToHexString()}";
                queryBuilder.AppendFormat("&u={0}&p={1}", UserName, encodedPassword);
            }

            uriBuilder.Query = queryBuilder.ToString();

            return(uriBuilder.Uri);
        }
コード例 #4
0
        public virtual async Task <TResponse> GetResponseAsync <TResponse>(Methods method, Version methodApiVersion, SubsonicParameters parameters = null, CancellationToken?cancelToken = null)
        {
            ValidateApiVersion(method, methodApiVersion);

            var result = default(TResponse);

            var response = await SubsonicRequest.RequestAsync(method, methodApiVersion, parameters, cancelToken);

            switch (response.Status)
            {
            case ResponseStatus.Ok:
                result = (TResponse)response.Item;
                break;

            case ResponseStatus.Failed:
                if (response.ItemElementName == ItemChoiceType.Error)
                {
                    throw new SubsonicErrorException(string.Format(CultureInfo.CurrentCulture, "Error occurred in {0}", method.GetXmlEnumAttribute()), response.Item as Error);
                }

                throw new SubsonicApiException(string.Format(CultureInfo.CurrentCulture, "Unknown error occurred in {0}", method.GetXmlEnumAttribute()));
            }

            return(result);
        }
コード例 #5
0
        public virtual async Task <long> GetResponseAsync(string path, bool pathOverride, Methods method, Version methodApiVersion, SubsonicParameters parameters = null, CancellationToken?cancelToken = null)
        {
            ValidateApiVersion(method, methodApiVersion);

            return(await SubsonicRequest.RequestAsync(path, pathOverride, method, methodApiVersion, parameters, cancelToken));
        }
コード例 #6
0
        public virtual async Task GetNoResponseAsync(Methods method, Version methodApiVersion, SubsonicParameters parameters = null, CancellationToken?cancelToken = null)
        {
            ValidateApiVersion(method, methodApiVersion);

            await SubsonicRequest.RequestWithoutResponseAsync(method, methodApiVersion, parameters, cancelToken);
        }
コード例 #7
0
 public virtual async Task <IImageFormat <T> > GetImageResponseAsync(Methods method, Version methodApiVersion, SubsonicParameters parameters = null, CancellationToken?cancelToken = null)
 {
     ValidateApiVersion(method, methodApiVersion);
     return(await SubsonicRequest.ImageRequestAsync(method, methodApiVersion, parameters, cancelToken));
 }
コード例 #8
0
 public virtual async Task <long> GetContentLengthAsync(Methods method, Version methodApiVersion, SubsonicParameters parameters = null, CancellationToken?cancelToken = null)
 {
     ValidateApiVersion(method, methodApiVersion);
     return(await SubsonicRequest.ContentLengthRequestAsync(method, methodApiVersion, parameters, cancelToken));
 }
コード例 #9
0
        public virtual async Task <long> ContentLengthRequestAsync(Methods method, Version methodApiVersion, SubsonicParameters parameters = null, CancellationToken?cancelToken = null)
        {
            var requestUri    = SubsonicServer.BuildRequestUri(method, methodApiVersion, parameters);
            var clientHandler = GetClientHandler();
            var client        = GetClient(clientHandler);

            long length = -1;

            cancelToken?.ThrowIfCancellationRequested();

            try
            {
                using (var message = new HttpRequestMessage(System.Net.Http.HttpMethod.Head, requestUri))
                    using (var response = cancelToken.HasValue ? await client.SendAsync(message, cancelToken.Value) : await client.SendAsync(message))
                    {
                        if (!response.IsSuccessStatusCode)
                        {
                            throw new SubsonicApiException(string.Format(CultureInfo.CurrentCulture, "Unexpected response status code: {0}", Enum.GetName(typeof(ItemChoiceType), response.StatusCode)));
                        }

                        if (response.Content.Headers.ContentLength != null)
                        {
                            length = response.Content.Headers.ContentLength.GetValueOrDefault();
                        }
                    }
            }
            catch (Exception ex)
            {
                throw new SubsonicApiException(ex.Message, ex);
            }

            cancelToken?.ThrowIfCancellationRequested();

            return(length);
        }
コード例 #10
0
        public virtual async Task <string> StringRequestAsync(Methods method, Version methodApiVersion, SubsonicParameters parameters = null, CancellationToken?cancelToken = null)
        {
            var requestUri    = SubsonicServer.BuildRequestUri(method, methodApiVersion, parameters);
            var clientHandler = GetClientHandler();
            var client        = GetClient(clientHandler);

            cancelToken?.ThrowIfCancellationRequested();

            try
            {
                using (var response = cancelToken.HasValue ? await client.PostAsync(requestUri, null, cancelToken.Value) : await client.PostAsync(requestUri, null))
                {
                    var stringResponse = await response.Content.ReadAsStringAsync();

                    if (stringResponse == null)
                    {
                        throw new SubsonicErrorException("HTTP response contains no content");
                    }

                    if (!response.Content.Headers.ContentType.MediaType.Contains(HttpContentTypes.TextXml))
                    {
                        return(stringResponse);
                    }

                    var result = await DeserializeResponseAsync(stringResponse);

                    if (result.ItemElementName == ItemChoiceType.Error)
                    {
                        throw new SubsonicErrorException("Error occurred during request.", result.Item as Error);
                    }

                    throw new SubsonicApiException(string.Format(CultureInfo.CurrentCulture, "Unexpected response type: {0}", Enum.GetName(typeof(ItemChoiceType), result.ItemElementName)));
                }
            }
            catch (Exception ex)
            {
                throw new SubsonicApiException(ex.Message, ex);
            }
        }
コード例 #11
0
        public virtual async Task RequestWithoutResponseAsync(Methods method, Version methodApiVersion, SubsonicParameters parameters = null, CancellationToken?cancelToken = null)
        {
            var requestUri    = SubsonicServer.BuildRequestUri(method, methodApiVersion, parameters);
            var clientHandler = GetClientHandler();
            var client        = GetClient(clientHandler);

            cancelToken?.ThrowIfCancellationRequested();

            try
            {
                using (var message = new HttpRequestMessage(System.Net.Http.HttpMethod.Head, requestUri))
                    using (var ignored = cancelToken.HasValue ? await client.SendAsync(message, cancelToken.Value) : await client.SendAsync(message)) { }
            }
            catch (Exception ex)
            {
                throw new SubsonicApiException(ex.Message, ex);
            }

            cancelToken?.ThrowIfCancellationRequested();
        }
コード例 #12
0
 public virtual Task <long> RequestAsync(string path, bool pathOverride, Methods method, Version methodApiVersion, SubsonicParameters parameters = null, CancellationToken?cancelToken = null)
 {
     throw new SubsonicApiException("Unsupported method");
 }
コード例 #13
0
        public virtual async Task <Response> RequestAsync(Methods method, Version methodApiVersion, SubsonicParameters parameters = null, CancellationToken?cancelToken = null)
        {
            var requestUri    = SubsonicServer.BuildRequestUri(method, methodApiVersion, parameters);
            var clientHandler = GetClientHandler();
            var client        = GetClient(clientHandler);

            cancelToken?.ThrowIfCancellationRequested();

            Response result;

            try
            {
                using (var response = cancelToken.HasValue ? await client.PostAsync(requestUri, null, cancelToken.Value) : await client.PostAsync(requestUri, null))
                {
                    var stringResponse = await response.Content.ReadAsStringAsync();

                    if (stringResponse == null)
                    {
                        throw new SubsonicErrorException("HTTP response contains no content");
                    }

                    if (response.Content.Headers.ContentType.MediaType.Contains(HttpContentTypes.TextXml))
                    {
                        result = await DeserializeResponseAsync(stringResponse);
                    }
                    else
                    {
                        throw new SubsonicApiException(string.Format(CultureInfo.CurrentCulture, "HTTP response does not contain XML, content type is: {0}", response.Content.Headers.ContentType.MediaType));
                    }
                }
            }
            catch (Exception ex)
            {
                throw new SubsonicApiException(ex.Message, ex);
            }

            cancelToken?.ThrowIfCancellationRequested();

            return(result);
        }